-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
parser.test.ts
127 lines (117 loc) · 4.52 KB
/
parser.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import fs from 'node:fs/promises'
import path from 'node:path'
import { DEFAULT_PARSER_OPTIONS } from './helpers'
import { parser } from 'eslint-mdx'
describe('parser', () => {
it('should throw on incorrect extension', () => {
expect(() =>
parser.parse("import A from 'a'\n", {
...DEFAULT_PARSER_OPTIONS,
filePath: '__placeholder__.js',
}),
).toThrowErrorMatchingInlineSnapshot(
`"Unsupported file extension, make sure setting the \`extensions\` or \`markdownExtensions\` option correctly."`,
)
})
it('should throw on invalid es syntaxes', () => {
expect(() =>
parser.parse(
"import A from 'a'\nimport A from 'a'",
DEFAULT_PARSER_OPTIONS,
),
).toThrowErrorMatchingInlineSnapshot(`
"Could not parse import/exports with acorn
Cause: Could not parse import/exports with acorn
Cause: Identifier 'A' has already been declared"
`)
expect(() => parser.parse('<header><>\n</header>', DEFAULT_PARSER_OPTIONS))
.toThrowErrorMatchingInlineSnapshot(`
"Unexpected closing tag \`</header>\`, expected corresponding closing tag for \`<>\` (1:9-1:11)
Cause: Unexpected closing tag \`</header>\`, expected corresponding closing tag for \`<>\` (1:9-1:11)"
`)
expect(() => parser.parse('<h1></h2>', DEFAULT_PARSER_OPTIONS))
.toThrowErrorMatchingInlineSnapshot(`
"Unexpected closing tag \`</h2>\`, expected corresponding closing tag for \`<h1>\` (1:1-1:5)
Cause: Unexpected closing tag \`</h2>\`, expected corresponding closing tag for \`<h1>\` (1:1-1:5)"
`)
expect(() =>
parser.parse('Header\n<>', {
parser: '@typescript-eslint/parser',
}),
).toThrowErrorMatchingInlineSnapshot(
`"The "path" argument must be of type string. Received undefined"`,
)
expect(() =>
parser.parse('Header\n<>', {
parser: '@babel/eslint-parser',
}),
).toThrowErrorMatchingInlineSnapshot(
`"The "path" argument must be of type string. Received undefined"`,
)
expect(() => parser.parse('<main><</main>', DEFAULT_PARSER_OPTIONS))
.toThrowErrorMatchingInlineSnapshot(`
"Unexpected character \`<\` (U+003C) before name, expected a character that can start a name, such as a letter, \`$\`, or \`_\`
Cause: Unexpected character \`<\` (U+003C) before name, expected a character that can start a name, such as a letter, \`$\`, or \`_\`"
`)
expect(() => parser.parse('<main>{<}</main>', DEFAULT_PARSER_OPTIONS))
.toThrowErrorMatchingInlineSnapshot(`
"Could not parse expression with acorn
Cause: Could not parse expression with acorn
Cause: Unexpected token"
`)
expect(() =>
parser.parse(
'<main>\n<section><</section></main>',
DEFAULT_PARSER_OPTIONS,
),
).toThrowErrorMatchingInlineSnapshot(`
"Unexpected character \`<\` (U+003C) before name, expected a character that can start a name, such as a letter, \`$\`, or \`_\`
Cause: Unexpected character \`<\` (U+003C) before name, expected a character that can start a name, such as a letter, \`$\`, or \`_\`"
`)
})
it('should not throw on adjacent JSX nodes', () =>
expect(() =>
parser.parse(
'<header></header>\n<main><section>left</section><section>right<input name="name"/></section></main>',
DEFAULT_PARSER_OPTIONS,
),
).not.toThrow())
it('should not throw on JSX with blank lines', () =>
expect(() =>
parser.parse('<header>\n\nTitle\n\n</header>', DEFAULT_PARSER_OPTIONS),
).not.toThrow())
it('should work with plain markdown file', () =>
expect(() =>
parser.parse('<img><br>', {
filePath: 'test.md',
}),
).not.toThrow())
it('should parse jsx spread correctly', () =>
expect(
parser.parse('<div {...{}}></div>', {
filePath: 'test.mdx',
}),
).toMatchSnapshot())
it('should match all AST snapshots', async () => {
const dirents = await fs.readdir(path.join(__dirname, 'fixtures'), {
withFileTypes: true,
})
for (const dirent of dirents) {
if (dirent.isDirectory()) {
continue
}
const fileName = dirent.name
const filePath = path.resolve(__dirname, 'fixtures', fileName)
try {
expect(
parser.parse(await fs.readFile(filePath, 'utf8'), {
filePath,
}),
).toMatchSnapshot(fileName)
} catch (err) {
// eslint-disable-next-line jest/no-conditional-expect
expect((err as Error).message).toMatchSnapshot(fileName)
}
}
})
})