-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new rule no-unescaped-entities
- Loading branch information
Showing
16 changed files
with
320 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,76 @@ | ||
import { last } from './helper' | ||
import { isComment, COMMENT_CONTENT_REGEX } from './regexp' | ||
|
||
import { Node } from 'unist' | ||
import { Node, Point, Parent } from 'unist' | ||
|
||
export const normalizeJsxNode = (node: Node) => { | ||
let rawText = node.value as string | ||
export interface Comment { | ||
fixed: string | ||
loc: { | ||
start: Point | ||
end: Point | ||
} | ||
origin: string | ||
} | ||
|
||
export const normalizeJsxNode = (node: Node, parent?: Parent) => { | ||
const value = node.value as string | ||
|
||
if (isComment(rawText)) { | ||
if (isComment(value) || !parent) { | ||
return node | ||
} | ||
|
||
const matched = rawText.match(COMMENT_CONTENT_REGEX) | ||
const matched = value.match(COMMENT_CONTENT_REGEX) | ||
|
||
if (!matched) { | ||
return node | ||
} | ||
|
||
node.jsxType = 'JSXElementWithHTMLComments' | ||
node.raw = rawText | ||
rawText = node.value = rawText.replace( | ||
COMMENT_CONTENT_REGEX, | ||
(_matched, $0, $1, $2) => | ||
`{/${'*'.repeat($0.length - 1)}${$1}${'*'.repeat($2.length - 1)}/}`, | ||
) | ||
const comments: Comment[] = [] | ||
const { | ||
position: { | ||
start: { line, column, offset: startOffset }, | ||
}, | ||
} = node | ||
|
||
return 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.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 | ||
}, | ||
), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import { noJsxHtmlComments } from './no-jsx-html-comments' | ||
import { noUnEscapedEntities } from './no-unescaped-entities' | ||
import { noUnUsedExpressions } from './no-unused-expressions' | ||
|
||
export { noJsxHtmlComments, noUnUsedExpressions } | ||
|
||
export const rules = { | ||
'no-jsx-html-comments': noJsxHtmlComments, | ||
'no-unescaped-entities': noUnEscapedEntities, | ||
'no-unused-expressions': noUnUsedExpressions, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.