|
1 |
| -import { TSESLint } from '@typescript-eslint/experimental-utils'; |
2 |
| -import * as parser from '../../src/parser'; |
| 1 | +import { parseForESLint } from '../../src/parser'; |
| 2 | +import { serializer } from '../tools/ts-error-serializer'; |
| 3 | +import type { ParserOptions } from '@typescript-eslint/types'; |
3 | 4 |
|
4 | 5 | //------------------------------------------------------------------------------
|
5 | 6 | // Tests
|
6 | 7 | //------------------------------------------------------------------------------
|
7 | 8 |
|
| 9 | +expect.addSnapshotSerializer(serializer); |
| 10 | + |
| 11 | +function parseWithError(code: string, options?: ParserOptions | null): unknown { |
| 12 | + try { |
| 13 | + return parseForESLint(code, options); |
| 14 | + } catch (e) { |
| 15 | + return e; |
| 16 | + } |
| 17 | +} |
| 18 | + |
8 | 19 | describe('TSX', () => {
|
9 | 20 | describe("if the filename ends with '.tsx', enable jsx option automatically.", () => {
|
10 |
| - const linter = new TSESLint.Linter(); |
11 |
| - linter.defineParser('@typescript-eslint/parser', parser); |
12 |
| - |
13 | 21 | it('filePath was not provided', () => {
|
14 | 22 | const code = 'const element = <T/>';
|
15 |
| - const config = { |
16 |
| - parser: '@typescript-eslint/parser', |
17 |
| - }; |
18 |
| - const messages = linter.verify(code, config); |
19 | 23 |
|
20 |
| - expect(messages).toStrictEqual([ |
21 |
| - { |
22 |
| - column: 18, |
23 |
| - fatal: true, |
24 |
| - line: 1, |
25 |
| - message: "Parsing error: '>' expected.", |
26 |
| - ruleId: null, |
27 |
| - severity: 2, |
28 |
| - }, |
29 |
| - ]); |
| 24 | + expect(parseWithError(code)).toMatchInlineSnapshot(` |
| 25 | + TSError { |
| 26 | + "column": 18, |
| 27 | + "index": 18, |
| 28 | + "lineNumber": 1, |
| 29 | + "message": "'>' expected.", |
| 30 | + } |
| 31 | + `); |
30 | 32 | });
|
31 | 33 |
|
32 | 34 | it("filePath was not provided and 'jsx:true' option", () => {
|
33 | 35 | const code = 'const element = <T/>';
|
34 |
| - const config = { |
35 |
| - parser: '@typescript-eslint/parser', |
36 |
| - parserOptions: { |
| 36 | + expect(() => |
| 37 | + parseForESLint(code, { |
37 | 38 | ecmaFeatures: {
|
38 | 39 | jsx: true,
|
39 | 40 | },
|
40 |
| - }, |
41 |
| - }; |
42 |
| - const messages = linter.verify(code, config); |
43 |
| - |
44 |
| - expect(messages).toStrictEqual([]); |
| 41 | + }), |
| 42 | + ).not.toThrow(); |
45 | 43 | });
|
46 | 44 |
|
47 | 45 | it('test.ts', () => {
|
48 | 46 | const code = 'const element = <T/>';
|
49 |
| - const config = { |
50 |
| - parser: '@typescript-eslint/parser', |
51 |
| - }; |
52 |
| - const messages = linter.verify(code, config, { filename: 'test.ts' }); |
53 |
| - |
54 |
| - expect(messages).toStrictEqual([ |
55 |
| - { |
56 |
| - column: 18, |
57 |
| - fatal: true, |
58 |
| - line: 1, |
59 |
| - message: "Parsing error: '>' expected.", |
60 |
| - ruleId: null, |
61 |
| - severity: 2, |
62 |
| - }, |
63 |
| - ]); |
| 47 | + expect( |
| 48 | + parseWithError(code, { |
| 49 | + filePath: 'test.ts', |
| 50 | + }), |
| 51 | + ).toMatchInlineSnapshot(` |
| 52 | + TSError { |
| 53 | + "column": 18, |
| 54 | + "index": 18, |
| 55 | + "lineNumber": 1, |
| 56 | + "message": "'>' expected.", |
| 57 | + } |
| 58 | + `); |
64 | 59 | });
|
65 | 60 |
|
66 | 61 | it("test.ts with 'jsx:true' option", () => {
|
67 | 62 | const code = 'const element = <T/>';
|
68 |
| - const config = { |
69 |
| - parser: '@typescript-eslint/parser', |
70 |
| - parserOptions: { |
| 63 | + |
| 64 | + expect( |
| 65 | + parseWithError(code, { |
| 66 | + filePath: 'test.ts', |
71 | 67 | ecmaFeatures: {
|
72 | 68 | jsx: true,
|
73 | 69 | },
|
74 |
| - }, |
75 |
| - }; |
76 |
| - const messages = linter.verify(code, config, { filename: 'test.ts' }); |
77 |
| - |
78 |
| - expect(messages).toStrictEqual([ |
79 |
| - { |
80 |
| - column: 18, |
81 |
| - fatal: true, |
82 |
| - line: 1, |
83 |
| - message: "Parsing error: '>' expected.", |
84 |
| - ruleId: null, |
85 |
| - severity: 2, |
86 |
| - }, |
87 |
| - ]); |
| 70 | + }), |
| 71 | + ).toMatchInlineSnapshot(` |
| 72 | + TSError { |
| 73 | + "column": 18, |
| 74 | + "index": 18, |
| 75 | + "lineNumber": 1, |
| 76 | + "message": "'>' expected.", |
| 77 | + } |
| 78 | + `); |
88 | 79 | });
|
89 | 80 |
|
90 | 81 | it('test.tsx', () => {
|
91 | 82 | const code = 'const element = <T/>';
|
92 |
| - const config = { |
93 |
| - parser: '@typescript-eslint/parser', |
94 |
| - }; |
95 |
| - const messages = linter.verify(code, config, { filename: 'test.tsx' }); |
96 |
| - |
97 |
| - expect(messages).toStrictEqual([]); |
| 83 | + expect(() => |
| 84 | + parseForESLint(code, { |
| 85 | + filePath: 'test.tsx', |
| 86 | + }), |
| 87 | + ).not.toThrow(); |
98 | 88 | });
|
99 | 89 |
|
100 | 90 | it("test.tsx with 'jsx:false' option", () => {
|
101 | 91 | const code = 'const element = <T/>';
|
102 |
| - const config = { |
103 |
| - parser: '@typescript-eslint/parser', |
104 |
| - parserOptions: { |
| 92 | + expect(() => |
| 93 | + parseForESLint(code, { |
105 | 94 | ecmaFeatures: {
|
106 | 95 | jsx: false,
|
107 | 96 | },
|
108 |
| - }, |
109 |
| - }; |
110 |
| - const messages = linter.verify(code, config, { filename: 'test.tsx' }); |
111 |
| - |
112 |
| - expect(messages).toStrictEqual([]); |
| 97 | + filePath: 'test.tsx', |
| 98 | + }), |
| 99 | + ).not.toThrow(); |
113 | 100 | });
|
114 | 101 | });
|
115 | 102 | });
|
0 commit comments