Skip to content

Commit

Permalink
fix: adjacent jsx nodes should be allowed in mdx
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Aug 4, 2019
1 parent 5937b07 commit 8456381
Show file tree
Hide file tree
Showing 12 changed files with 318 additions and 191 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-mdx",
"version": "0.9.3",
"version": "0.9.4",
"description": "ESLint Parser/Plugin for MDX",
"repository": "git@github.com:rx-ts/eslint-plugin-mdx.git",
"author": "JounQin <admin@1stg.me>",
Expand Down
73 changes: 7 additions & 66 deletions packages/eslint-mdx/src/helper.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { isComment, COMMENT_CONTENT_REGEX } from './regexp'
import { Comment, Arrayable } from './types'
import { Arrayable, JsxNode, JsxType, JsxTypes } from './types'

import { Position, Node, Parent } from 'unist'
import { Position } from 'unist'
import { AST } from 'eslint'
// `SourceLocation` is not exported from estree, but it is actually working
// eslint-disable-next-line import/named
import { SourceLocation } from 'estree'

export const JSX_TYPES: JsxTypes = ['JSXElement', 'JSXFragment']

export const isJsxNode = (node: { type: string }): node is JsxNode =>
JSX_TYPES.includes(node.type as JsxType)

export const normalizePosition = (position: Position) => {
const start = position.start.offset
const end = position.end.offset
Expand Down Expand Up @@ -82,69 +86,6 @@ export const first = <T>(items: T[] | ReadonlyArray<T>) => items && items[0]
export const last = <T>(items: T[] | ReadonlyArray<T>) =>
items && items[items.length - 1]

export const normalizeJsxNode = (node: Node, parent?: Parent) => {
const value = node.value as string

if (isComment(value)) {
return node
}

const matched = value.match(COMMENT_CONTENT_REGEX)

if (!matched) {
return node
}

const comments: Comment[] = []
const {
position: {
start: { line, column, offset: startOffset },
},
} = node

return Object.assign(node, {
data: {
...node.data,
jsxType: 'JSXElementWithHTMLComments',
comments,
// jsx in paragraph is considered as plain html in mdx, what means html style comments are valid
// TODO: in this case, jsx style comments could be a mistake
inline: !!parent && parent.type !== 'root',
},
value: value.replace(
COMMENT_CONTENT_REGEX,
(matched: string, $0: string, $1: string, $2: string, offset: number) => {
const endOffset = offset + matched.length
const startLines = value.slice(0, offset).split('\n')
const endLines = value.slice(0, endOffset).split('\n')
const fixed = `{/${'*'.repeat($0.length - 2)}${$1}${'*'.repeat(
$2.length - 2,
)}/}`
const startLineOffset = startLines.length - 1
const endLineOffset = endLines.length - 1
comments.push({
fixed,
loc: {
start: {
line: line + startLineOffset,
column:
last(startLines).length + (startLineOffset ? 0 : column - 1),
offset: startOffset + offset,
},
end: {
line: line + endLineOffset - 1,
column: last(endLines).length + (endLineOffset ? 0 : column - 1),
offset: startOffset + endOffset,
},
},
origin: matched,
})
return fixed
},
),
})
}

export const hasProperties = <T, P extends keyof T = keyof T>(
obj: {},
properties: Arrayable<P>,
Expand Down
Loading

0 comments on commit 8456381

Please sign in to comment.