8000 test(typescript-estree): add test cases for extraFileExtensions (#1405) · alex-dow/typescript-eslint@a2c53ae · GitHub
[go: up one dir, main page]

Skip to content

Commit a2c53ae

Browse files
armano2bradzacher
authored andcommitted
test(typescript-estree): add test cases for extraFileExtensions (typescript-eslint#1405)
1 parent 01caad2 commit a2c53ae

File tree

2 files changed

+291
-8
lines changed

2 files changed

+291
-8
lines changed

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

Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,32 @@ Object {
311311
}
312312
`;
313313

314+
exports[`parse() invalid file error messages "parserOptions.extraFileExtensions" is empty the extension does not match 1`] = `
315+
"\\"parserOptions.project\\" has been set for @typescript-eslint/parser.
316+
The file does not match your project config: tests/fixtures/invalidFileErrors/other/unknownFileType.unknown.
317+
The extension for the file (.unknown) is non-standard. You should add \\"parserOptions.extraFileExtensions\\" to your config."
318+
`;
319+
320+
exports[`parse() invalid file error messages "parserOptions.extraFileExtensions" is non-empty invalid extension 1`] = `
321+
"\\"parserOptions.project\\" has been set for @typescript-eslint/parser.
322+
The file does not match your project config: tests/fixtures/invalidFileErrors/other/unknownFileType.unknown.
323+
Found unexpected extension \\"unknown\\" specified with the \\"extraFileExtensions\\" option. Did you mean \\".unknown\\"?
324+
The extension for the file (.unknown) is non-standard. It should be added to your existing \\"parserOptions.extraFileExtensions\\"."
325+
`;
326+
314327
exports[`parse() invalid file error messages "parserOptions.extraFileExtensions" is non-empty the extension does not match 1`] = `
315328
"\\"parserOptions.project\\" has been set for @typescript-eslint/parser.
316329
The file does not match your project config: tests/fixtures/invalidFileErrors/other/unknownFileType.unknown.
317330
The extension for the file (.unknown) is non-standard. It should be added to your existing \\"parserOptions.extraFileExtensions\\"."
318331
`;
319332

333+
exports[`parse() invalid file error messages "parserOptions.extraFileExtensions" is non-empty the extension matches duplicate extension 1`] = `
334+
"\\"parserOptions.project\\" has been set for @typescript-eslint/parser.
335+
The file does not match your project config: tests/fixtures/invalidFileErrors/ts/notIncluded.ts.
336+
You unnecessarily included the extension \\".ts\\" with the \\"extraFileExtensions\\" option. This extension is already handled by the parser by default.
337+
The file must be included in at least one of the projects provided."
338+
`;
339+
320340
exports[`parse() invalid file error messages "parserOptions.extraFileExtensions" is non-empty the extension matches the file isn't included 1`] = `
321341
"\\"parserOptions.project\\" has been set for @typescript-eslint/parser.
322342
The file does not match your project config: tests/fixtures/invalidFileErrors/other/notIncluded.vue.
@@ -1283,6 +1303,224 @@ Object {
12831303
}
12841304
`;
12851305

1306+
exports[`parse() isolated parsing should parse .json file - without JSX content - parserOptions.jsx = false 1`] = `
1307+
Object {
1308+
"ast": Object {
1309+
"body": Array [
1310+
Object {
1311+
"expression": Object {
1312+
"loc": Object {
1313+
"end": Object {
1314+
"column": 10,
1315+
"line": 1,
1316+
},
1317+
"start": Object {
1318+
"column": 0,
1319+
"line": 1,
1320+
},
1321+
},
1322+
"properties": Array [
1323+
Object {
1324+
"computed": false,
1325+
"key": Object {
1326+
"loc": Object {
1327+
"end": Object {
1328+
"column": 5,
1329+
"line": 1,
1330+
},
1331+
"start": Object {
1332+
"column": 2,
1333+
"line": 1,
1334+
},
1335+
},
1336+
"range": Array [
1337+
2,
1338+
5,
1339+
],
1340+
"raw": "\\"x\\"",
1341+
"type": "Literal",
1342+
"value": "x",
1343+
},
1344+
"kind": "init",
1345+
"loc": Object {
1346+
"end": Object {
1347+
"column": 8,
1348+
"line": 1,
1349+
},
1350+
"start": Object {
1351+
"column": 2,
1352+
"line": 1,
1353+
},
1354+
},
1355+
"method": false,
1356+
"range": Array [
1357+
2,
1358+
8,
1359+
],
1360+
"shorthand": false,
1361+
"type": "Property",
1362+
"value": Object {
1363+
"loc": Object {
1364+
"end": Object {
1365+
"column": 8,
1366+
"line": 1,
1367+
},
1368+
"start": Object {
1369+
"column": 7,
1370+
"line": 1,
1371+
},
1372+
},
1373+
"range": Array [
1374+
7,
1375+
8,
1376+
],
1377+
"raw": "1",
1378+
"type": "Literal",
1379+
"value": 1,
1380+
},
1381+
},
1382+
],
1383+
"range": Array [
1384+
0,
1385+
10,
1386+
],
1387+
"type": "ObjectExpression",
1388+
},
1389+
"loc": Object {
1390+
"end": Object {
1391+
"column": 10,
1392+
"line": 1,
1393+
},
1394+
"start": Object {
1395+
"column": 0,
1396+
"line": 1,
1397+
},
1398+
},
1399+
"range": Array [
1400+
0,
1401+
10,
1402+
],
1403+
"type": "ExpressionStatement",
1404+
},
1405+
],
1406+
"comments": Array [],
1407+
"loc": Object {
1408+
"end": Object {
1409+
"column": 10,
1410+
"line": 1,
1411+
},
1412+
"start": Object {
1413+
"column": 0,
1414+
"line": 1,
1415+
},
1416+
},
1417+
"range": Array [
1418+
0,
1419+
10,
1420+
],
1421+
"sourceType": "script",
1422+
"tokens": Array [
1423+
Object {
1424+
"loc": Object {
1425+
"end": Object {
1426+
"column": 1,
1427+
"line": 1,
1428+
},
1429+
"start": Object {
1430+
"column": 0,
1431+
"line": 1,
1432+
},
1433+
},
1434+
"range": Array [
1435+
0,
1436+
1,
1437+
],
1438+
"type": "Punctuator",
1439+
"value": "{",
1440+
},
1441+
Object {
1442+
"loc": Object {
1443+
"end": Object {
1444+
"column": 5,
1445+
"line": 1,
1446+
},
1447+
"start": Object {
1448+
"column": 2,
1449+
"line": 1,
1450+
},
1451+
},
1452+
"range": Array [
1453+
2,
1454+
5,
1455+
],
1456+
"type": "String",
1457+
"value": "\\"x\\"",
1458+
},
1459+
Object {
1460+
"loc": Object {
1461+
"end": Object {
1462+
"column": 6,
1463+
"line": 1,
1464+
},
1465+
"start": Object {
1466+
"column": 5,
1467+
"line": 1,
1468+
},
1469+
},
1470+
"range": Array [
1471+
5,
1472+
6,
1473+
],
1474+
"type": "Punctuator",
1475+
"value": ":",
1476+
},
1477+
Object {
1478+
"loc": Object {
1479+
"end": Object {
1480+
"column": 8,
1481+
"line": 1,
1482+
},
1483+
"start": Object {
1484+
"column": 7,
1485+
"line": 1,
1486+
},
1487+
},
1488+
"range": Array [
1489+
7,
1490+
8,
1491+
],
1492+
"type": "Numeric",
1493+
"value": "1",
1494+
},
1495+
Object {
1496+
"loc": Object {
1497+
"end": Object {
1498+
"column": 10,
1499+
"line": 1,
1500+
},
1501+
"start": Object {
1502+
"column": 9,
1503+
"line": 1,
1504+
},
1505+
},
1506+
"range": Array [
1507+
9,
1508+
10,
1509+
],
1510+
"type": "Punctuator",
1511+
"value": "}",
1512+
},
1513+
],
1514+
"type": "Program",
1515+
},
1516+
"services": Object {
1517+
"esTreeNodeToTSNodeMap": undefined,
1518+
"program": undefined,
1519+
"tsNodeToESTreeNodeMap": undefined,
1520+
},
1521+
}
1522+
`;
1523+
12861524
exports[`parse() isolated parsing should parse .jsx file - with JSX content - parserOptions.jsx = false 1`] = `
12871525
Object {
12881526
"ast": Object {

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

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,17 @@ describe('parse()', () => {
258258
jsxSetting,
259259
shouldThrow = false,
260260
}: {
261-
ext: '.js' | '.jsx' | '.ts' | '.tsx' | '.vue';
261+
ext: '.js' | '.jsx' | '.ts' | '.tsx' | '.vue' | '.json';
262262
jsxContent: boolean;
263263
jsxSetting: boolean;
264264
shouldThrow?: boolean;
265265
}): void => {
266-
const code = jsxContent ? 'const x = <div />;' : 'const x = 1';
266+
const code =
267+
ext === '.json'
268+
? '{ "x": 1 }'
269+
: jsxContent
270+
? 'const x = <div />;'
271+
: 'const x = 1';
267272
it(`should parse ${ext} file - ${
268273
jsxContent ? 'with' : 'without'
269274
} JSX content - parserOptions.jsx = ${jsxSetting}`, () => {
@@ -394,6 +399,11 @@ describe('parse()', () => {
394399
jsxContent: true,
395400
jsxSetting: true,
396401
});
402+
testParse({
403+
ext: '.json',
404+
jsxContent: false,
405+
jsxSetting: false,
406+
});
397407
});
398408

399409
describe('invalid file error messages', () => {
@@ -406,13 +416,24 @@ describe('parse()', () => {
406416
loc: true,
407417
tsconfigRootDir: PROJECT_DIR,
408418
project: './tsconfig.json',
409-
extraFileExtensions: ['.vue'],
410419
};
411-
const testParse = (filePath: string) => (): void => {
412-
parser.parseAndGenerateServices(code, {
413-
...config,
414-
filePath: join(PROJECT_DIR, filePath),
415-
});
420+
const testParse = (
421+
filePath: string,
422+
extraFileExtensions: string[] = ['.vue'],
423+
) => (): void => {
424+
try {
425+
parser.parseAndGenerateServices(code, {
426+
...config,
427+
extraFileExtensions,
428+
filePath: join(PROJECT_DIR, filePath),
429+
});
430+
} catch (error) {
431+
/**
432+
* Aligns paths between environments, node for windows uses `\`, for linux and mac uses `/`
433+
*/
434+
error.message = error.message.replace(/\\(?!["])/gm, '/');
435+
throw error;
436+
}
416437
};
417438

418439
describe('project includes', () => {
@@ -431,6 +452,18 @@ describe('parse()', () => {
431452
});
432453
});
433454

455+
describe('"parserOptions.extraFileExtensions" is empty', () => {
456+
it('should not error', () => {
457+
expect(testParse('ts/included.ts', [])).not.toThrow();
458+
});
459+
460+
it('the extension does not match', () => {
461+
expect(
462+
testParse('other/unknownFileType.unknown', []),
463+
).toThrowErrorMatchingSnapshot();
464+
});
465+
});
466+
434467
describe('"parserOptions.extraFileExtensions" is non-empty', () => {
435468
describe('the extension matches', () => {
436469
it('the file is included', () => {
@@ -442,6 +475,18 @@ describe('parse()', () => {
442475
testParse('other/notIncluded.vue'),
443476
).toThrowErrorMatchingSnapshot();
444477
});
478+
479+
it('duplicate extension', () => {
480+
expect(
481+
testParse('ts/notIncluded.ts', ['.ts']),
482+
).toThrowErrorMatchingSnapshot();
483+
});
484+
});
485+
486+
it('invalid extension', () => {
487+
expect(
488+
testParse('other/unknownFileType.unknown', ['unknown']),
489+
).toThrowErrorMatchingSnapshot();
445490
});
446491

447492
it('the extension does not match', () => {

0 commit comments

Comments
 (0)
0