-
-
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.
feat: processor support for normalize comments in jsx [ci skip]
- Loading branch information
Showing
19 changed files
with
277 additions
and
398 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
} |
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { isComment, COMMENT_CONTENT_REGEX } from './regexp' | ||
import { mdxProcessor } from './parser' | ||
|
||
import { Node, Parent } 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}*/}`, | ||
) | ||
} | ||
} | ||
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) | ||
} | ||
|
||
if (index === children.length - 1 && endLine < lines) { | ||
result += '\n'.repeat(lines - endLine) | ||
} | ||
|
||
lastLine = endLine | ||
return result | ||
}, '') | ||
} |
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,10 @@ | ||
import { normalizeMdx } from './normalizer' | ||
|
||
export const processors = { | ||
'.mdx': { | ||
preprocess(code: string) { | ||
return [normalizeMdx(code)] | ||
}, | ||
supportsAutofix: true, | ||
}, | ||
} |
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
Oops, something went wrong.