Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fdac435

Browse files
armano2JamesHenry
authored andcommitted
test(parser): add missing test cases for parser options (typescript-eslint#213)
1 parent c1abae9 commit fdac435

File tree

4 files changed

+327
-1
lines changed

4 files changed

+327
-1
lines changed

packages/typescript-estree/src/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ function getASTAndDefaultProject(code: string, options: ParserOptions) {
107107
function createNewProgram(code: string) {
108108
const FILENAME = getFileName(extra);
109109

110-
const compilerHost = {
110+
const compilerHost: ts.CompilerHost = {
111111
fileExists() {
112112
return true;
113113
},

packages/typescript-estree/tests/lib/__snapshots__/convert.ts.snap

Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,179 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`convert deeplyCopy should convert array of nodes 1`] = `
4+
Object {
5+
"amdDependencies": Array [],
6+
"bindDiagnostics": Array [],
7+
"bindSuggestionDiagnostics": undefined,
8+
"checkJsDirective": undefined,
9+
"endOfFileToken": Object {
10+
"loc": Object {
11+
"end": Object {
12+
"column": 12,
13+
"line": 1,
14+
},
15+
"start": Object {
16+
"column": 12,
17+
"line": 1,
18+
},
19+
},
20+
"range": Array [
21+
12,
22+
12,
23+
],
24+
"type": "TSEndOfFileToken",
25+
},
26+
"externalModuleIndicator": undefined,
27+
"fileName": "text.ts",
28+
"hasNoDefaultLib": false,
29+
"identifierCount": 2,
30+
"identifiers": Map {
31+
"foo" => "foo",
32+
"T" => "T",
33+
},
34+
"isDeclarationFile": false,
35+
"languageVariant": 1,
36+
"languageVersion": 6,
37+
"libReferenceDirectives": Array [],
38+
"lineMap": Array [
39+
null,
40+
],
41+
"loc": Object {
42+
"end": Object {
43+
"column": 12,
44+
"line": 1,
45+
},
46+
"start": Object {
47+
"column": 0,
48+
"line": 1,
49+
},
50+
},
51+
"nodeCount": 7,
52+
"parseDiagnostics": Array [],
53+
"pragmas": Map {},
54+
"range": Array [
55+
0,
56+
12,
57+
],
58+
"referencedFiles": Array [],
59+
"scriptKind": 4,
60+
"statements": Array [
61+
Object {
62+
"expression": Object {
63+
"arguments": Array [],
64+
"callee": Object {
65+
"loc": Object {
66+
"end": Object {
67+
"column": 7,
68+
"line": 1,
69+
},
70+
"start": Object {
71+
"column": 4,
72+
"line": 1,
73+
},
74+
},
75+
"name": "foo",
76+
"range": Array [
77+
4,
78+
7,
79+
],
80+
"type": "Identifier",
81+
},
82+
"loc": Object {
83+
"end": Object {
84+
"column": 12,
85+
"line": 1,
86+
},
87+
"start": Object {
88+
"column": 0,
89+
"line": 1,
90+
},
91+
},
92+
"range": Array [
93+
0,
94+
12,
95+
],
96+
"type": "NewExpression",
97+
"typeParameters": Object {
98+
"loc": Object {
99+
"end": Object {
100+
"column": 10,
101+
"line": 1,
102+
},
103+
"start": Object {
104+
"column": 7,
105+
"line": 1,
106+
},
107+
},
108+
"params": Array [
109+
Object {
110+
"loc": Object {
111+
"end": Object {
112+
"column": 9,
113+
"line": 1,
114+
},
115+
"start": Object {
116+
"column": 8,
117+
"line": 1,
118+
},
119+
},
120+
"range": Array [
121+
8,
122+
9,
123+
],
124+
"type": "TSTypeReference",
125+
"typeName": Object {
126+
"loc": Object {
127+
"end": Object {
128+
"column": 9,
129+
"line": 1,
130+
},
131+
"start": Object {
132+
"column": 8,
133+
"line": 1,
134+
},
135+
},
136+
"name": "T",
137+
"range": Array [
138+
8,
139+
9,
140+
],
141+
"type": "Identifier",
142+
},
143+
"typeParameters": undefined,
144+
},
145+
],
146+
"range": Array [
147+
7,
148+
10,
149+
],
150+
"type": "TSTypeParameterInstantiation",
151+
},
152+
},
153+
"loc": Object {
154+
"end": Object {
155+
"column": 12,
156+
"line": 1,
157+
},
158+
"start": Object {
159+
"column": 0,
160+
"line": 1,
161+
},
162+
},
163+
"range": Array [
164+
0,
165+
12,
166+
],
167+
"type": "ExpressionStatement",
168+
},
169+
],
170+
"text": "new foo<T>()",
171+
"transformFlags": undefined,
172+
"type": "TSSourceFile",
173+
"typeReferenceDirectives": Array [],
174+
}
175+
`;
176+
3177
exports[`convert deeplyCopy should convert node correctly 1`] = `
4178
Object {
5179
"body": Array [
@@ -421,6 +595,101 @@ Object {
421595
}
422596
`;
423597

598+
exports[`convert deeplyCopy should convert node with type arguments correctly 1`] = `
599+
Object {
600+
"arguments": Array [],
601+
"expression": Object {
602+
"loc": Object {
603+
"end": Object {
604+
"column": 7,
605+
"line": 1,
606+
},
607+
"start": Object {
608+
"column": 4,
609+
"line": 1,
610+
},
611+
},
612+
"name": "foo",
613+
"range": Array [
614+
4,
615+
7,
616+
],
617+
"type": "Identifier",
618+
},
619+
"loc": Object {
620+
"end": Object {
621+
"column": 12,
622+
"line": 1,
623+
},
624+
"start": Object {
625+
"column": 0,
626+
"line": 1,
627+
},
628+
},
629+
"range": Array [
630+
0,
631+
12,
632+
],
633+
"transformFlags": undefined,
634+
"type": "TSNewExpression",
635+
"typeParameters": Object {
636+
"loc": Object {
637+
"end": Object {
638+
"column": 10,
639+
"line": 1,
640+
},
641+
"start": Object {
642+
"column": 7,
643+
"line": 1,
644+
},
645+
},
646+
"params": Array [
647+
Object {
648+
"loc": Object {
649+
"end": Object {
650+
"column": 9,
651+
"line": 1,
652+
},
653+
"start": Object {
654+
"column": 8,
655+
"line": 1,
656+
},
657+
},
658+
"range": Array [
659+
8,
660+
9,
661+
],
662+
"type": "TSTypeReference",
663+
"typeName": Object {
664+
"loc": Object {
665+
"end": Object {
666+
"column": 9,
667+
"line": 1,
668+
},
669+
"start": Object {
670+
"column": 8,
671+
"line": 1,
672+
},
673+
},
674+
"name": "T",
675+
"range": Array [
676+
8,
677+
9,
678+
],
679+
"type": "Identifier",
680+
},
681+
"typeParameters": undefined,
682+
},
683+
],
684+
"range": Array [
685+
7,
686+
10,
687+
],
688+
"type": "TSTypeParameterInstantiation",
689+
},
690+
}
691+
`;
692+
424693
exports[`convert deeplyCopy should convert node with type parameters correctly 1`] = `
425694
Object {
426695
"heritageClauses": undefined,

packages/typescript-estree/tests/lib/convert.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,30 @@ describe('convert', () => {
4545
expect((instance as any).deeplyCopy(ast.statements[0])).toMatchSnapshot();
4646
});
4747

48+
it('deeplyCopy should convert node with type arguments correctly', () => {
49+
const ast = convertCode('new foo<T>()');
50+
51+
const instance = new Converter(ast, {
52+
errorOnUnknownASTType: false,
53+
useJSXTextNode: false,
54+
shouldProvideParserServices: false
55+
});
56+
expect(
57+
(instance as any).deeplyCopy((ast.statements[0] as any).expression)
58+
).toMatchSnapshot();
59+
});
60+
61+
it('deeplyCopy should convert array of nodes', () => {
62+
const ast = convertCode('new foo<T>()');
63+
64+
const instance = new Converter(ast, {
65+
errorOnUnknownASTType: false,
66+
useJSXTextNode: false,
67+
shouldProvideParserServices: false
68+
});
69+
expect((instance as any).deeplyCopy(ast)).toMatchSnapshot();
70+
});
71+
4872
it('deeplyCopy should fail on unknown node', () => {
4973
const ast = convertCode('type foo = ?foo<T> | ?(() => void)?');
5074

packages/typescript-estree/tests/lib/parse.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,37 @@ describe('parse()', () => {
104104
);
105105
});
106106
});
107+
108+
describe('errorOnTypeScriptSyntacticAndSemanticIssues', () => {
109+
const code = '@test const foo = 2';
110+
const options: ParserOptions = {
111+
comment: true,
112+
tokens: true,
113+
range: true,
114+
loc: true,
115+
errorOnTypeScriptSyntacticAndSemanticIssues: true
116+
};
117+
118+
it('should throw on invalid option when used in parse', () => {
119+
expect(() => {
120+
parser.parse(code, options);
121+
}).toThrow(
122+
`"errorOnTypeScriptSyntacticAndSemanticIssues" is only supported for parseAndGenerateServices()`
123+
);
124+
});
125+
126+
it('should not throw when used in parseAndGenerateServices', () => {
127+
expect(() => {
128+
parser.parseAndGenerateServices(code, options);
129+
}).not.toThrow(
130+
`"errorOnTypeScriptSyntacticAndSemanticIssues" is only supported for parseAndGenerateServices()`
131+
);
132+
});
133+
134+
it('should error on invalid code', () => {
135+
expect(() => {
136+
parser.parseAndGenerateServices(code, options);
137+
}).toThrow('Decorators are not valid here.');
138+
});
139+
});
107140
});

0 commit comments

Comments
 (0)