Skip to content

Commit

Permalink
chore: debug
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Mar 16, 2021
1 parent e7bcbad commit 86ee6ae
Show file tree
Hide file tree
Showing 16 changed files with 1,293 additions and 35 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@1stg/tslint-config": "^1.2.0",
"@types/eslint": "^7.2.7",
"@types/jest": "^26.0.20",
"@types/node": "^14.14.34",
"@types/node": "^14.14.35",
"@types/react": "^17.0.3",
"@types/rebass": "^4.0.8",
"@types/unist": "^2.0.3",
Expand Down Expand Up @@ -60,6 +60,7 @@
"fixtures",
"lib",
"CHANGELOG.md",
"packages/**/plugins/*.js",
"!/.*.js"
],
"jest": {
Expand All @@ -75,7 +76,7 @@
"statements": 100
}
},
"preset": "ts-jest"
"preset": "ts-jest/presets/js-with-ts"
},
"prettier": "@1stg/prettier-config",
"remarkConfig": {
Expand Down
7 changes: 6 additions & 1 deletion packages/eslint-mdx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,14 @@
"eslint": ">=5.0.0"
},
"dependencies": {
"hast-util-to-estree": "^1.3.2",
"remark-mdx": "https://gitpkg.now.sh/mdx-js/mdx/packages/remark-mdx?main",
"remark-parse": "^9.0.0",
"remark-rehype": "^8.0.0",
"tslib": "^2.1.0",
"unified": "^9.2.1"
"unified": "^9.2.1",
"unist-builder": "^2.0.3",
"unist-util-position-from-estree": "^1.0.0",
"unist-util-visit": "^2.0.3"
}
}
69 changes: 44 additions & 25 deletions packages/eslint-mdx/src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
/// <reference path="../typings.d.ts" />

import path from 'path'

import type { AST, Linter } from 'eslint'
import remarkMdx from 'remark-mdx'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import unified from 'unified'
import type { Parent } from 'unist'

import { isMdxNode, normalizeParser, normalizePosition } from './helpers'
import { traverse } from './traverse'
import { MDX_NODE_TYPES, normalizeParser, normalizePosition } from './helpers'
import { recmaDocument } from './plugins/recma-document'
import { recmaJsxRewrite } from './plugins/recma-jsx-rewrite'
import { rehypeRecma } from './plugins/rehype-recma'
import { remarkMarkAndUnravel } from './plugins/remark-mark-and-unravel'
import type { ParserFn, ParserOptions } from './types'

export const mdProcessor = unified().use(remarkParse).freeze()
Expand All @@ -28,6 +33,15 @@ export const getEsLintMdxProcessor = (tokens: AST.Token[]) =>
},
},
)
.use(remarkMarkAndUnravel)
.use(remarkRehype, {
allowDangerousHtml: true,
passThrough: [...MDX_NODE_TYPES],
})
.use(rehypeRecma)
.use(recmaDocument)
// @ts-ignore
.use(recmaJsxRewrite)
.freeze()

export const AST_PROPS = ['body', 'comments'] as const
Expand Down Expand Up @@ -84,31 +98,36 @@ export class Parser {

const tokens: AST.Token[] = []

const root = (isMdx ? getEsLintMdxProcessor(tokens) : mdProcessor).parse(
code,
) as Parent

this._ast = {
...normalizePosition(root.position),
type: 'Program',
sourceType: options.sourceType,
body: [],
comments: [],
tokens,
}
const processor = isMdx ? getEsLintMdxProcessor(tokens) : mdProcessor
const root = processor.runSync(processor.parse(code))

if (isMdx) {
traverse(root, node => {
if (!isMdxNode(node)) {
return
}
console.log(JSON.stringify(root, null, 2))

for (const prop of AST_PROPS) {
// @ts-ignore
this._ast[prop].push(...(node.data?.estree[prop] || []))
this._ast = isMdx
? ((root as unknown) as AST.Program)
: {
...normalizePosition(root.position),
type: 'Program',
sourceType: options.sourceType,
body: [],
comments: [],
tokens,
}
})
}

this._ast.tokens = tokens

// if (isMdx) {
// traverse(root, node => {
// if (!isMdxNode(node)) {
// return
// }

// for (const prop of AST_PROPS) {
// // @ts-ignore
// this._ast[prop].push(...(node.data?.estree?.[prop] || []))
// }
// })
// }

return { ast: this._ast }
}
Expand Down
20 changes: 20 additions & 0 deletions packages/eslint-mdx/src/plugins/estree-util-create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @template {import('estree').Node} N
* @param {import('estree').Node} template
* @param {N} node
* @returns {N}
*/
export function create(template, node) {
const fields = ['start', 'end', 'loc', 'range', 'comments']
let index = -1
let field

while (++index < fields.length) {
field = fields[index]
if (field in template) {
node[field] = template[field]
}
}

return node
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import u from 'unist-builder'

import { create } from './estree-util-create.js'

/**
* @typedef {import('estree').ImportSpecifier} ImportSpecifier
* @typedef {import('estree').ImportDefaultSpecifier} ImportDefaultSpecifier
* @typedef {import('estree').ImportNamespaceSpecifier} ImportNamespaceSpecifier
* @typedef {import('estree').ExportSpecifier} ExportSpecifier
*
* @param {Array<ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier | ExportSpecifier>} specifiers
* @returns {import('estree').ObjectPattern}
*/
export function specifiersToObjectPattern(specifiers) {
return u('ObjectPattern', {
properties: specifiers.map(specifier => {
const key =
'imported' in specifier
? specifier.imported
: 'exported' in specifier
? specifier.exported
: u('Identifier', { name: 'default' })
return create(
specifier,
u('Property', {
kind: 'init',
shorthand: key.name === specifier.local.name,
method: false,
computed: false,
key,
value: specifier.local,
}),
)
}),
})
}
Loading

0 comments on commit 86ee6ae

Please sign in to comment.