Skip to content

Commit

Permalink
fix: upgrade micromark-util-events-to-acorn (#383)
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin authored Apr 21, 2022
1 parent c497839 commit b5e8987
Show file tree
Hide file tree
Showing 10 changed files with 1,698 additions and 805 deletions.
28 changes: 6 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"lint": "run-p lint:*",
"lint:es": "eslint . --cache -f friendly",
"lint:tsc": "tsc --noEmit",
"postinstall": "simple-git-hooks && yarn-deduplicate --strategy fewer || exit 0",
"postinstall": "patch-package && simple-git-hooks && yarn-deduplicate --strategy fewer || exit 0",
"prelint": "yarn build",
"prerelease": "yarn build",
"release": "lerna publish --conventional-commits --create-release github --yes",
Expand All @@ -30,19 +30,13 @@
"@types/eslint": "^8.4.1",
"@types/eslint-plugin-markdown": "^2.0.0",
"@types/jest": "^27.4.1",
"@types/node": "^17.0.23",
"@types/react": "^17.0.43",
"@types/node": "^17.0.25",
"@types/react": "^18.0.5",
"@types/unist": "^2.0.6",
"lerna": "^4.0.0",
"patch-package": "^6.4.7",
"react": "^18.0.0",
"remark-frontmatter": "4.0.1",
"remark-gfm": "^3.0.1",
"remark-lint": "^9.1.1",
"remark-preset-lint-consistent": "^5.1.1",
"remark-preset-lint-markdown-style-guide": "^5.1.2",
"remark-preset-lint-recommended": "^6.1.2",
"remark-preset-prettier": "^1.0.0",
"remark-validate-links": "^11.0.2",
"ts-jest": "^27.1.4",
"ts-node": "^10.7.0",
"type-coverage": "^2.21.1",
Expand Down Expand Up @@ -85,19 +79,9 @@
},
"prettier": "@1stg/prettier-config",
"remarkConfig": {
"settings": {
"emphasis": "_",
"strong": "*"
},
"plugins": [
"frontmatter",
"lint",
"preset-lint-consistent",
"preset-lint-markdown-style-guide",
"preset-lint-recommended",
"preset-prettier",
"gfm",
"validate-links"
"@1stg/config",
"frontmatter"
]
},
"renovate": {
Expand Down
17 changes: 6 additions & 11 deletions packages/eslint-mdx/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ import { getPhysicalFilename } from './processor'
import { traverse } from './traverse'
import type { ParserOptions, WorkerParseResult } from './types'

export const AST_PROPS = [
'body',
// disable comments temporarily -- #380
// 'comments'
] as const

export const DEFAULT_EXTENSIONS: readonly string[] = ['.mdx']
export const MARKDOWN_EXTENSIONS: readonly string[] = ['.md']

Expand Down Expand Up @@ -85,14 +79,14 @@ export class Parser {
})
}

const { root, tokens, comments } = result
const { root, tokens } = result

this._ast = {
...normalizePosition(root.position),
type: 'Program',
sourceType,
body: [],
comments,
comments: [],
tokens,
}

Expand All @@ -102,9 +96,10 @@ export class Parser {
return
}

for (const prop of AST_PROPS) {
this._ast[prop].push(...(node.data?.estree[prop] || []))
}
const estree = node.data?.estree

this._ast.body.push(...(estree?.body || []))
this._ast.comments.push(...(estree?.comments || []))
})
}

Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-mdx/src/traverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ export class Traverse {

const children = (node as Parent).children

this._enter(node, parent)

if (children) {
const parent = node as Parent
for (const child of children) {
this.traverse(child, parent)
}
}

this._enter(node, parent)
}
}

Expand Down
3 changes: 1 addition & 2 deletions packages/eslint-mdx/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { AST, Linter } from 'eslint'
import type { Comment, Program } from 'estree'
import type { Program } from 'estree'
import type { Plugin } from 'unified'
import type { Node, Parent } from 'unist'
import type { VFileOptions } from 'vfile'
Expand Down Expand Up @@ -51,7 +51,6 @@ export interface WorkerOptions {
export interface WorkerParseResult {
root: Parent
tokens: AST.Token[]
comments: Comment[]
}

export interface WorkerProcessResult {
Expand Down
22 changes: 4 additions & 18 deletions packages/eslint-mdx/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
/* eslint-disable unicorn/no-await-expression-member */
import path from 'path'

import type { Comment, Token } from 'acorn'
import type { Token } from 'acorn'
import { cosmiconfig } from 'cosmiconfig'
import type { CosmiconfigResult } from 'cosmiconfig/dist/types'
import type { AST } from 'eslint'
import type { EsprimaToken } from 'espree/lib/token-translator'
import type { Comment as EsComment } from 'estree'
import type { Options } from 'micromark-extension-mdx-expression'
import { extractProperties, runAsWorker } from 'synckit'
import type { FrozenProcessor } from 'unified'
Expand All @@ -33,22 +32,17 @@ const explorer = cosmiconfig('remark', {

export const processorCache = new Map<string, FrozenProcessor>()

const getRemarkMdxOptions = (
tokens: Token[],
comments: Comment[],
): Options => ({
const getRemarkMdxOptions = (tokens: Token[]): Options => ({
acornOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
locations: true,
ranges: true,
onToken: tokens,
onComment: comments,
},
})

const sharedTokens: Token[] = []
const sharedComments: Comment[] = []

export const getRemarkProcessor = async (
searchFrom: string,
Expand Down Expand Up @@ -111,10 +105,7 @@ export const getRemarkProcessor = async (
.use(remarkStringify)

if (isMdx) {
initProcessor.use(
remarkMdx,
getRemarkMdxOptions(sharedTokens, sharedComments),
)
initProcessor.use(remarkMdx, getRemarkMdxOptions(sharedTokens))
}

cachedProcessor = (
Expand All @@ -136,10 +127,7 @@ export const getRemarkProcessor = async (
const initProcessor = remarkProcessor().use(remarkStringify)

if (isMdx) {
initProcessor.use(
remarkMdx,
getRemarkMdxOptions(sharedTokens, sharedComments),
)
initProcessor.use(remarkMdx, getRemarkMdxOptions(sharedTokens))
}

cachedProcessor = initProcessor.freeze()
Expand All @@ -161,7 +149,6 @@ runAsWorker(
ignoreRemarkConfig,
}: WorkerOptions): Promise<WorkerResult> => {
sharedTokens.length = 0
sharedComments.length = 0

const processor = await getRemarkProcessor(
physicalFilename,
Expand Down Expand Up @@ -250,7 +237,6 @@ runAsWorker(
return {
root,
tokens,
comments: sharedComments as EsComment[],
}
},
)
4 changes: 4 additions & 0 deletions packages/eslint-plugin-mdx/src/configs/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import type { Linter } from 'eslint'

export const base: Linter.Config = {
parser: 'eslint-mdx',
parserOptions: {
sourceType: 'module',
ecmaVersion: 'latest',
},
plugins: ['mdx'],
processor: 'mdx/remark',
rules: {
Expand Down
Loading

0 comments on commit b5e8987

Please sign in to comment.