Skip to content

Commit d2cd98e

Browse files
committed
test: add more unit and e2e tests
1 parent 58bd278 commit d2cd98e

18 files changed

+233
-32
lines changed

packages/eslint-mdx/src/helper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const JSX_TYPES: JsxTypes = ['JSXElement', 'JSXFragment']
2828
export const isJsxNode = (node: { type: string }): node is JsxNode =>
2929
JSX_TYPES.includes(node.type as JsxType)
3030

31-
export const normalizeParser = (parser: ParserOptions['parser']) => {
31+
export const normalizeParser = (parser?: ParserOptions['parser']) => {
3232
if (parser) {
3333
if (typeof parser === 'string') {
3434
// eslint-disable-next-line @typescript-eslint/no-require-imports

packages/eslint-mdx/src/regexp.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@ export const COMMENT_CONTENT_REGEX = new RegExp(
4242
export const isOpenTag = (text: string) => OPEN_TAG_REGEX.test(text)
4343
export const isCloseTag = (text: string) => CLOSE_TAG_REGEX.test(text)
4444
export const isOpenCloseTag = (text: string) => OPEN_CLOSE_TAG_REGEX.test(text)
45-
export const isSelfClosingTag = (text: string) =>
46-
SELF_CLOSING_TAG_REGEX.test(text)
45+
// prettier-ignore
46+
export const isSelfClosingTag = (text: string) => SELF_CLOSING_TAG_REGEX.test(text)
4747
export const isComment = (text: string) => COMMENT_REGEX.test(text)

packages/eslint-mdx/src/traverse.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,18 @@ export class Traverse {
3333
} else {
3434
if (isCloseTag(rawText)) {
3535
offset--
36-
} else if (
36+
}
37+
// prettier-ignore
38+
/* istanbul ignore next */
39+
else if (
3740
!isComment(rawText) &&
3841
!isSelfClosingTag(rawText) &&
3942
!isOpenCloseTag(rawText)
4043
) {
44+
// should never happen, just for robustness
4145
const { start } = node.position
4246
throw Object.assign(
43-
new SyntaxError(
44-
`'Unknown node type: ${JSON.stringify(
45-
node.type,
46-
)}, text: ${JSON.stringify(rawText)}`,
47-
),
47+
new SyntaxError('unknown jsx node: ' + JSON.stringify(rawText)),
4848
{
4949
lineNumber: start.line,
5050
column: start.column,
@@ -78,7 +78,9 @@ export class Traverse {
7878
}
7979

8080
traverse(node: Node, parent?: Parent) {
81+
/* istanbul ignore if */
8182
if (!node) {
83+
// should never happen, just for robustness
8284
return
8385
}
8486

packages/eslint-plugin-mdx/src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* istanbul ignore file */
2+
13
import { parse as oParse, parseForESLint as oParseForESLint } from 'eslint-mdx'
24

35
import * as configs from './configs'
@@ -13,11 +15,17 @@ const warn = () =>
1315
'parse from this plugin is deprecated, please use parser `eslint-mdx` directly',
1416
)
1517

18+
/**
19+
* @deprecated
20+
*/
1621
export const parse = (code: string, options?: Linter.ParserOptions) => {
1722
warn()
1823
return oParse(code, options)
1924
}
2025

26+
/**
27+
* @deprecated
28+
*/
2129
export const parseForESLint = (
2230
code: string,
2331
options?: Linter.ParserOptions,
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { noJsxHtmlComments } from './no-jsx-html-comments'
2-
import { noUnEscapedEntities } from './no-unescaped-entities'
3-
import { noUnUsedExpressions } from './no-unused-expressions'
2+
import { noUnescapedEntities } from './no-unescaped-entities'
3+
import { noUnusedExpressions } from './no-unused-expressions'
44

55
export * from './types'
66

7-
export { noJsxHtmlComments, noUnEscapedEntities, noUnUsedExpressions }
7+
export { noJsxHtmlComments, noUnescapedEntities, noUnusedExpressions }
88

99
export const rules = {
1010
'no-jsx-html-comments': noJsxHtmlComments,
11-
'no-unescaped-entities': noUnEscapedEntities,
12-
'no-unused-expressions': noUnUsedExpressions,
11+
'no-unescaped-entities': noUnescapedEntities,
12+
'no-unused-expressions': noUnusedExpressions,
1313
}

packages/eslint-plugin-mdx/src/rules/no-unescaped-entities.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/// <reference path="../../typings.d.ts" />
33

44
import { isJsxNode, openTag } from 'eslint-mdx'
5-
import reactNoUnEscapedEntities from 'eslint-plugin-react/lib/rules/no-unescaped-entities'
5+
import reactNoUnescapedEntities from 'eslint-plugin-react/lib/rules/no-unescaped-entities'
66

77
import { NodeWithParent } from './types'
88

@@ -37,8 +37,8 @@ const DEFAULTS: EscapeEntity[] = [
3737

3838
const EXPRESSION = 'Literal, JSXText'
3939

40-
export const noUnEscapedEntities: Rule.RuleModule = {
41-
...reactNoUnEscapedEntities,
40+
export const noUnescapedEntities: Rule.RuleModule = {
41+
...reactNoUnescapedEntities,
4242
create(context) {
4343
const configuration = context.options[0] || {}
4444
const entities: EscapeEntity[] = configuration.forbid || DEFAULTS
@@ -73,7 +73,9 @@ export const noUnEscapedEntities: Rule.RuleModule = {
7373
.join('\n')
7474
.search(openTag)
7575

76+
/* istanbul ignore if */
7677
if (firstLineOffset < 0) {
78+
// should never happen, just for robustness
7779
firstLineOffset = 0
7880
}
7981

packages/eslint-plugin-mdx/src/rules/no-unused-expressions.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
// eslint-disable-next-line @typescript-eslint/triple-slash-reference
22
/// <reference path="../../typings.d.ts" />
33

4-
import esLintNoUnUsedExpressions from 'eslint/lib/rules/no-unused-expressions'
4+
import esLintNoUnusedExpressions from 'eslint/lib/rules/no-unused-expressions'
55
import { isJsxNode } from 'eslint-mdx'
66

77
import { ExpressionStatementWithParent } from './types'
88

99
import { Rule } from 'eslint'
1010

11-
export const noUnUsedExpressions: Rule.RuleModule = {
12-
...esLintNoUnUsedExpressions,
11+
export const noUnusedExpressions: Rule.RuleModule = {
12+
...esLintNoUnusedExpressions,
1313
create(context) {
14-
const esLintRuleListener = esLintNoUnUsedExpressions.create(context)
14+
const esLintRuleListener = esLintNoUnusedExpressions.create(context)
1515
return {
1616
ExpressionStatement(node: ExpressionStatementWithParent) {
1717
if (isJsxNode(node.expression) && node.parent.type === 'Program') {
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
declare module 'eslint/lib/rules/no-unused-expressions' {
22
import { Rule } from 'eslint'
3-
const noUnUsedExpressions: Rule.RuleModule
4-
export = noUnUsedExpressions
3+
const esLintNoUnusedExpressions: Rule.RuleModule
4+
export = esLintNoUnusedExpressions
55
}
66

77
declare module 'eslint-plugin-react/lib/rules/no-unescaped-entities' {
88
import { Rule } from 'eslint'
9-
const reactNoUnEscapedEntities: Rule.RuleModule
10-
export = reactNoUnEscapedEntities
9+
const reactNoUnescapedEntities: Rule.RuleModule
10+
export = reactNoUnescapedEntities
1111
}

test/__snapshots__/fixtures.test.ts.snap

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,20 @@ exports[`fixtures should match all snapshots: no-jsx-html-comments.mdx 1`] = `
3232
3333
<header>Header{/** JSX HTML comment */}</header>
3434
<main>Main Content</main>
35+
36+
Inline <header>Header<!-- JSX HTML comment --></header>
37+
38+
<main>Main Content</main>
39+
"
40+
`;
41+
42+
exports[`fixtures should match all snapshots: no-unescaped-entities.mdx 1`] = `
43+
"<header> &apos; </header>
44+
<main> > </main>
45+
46+
Header <header> &apos; </header>
47+
Main <main> > </main>
3548
"
3649
`;
50+
51+
exports[`fixtures should match all snapshots: parser.mdx 1`] = `undefined`;

test/fixtures.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ describe('fixtures', () => {
1111
it('should match all snapshots', () => {
1212
cli
1313
.executeOnFiles(['test/fixtures/*.mdx'])
14-
.results.forEach(({ filePath, output }) =>
15-
expect(output).toMatchSnapshot(basename(filePath)),
14+
.results.forEach(({ filePath, output, source }) =>
15+
expect(output || source).toMatchSnapshot(basename(filePath)),
1616
)
1717
})
1818
})

0 commit comments

Comments
 (0)