-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
177 additions
and
97 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ before_install: | |
script: | ||
- set -e | ||
- yarn lint | ||
- yarn test | ||
- yarn build | ||
|
||
before_deploy: | ||
|
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,46 +1,27 @@ | ||
import { isComment, COMMENT_CONTENT_REGEX } from './regexp' | ||
import { mdxProcessor } from './parser' | ||
|
||
import { Node, Parent } from 'unist' | ||
import { Node } from 'unist' | ||
|
||
export const normalizeJsxNode = (node: Node) => { | ||
const rawText = node.value as string | ||
if (!isComment(rawText)) { | ||
const matched = rawText.match(COMMENT_CONTENT_REGEX) | ||
if (matched) { | ||
node.value = rawText.replace( | ||
COMMENT_CONTENT_REGEX, | ||
(_matched, $0) => `{/*${$0}*/}`, | ||
) | ||
} | ||
let rawText = node.value as string | ||
|
||
if (isComment(rawText)) { | ||
return node | ||
} | ||
return node | ||
} | ||
|
||
export const normalizeMdx = (source: string) => { | ||
const lines = source.split('\n').length | ||
const { children } = mdxProcessor.parse(source) as Parent | ||
let lastLine: number | ||
return children.reduce((result, node, index) => { | ||
const { | ||
position: { start, end }, | ||
} = node | ||
const startLine = start.line | ||
const endLine = end.line | ||
if (lastLine != null && lastLine !== startLine) { | ||
result += '\n'.repeat(startLine - lastLine) | ||
} | ||
if (node.type === 'jsx') { | ||
result += normalizeJsxNode(node).value | ||
} else { | ||
result += source.slice(start.offset, end.offset) | ||
} | ||
const matched = rawText.match(COMMENT_CONTENT_REGEX) | ||
|
||
if (index === children.length - 1 && endLine < lines) { | ||
result += '\n'.repeat(lines - endLine) | ||
} | ||
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)}/}`, | ||
) | ||
|
||
lastLine = endLine | ||
return result | ||
}, '') | ||
return node | ||
} |
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 was deleted.
Oops, something went wrong.
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,5 +1,9 @@ | ||
import { noJsxHtmlComments } from './no-jsx-html-comments' | ||
import { noUnUsedExpressions } from './no-unused-expressions' | ||
|
||
export { noJsxHtmlComments, noUnUsedExpressions } | ||
|
||
export const rules = { | ||
'no-jsx-html-comments': noJsxHtmlComments, | ||
'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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { ExpressionStatementWithParent, JSX_TYPES, JsxType } from './types' | ||
|
||
import { Rule } from 'eslint' | ||
import { Node } from 'unist' | ||
|
||
export const noJsxHtmlComments: Rule.RuleModule = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Forbid invalid html style comments in jsx block', | ||
category: 'SyntaxError', | ||
recommended: true, | ||
}, | ||
messages: { | ||
jdxHtmlComments: 'html style comments are invalid in jsx: {{ raw }}', | ||
}, | ||
fixable: 'code', | ||
schema: [], | ||
}, | ||
create(context) { | ||
return { | ||
ExpressionStatement(node: ExpressionStatementWithParent) { | ||
const invalidNodes: Node[] = | ||
context.parserServices.JSXElementsWithHTMLComments | ||
|
||
if ( | ||
!JSX_TYPES.includes(node.expression.type as JsxType) || | ||
node.parent.type !== 'Program' || | ||
!invalidNodes || | ||
!invalidNodes.length | ||
) { | ||
return | ||
} | ||
|
||
const invalidNode = invalidNodes.shift() | ||
// unist column is 1-indexed, but estree is 0-indexed... | ||
const { start, end } = invalidNode.position | ||
context.report({ | ||
messageId: 'jdxHtmlComments', | ||
data: { | ||
raw: invalidNode.raw as string, | ||
}, | ||
loc: { | ||
start: { | ||
...start, | ||
column: start.column - 1, | ||
}, | ||
end: { | ||
...end, | ||
column: end.column - 1, | ||
}, | ||
}, | ||
fix(fixer) { | ||
return fixer.replaceTextRange( | ||
[start.offset, end.offset], | ||
invalidNode.value as string, | ||
) | ||
}, | ||
}) | ||
}, | ||
} | ||
}, | ||
} as Rule.RuleModule |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { ExpressionStatement, Node } from 'estree' | ||
|
||
export const JSX_TYPES = ['JSXElement', 'JSXFragment'] as const | ||
|
||
export type JsxType = (typeof JSX_TYPES)[number] | ||
|
||
export interface ExpressionStatementWithParent extends ExpressionStatement { | ||
parent?: { | ||
type: Node['type'] | ||
} | ||
} |
Oops, something went wrong.