From a2c53ae5c50ac9c0064ce2928ebeb20fcf586b6e Mon Sep 17 00:00:00 2001 From: Armano Date: Mon, 6 Jan 2020 06:50:22 +0100 Subject: [PATCH] test(typescript-estree): add test cases for extraFileExtensions (#1405) --- .../tests/lib/__snapshots__/parse.ts.snap | 238 ++++++++++++++++++ packages/typescript-estree/tests/lib/parse.ts | 61 ++++- 2 files changed, 291 insertions(+), 8 deletions(-) diff --git a/packages/typescript-estree/tests/lib/__snapshots__/parse.ts.snap b/packages/typescript-estree/tests/lib/__snapshots__/parse.ts.snap index c09d5159cd4..616c95d748e 100644 --- a/packages/typescript-estree/tests/lib/__snapshots__/parse.ts.snap +++ b/packages/typescript-estree/tests/lib/__snapshots__/parse.ts.snap @@ -311,12 +311,32 @@ Object { } `; +exports[`parse() invalid file error messages "parserOptions.extraFileExtensions" is empty the extension does not match 1`] = ` +"\\"parserOptions.project\\" has been set for @typescript-eslint/parser. +The file does not match your project config: tests/fixtures/invalidFileErrors/other/unknownFileType.unknown. +The extension for the file (.unknown) is non-standard. You should add \\"parserOptions.extraFileExtensions\\" to your config." +`; + +exports[`parse() invalid file error messages "parserOptions.extraFileExtensions" is non-empty invalid extension 1`] = ` +"\\"parserOptions.project\\" has been set for @typescript-eslint/parser. +The file does not match your project config: tests/fixtures/invalidFileErrors/other/unknownFileType.unknown. +Found unexpected extension \\"unknown\\" specified with the \\"extraFileExtensions\\" option. Did you mean \\".unknown\\"? +The extension for the file (.unknown) is non-standard. It should be added to your existing \\"parserOptions.extraFileExtensions\\"." +`; + exports[`parse() invalid file error messages "parserOptions.extraFileExtensions" is non-empty the extension does not match 1`] = ` "\\"parserOptions.project\\" has been set for @typescript-eslint/parser. The file does not match your project config: tests/fixtures/invalidFileErrors/other/unknownFileType.unknown. The extension for the file (.unknown) is non-standard. It should be added to your existing \\"parserOptions.extraFileExtensions\\"." `; +exports[`parse() invalid file error messages "parserOptions.extraFileExtensions" is non-empty the extension matches duplicate extension 1`] = ` +"\\"parserOptions.project\\" has been set for @typescript-eslint/parser. +The file does not match your project config: tests/fixtures/invalidFileErrors/ts/notIncluded.ts. +You unnecessarily included the extension \\".ts\\" with the \\"extraFileExtensions\\" option. This extension is already handled by the parser by default. +The file must be included in at least one of the projects provided." +`; + exports[`parse() invalid file error messages "parserOptions.extraFileExtensions" is non-empty the extension matches the file isn't included 1`] = ` "\\"parserOptions.project\\" has been set for @typescript-eslint/parser. The file does not match your project config: tests/fixtures/invalidFileErrors/other/notIncluded.vue. @@ -1283,6 +1303,224 @@ Object { } `; +exports[`parse() isolated parsing should parse .json file - without JSX content - parserOptions.jsx = false 1`] = ` +Object { + "ast": Object { + "body": Array [ + Object { + "expression": Object { + "loc": Object { + "end": Object { + "column": 10, + "line": 1, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "properties": Array [ + Object { + "computed": false, + "key": Object { + "loc": Object { + "end": Object { + "column": 5, + "line": 1, + }, + "start": Object { + "column": 2, + "line": 1, + }, + }, + "range": Array [ + 2, + 5, + ], + "raw": "\\"x\\"", + "type": "Literal", + "value": "x", + }, + "kind": "init", + "loc": Object { + "end": Object { + "column": 8, + "line": 1, + }, + "start": Object { + "column": 2, + "line": 1, + }, + }, + "method": false, + "range": Array [ + 2, + 8, + ], + "shorthand": false, + "type": "Property", + "value": Object { + "loc": Object { + "end": Object { + "column": 8, + "line": 1, + }, + "start": Object { + "column": 7, + "line": 1, + }, + }, + "range": Array [ + 7, + 8, + ], + "raw": "1", + "type": "Literal", + "value": 1, + }, + }, + ], + "range": Array [ + 0, + 10, + ], + "type": "ObjectExpression", + }, + "loc": Object { + "end": Object { + "column": 10, + "line": 1, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "range": Array [ + 0, + 10, + ], + "type": "ExpressionStatement", + }, + ], + "comments": Array [], + "loc": Object { + "end": Object { + "column": 10, + "line": 1, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "range": Array [ + 0, + 10, + ], + "sourceType": "script", + "tokens": Array [ + Object { + "loc": Object { + "end": Object { + "column": 1, + "line": 1, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "range": Array [ + 0, + 1, + ], + "type": "Punctuator", + "value": "{", + }, + Object { + "loc": Object { + "end": Object { + "column": 5, + "line": 1, + }, + "start": Object { + "column": 2, + "line": 1, + }, + }, + "range": Array [ + 2, + 5, + ], + "type": "String", + "value": "\\"x\\"", + }, + Object { + "loc": Object { + "end": Object { + "column": 6, + "line": 1, + }, + "start": Object { + "column": 5, + "line": 1, + }, + }, + "range": Array [ + 5, + 6, + ], + "type": "Punctuator", + "value": ":", + }, + Object { + "loc": Object { + "end": Object { + "column": 8, + "line": 1, + }, + "start": Object { + "column": 7, + "line": 1, + }, + }, + "range": Array [ + 7, + 8, + ], + "type": "Numeric", + "value": "1", + }, + Object { + "loc": Object { + "end": Object { + "column": 10, + "line": 1, + }, + "start": Object { + "column": 9, + "line": 1, + }, + }, + "range": Array [ + 9, + 10, + ], + "type": "Punctuator", + "value": "}", + }, + ], + "type": "Program", + }, + "services": Object { + "esTreeNodeToTSNodeMap": undefined, + "program": undefined, + "tsNodeToESTreeNodeMap": undefined, + }, +} +`; + exports[`parse() isolated parsing should parse .jsx file - with JSX content - parserOptions.jsx = false 1`] = ` Object { "ast": Object { diff --git a/packages/typescript-estree/tests/lib/parse.ts b/packages/typescript-estree/tests/lib/parse.ts index 9f469f4405d..6cba9ba867b 100644 --- a/packages/typescript-estree/tests/lib/parse.ts +++ b/packages/typescript-estree/tests/lib/parse.ts @@ -258,12 +258,17 @@ describe('parse()', () => { jsxSetting, shouldThrow = false, }: { - ext: '.js' | '.jsx' | '.ts' | '.tsx' | '.vue'; + ext: '.js' | '.jsx' | '.ts' | '.tsx' | '.vue' | '.json'; jsxContent: boolean; jsxSetting: boolean; shouldThrow?: boolean; }): void => { - const code = jsxContent ? 'const x =
;' : 'const x = 1'; + const code = + ext === '.json' + ? '{ "x": 1 }' + : jsxContent + ? 'const x =
;' + : 'const x = 1'; it(`should parse ${ext} file - ${ jsxContent ? 'with' : 'without' } JSX content - parserOptions.jsx = ${jsxSetting}`, () => { @@ -394,6 +399,11 @@ describe('parse()', () => { jsxContent: true, jsxSetting: true, }); + testParse({ + ext: '.json', + jsxContent: false, + jsxSetting: false, + }); }); describe('invalid file error messages', () => { @@ -406,13 +416,24 @@ describe('parse()', () => { loc: true, tsconfigRootDir: PROJECT_DIR, project: './tsconfig.json', - extraFileExtensions: ['.vue'], }; - const testParse = (filePath: string) => (): void => { - parser.parseAndGenerateServices(code, { - ...config, - filePath: join(PROJECT_DIR, filePath), - }); + const testParse = ( + filePath: string, + extraFileExtensions: string[] = ['.vue'], + ) => (): void => { + try { + parser.parseAndGenerateServices(code, { + ...config, + extraFileExtensions, + filePath: join(PROJECT_DIR, filePath), + }); + } catch (error) { + /** + * Aligns paths between environments, node for windows uses `\`, for linux and mac uses `/` + */ + error.message = error.message.replace(/\\(?!["])/gm, '/'); + throw error; + } }; describe('project includes', () => { @@ -431,6 +452,18 @@ describe('parse()', () => { }); }); + describe('"parserOptions.extraFileExtensions" is empty', () => { + it('should not error', () => { + expect(testParse('ts/included.ts', [])).not.toThrow(); + }); + + it('the extension does not match', () => { + expect( + testParse('other/unknownFileType.unknown', []), + ).toThrowErrorMatchingSnapshot(); + }); + }); + describe('"parserOptions.extraFileExtensions" is non-empty', () => { describe('the extension matches', () => { it('the file is included', () => { @@ -442,6 +475,18 @@ describe('parse()', () => { testParse('other/notIncluded.vue'), ).toThrowErrorMatchingSnapshot(); }); + + it('duplicate extension', () => { + expect( + testParse('ts/notIncluded.ts', ['.ts']), + ).toThrowErrorMatchingSnapshot(); + }); + }); + + it('invalid extension', () => { + expect( + testParse('other/unknownFileType.unknown', ['unknown']), + ).toThrowErrorMatchingSnapshot(); }); it('the extension does not match', () => {