From 2ee02883ab5651f1024492fa1247b5429e50e3b4 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Fri, 6 Sep 2019 12:14:20 -0700 Subject: [PATCH 1/4] test: temp commit - testing duplicate files --- .../src/WatchCompilerHostOfConfigFile.ts | 29 +++++++++++++++ packages/typescript-estree/src/parser.ts | 1 + .../typescript-estree/src/semantic-errors.ts | 1 + .../typescript-estree/src/tsconfig-parser.ts | 35 ++++++++----------- .../tests/fixtures/duplicateFileNames/test.js | 1 + .../tests/fixtures/duplicateFileNames/test.ts | 1 + .../fixtures/duplicateFileNames/tsconfig.json | 6 ++++ packages/typescript-estree/tests/lib/parse.ts | 26 ++++++++++++++ 8 files changed, 79 insertions(+), 21 deletions(-) create mode 100644 packages/typescript-estree/src/WatchCompilerHostOfConfigFile.ts create mode 100644 packages/typescript-estree/tests/fixtures/duplicateFileNames/test.js create mode 100644 packages/typescript-estree/tests/fixtures/duplicateFileNames/test.ts create mode 100644 packages/typescript-estree/tests/fixtures/duplicateFileNames/tsconfig.json diff --git a/packages/typescript-estree/src/WatchCompilerHostOfConfigFile.ts b/packages/typescript-estree/src/WatchCompilerHostOfConfigFile.ts new file mode 100644 index 000000000000..d5185cb2eef3 --- /dev/null +++ b/packages/typescript-estree/src/WatchCompilerHostOfConfigFile.ts @@ -0,0 +1,29 @@ +// These types are internal to typescript + +import * as ts from 'typescript'; // leave this as * as ts so people using util package don't need syntheticDefaultImports + +interface DirectoryStructureHost { + fileExists(path: string): boolean; + readFile(path: string, encoding?: string): string | undefined; + + // TODO: GH#18217 Optional methods are frequently used as non-optional + directoryExists?(path: string): boolean; + getDirectories?(path: string): string[]; + readDirectory?(path: string, extensions?: ReadonlyArray, exclude?: ReadonlyArray, include?: ReadonlyArray, depth?: number): string[]; + realpath?(path: string): string; + + createDirectory?(path: string): void; + writeFile?(path: string, data: string, writeByteOrderMark?: boolean): void; +} + +interface CachedDirectoryStructureHost extends DirectoryStructureHost { + readDirectory(path: string, extensions?: ReadonlyArray, exclude?: ReadonlyArray, include?: ReadonlyArray, depth?: number): string[]; +} + +interface WatchCompilerHostOfConfigFile extends ts.WatchCompilerHostOfConfigFile { + onCachedDirectoryStructureHostCreate(host: CachedDirectoryStructureHost): void; +} + +export { + WatchCompilerHostOfConfigFile, +} diff --git a/packages/typescript-estree/src/parser.ts b/packages/typescript-estree/src/parser.ts index 642b41aa4889..f74b1c5a2e2e 100644 --- a/packages/typescript-estree/src/parser.ts +++ b/packages/typescript-estree/src/parser.ts @@ -86,6 +86,7 @@ function getASTFromProject( calculateProjectParserOptions(code, filePath, extra), currentProgram => { const ast = currentProgram.getSourceFile(filePath); + console.log(ast); return ast && { ast, program: currentProgram }; }, ); diff --git a/packages/typescript-estree/src/semantic-errors.ts b/packages/typescript-estree/src/semantic-errors.ts index c580697a185f..40dd69337df6 100644 --- a/packages/typescript-estree/src/semantic-errors.ts +++ b/packages/typescript-estree/src/semantic-errors.ts @@ -89,6 +89,7 @@ function whitelistSupportedDiagnostics( case 2364: // ts 3.2 "The left-hand side of an assignment expression must be a variable or a property access." case 2369: // ts 3.2 "A parameter property is only allowed in a constructor implementation." case 2462: // ts 3.2 "A rest element must be last in a destructuring pattern." + // case 5056: // #955 - "Cannot write file '{0}' because it would be overwritten by multiple input files." case 8017: // ts 3.2 "Octal literal types must use ES2015 syntax. Use the syntax '{0}'." case 17012: // ts 3.2 "'{0}' is not a valid meta-property for keyword '{1}'. Did you mean '{2}'?" case 17013: // ts 3.2 "Meta-property '{0}' is only allowed in the body of a function declaration, function expression, or constructor." diff --git a/packages/typescript-estree/src/tsconfig-parser.ts b/packages/typescript-estree/src/tsconfig-parser.ts index 364dd0543c8e..ffa6a09d5e19 100644 --- a/packages/typescript-estree/src/tsconfig-parser.ts +++ b/packages/typescript-estree/src/tsconfig-parser.ts @@ -1,6 +1,7 @@ import path from 'path'; import * as ts from 'typescript'; // leave this as * as ts so people using util package don't need syntheticDefaultImports import { Extra } from './parser-options'; +import { WatchCompilerHostOfConfigFile } from './WatchCompilerHostOfConfigFile'; //------------------------------------------------------------------------------ // Environment calculation @@ -115,7 +116,7 @@ export function calculateProjectParserOptions( ts.createSemanticDiagnosticsBuilderProgram, diagnosticReporter, /*reportWatchStatus*/ () => {}, - ); + ) as WatchCompilerHostOfConfigFile; // ensure readFile reads the code being linted instead of the copy on disk const oldReadFile = watchCompilerHost.readFile; @@ -138,14 +139,19 @@ export function calculateProjectParserOptions( diag.category === ts.DiagnosticCategory.Error && diag.code !== 18003, ); + console.log( + program + .getSourceFiles() + .map(f => f.fileName) + .filter(f => !f.includes('node_modules')), + ); if (configFileDiagnostics.length > 0) { diagnosticReporter(configFileDiagnostics[0]); } }; // register callbacks to trigger program updates without using fileWatchers - // eslint-disable-next-line @typescript-eslint/explicit-function-return-type - watchCompilerHost.watchFile = (fileName, callback) => { + watchCompilerHost.watchFile = (fileName, callback): ts.FileWatcher => { const normalizedFileName = path.normalize(fileName); watchCallbackTrackingMap.set(normalizedFileName, callback); return { @@ -156,26 +162,14 @@ export function calculateProjectParserOptions( }; // ensure fileWatchers aren't created for directories - watchCompilerHost.watchDirectory = (): typeof noopFileWatcher => - noopFileWatcher; + watchCompilerHost.watchDirectory = (): ts.FileWatcher => noopFileWatcher; - // we're using internal typescript APIs which aren't on the types - /* eslint-disable @typescript-eslint/no-explicit-any */ // allow files with custom extensions to be included in program (uses internal ts api) - const oldOnDirectoryStructureHostCreate = (watchCompilerHost as any) - .onCachedDirectoryStructureHostCreate; - (watchCompilerHost as any).onCachedDirectoryStructureHostCreate = ( - host: any, - ): void => { + const oldOnDirectoryStructureHostCreate = + watchCompilerHost.onCachedDirectoryStructureHostCreate; + watchCompilerHost.onCachedDirectoryStructureHostCreate = (host): void => { const oldReadDirectory = host.readDirectory; - // eslint-disable-next-line @typescript-eslint/explicit-function-return-type - host.readDirectory = ( - path: string, - extensions?: readonly string[], - exclude?: readonly string[], - include?: readonly string[], - depth?: number, - ) => + host.readDirectory = (path, extensions, exclude, include, depth): string[] => oldReadDirectory( path, !extensions @@ -187,7 +181,6 @@ export function calculateProjectParserOptions( ); oldOnDirectoryStructureHostCreate(host); }; - /* eslint-enable @typescript-eslint/no-explicit-any */ // create program const programWatch = ts.createWatchProgram(watchCompilerHost); diff --git a/packages/typescript-estree/tests/fixtures/duplicateFileNames/test.js b/packages/typescript-estree/tests/fixtures/duplicateFileNames/test.js new file mode 100644 index 000000000000..ef92ec1ef9f2 --- /dev/null +++ b/packages/typescript-estree/tests/fixtures/duplicateFileNames/test.js @@ -0,0 +1 @@ +console.log('goodbye'); diff --git a/packages/typescript-estree/tests/fixtures/duplicateFileNames/test.ts b/packages/typescript-estree/tests/fixtures/duplicateFileNames/test.ts new file mode 100644 index 000000000000..652fb67bad79 --- /dev/null +++ b/packages/typescript-estree/tests/fixtures/duplicateFileNames/test.ts @@ -0,0 +1 @@ +console.log('henlo'); diff --git a/packages/typescript-estree/tests/fixtures/duplicateFileNames/tsconfig.json b/packages/typescript-estree/tests/fixtures/duplicateFileNames/tsconfig.json new file mode 100644 index 000000000000..638ea7325469 --- /dev/null +++ b/packages/typescript-estree/tests/fixtures/duplicateFileNames/tsconfig.json @@ -0,0 +1,6 @@ +{ + "include": [ + "test.js", + "test.ts", + ] +} diff --git a/packages/typescript-estree/tests/lib/parse.ts b/packages/typescript-estree/tests/lib/parse.ts index 6ed8db76cbc9..d5bf415071d6 100644 --- a/packages/typescript-estree/tests/lib/parse.ts +++ b/packages/typescript-estree/tests/lib/parse.ts @@ -297,4 +297,30 @@ describe('parse()', () => { }); }); }); + + describe('duplicate filenames', () => { + const PROJECT_DIR = resolve(FIXTURES_DIR, '../duplicateFileNames'); + const code = 'var a = true'; + const config: TSESTreeOptions = { + comment: true, + tokens: true, + range: true, + loc: true, + tsconfigRootDir: PROJECT_DIR, + project: './tsconfig.json', + }; + const testParse = (filePath: string): void => { + parser.parseAndGenerateServices(code, { + ...config, + filePath: relative(process.cwd(), join(PROJECT_DIR, filePath)), + }); + }; + + it('doesnt error on duplicate filenames', () => { + expect(() => { + testParse('test.js'); + testParse('test.ts'); + }).not.toThrow(); + }); + }); }); From 3cfd333ca05ac1f7407c8a2941459ac783a11820 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Fri, 6 Sep 2019 22:33:17 -0700 Subject: [PATCH 2/4] remove test code because it's a lost cause --- .vscode/launch.json | 14 ++++++++++ packages/typescript-estree/src/parser.ts | 1 - .../typescript-estree/src/semantic-errors.ts | 1 - .../typescript-estree/src/tsconfig-parser.ts | 16 +++++++----- .../tests/fixtures/duplicateFileNames/test.js | 1 - .../tests/fixtures/duplicateFileNames/test.ts | 1 - .../fixtures/duplicateFileNames/tsconfig.json | 6 ----- packages/typescript-estree/tests/lib/parse.ts | 26 ------------------- 8 files changed, 23 insertions(+), 43 deletions(-) delete mode 100644 packages/typescript-estree/tests/fixtures/duplicateFileNames/test.js delete mode 100644 packages/typescript-estree/tests/fixtures/duplicateFileNames/test.ts delete mode 100644 packages/typescript-estree/tests/fixtures/duplicateFileNames/tsconfig.json diff --git a/.vscode/launch.json b/.vscode/launch.json index 35c5c3194ec3..1c2b4b3dd803 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -18,6 +18,20 @@ "sourceMaps": true, "console": "integratedTerminal", "internalConsoleOptions": "neverOpen" + }, + { + "type": "node", + "request": "launch", + "name": "Run currently opened typescript-estree test", + "cwd": "${workspaceFolder}/packages/typescript-estree/", + "program": "${workspaceFolder}/node_modules/jest/bin/jest.js", + "args": [ + "--runInBand", + "${relativeFile}" + ], + "sourceMaps": true, + "console": "integratedTerminal", + "internalConsoleOptions": "neverOpen" } ] } diff --git a/packages/typescript-estree/src/parser.ts b/packages/typescript-estree/src/parser.ts index f74b1c5a2e2e..642b41aa4889 100644 --- a/packages/typescript-estree/src/parser.ts +++ b/packages/typescript-estree/src/parser.ts @@ -86,7 +86,6 @@ function getASTFromProject( calculateProjectParserOptions(code, filePath, extra), currentProgram => { const ast = currentProgram.getSourceFile(filePath); - console.log(ast); return ast && { ast, program: currentProgram }; }, ); diff --git a/packages/typescript-estree/src/semantic-errors.ts b/packages/typescript-estree/src/semantic-errors.ts index 40dd69337df6..c580697a185f 100644 --- a/packages/typescript-estree/src/semantic-errors.ts +++ b/packages/typescript-estree/src/semantic-errors.ts @@ -89,7 +89,6 @@ function whitelistSupportedDiagnostics( case 2364: // ts 3.2 "The left-hand side of an assignment expression must be a variable or a property access." case 2369: // ts 3.2 "A parameter property is only allowed in a constructor implementation." case 2462: // ts 3.2 "A rest element must be last in a destructuring pattern." - // case 5056: // #955 - "Cannot write file '{0}' because it would be overwritten by multiple input files." case 8017: // ts 3.2 "Octal literal types must use ES2015 syntax. Use the syntax '{0}'." case 17012: // ts 3.2 "'{0}' is not a valid meta-property for keyword '{1}'. Did you mean '{2}'?" case 17013: // ts 3.2 "Meta-property '{0}' is only allowed in the body of a function declaration, function expression, or constructor." diff --git a/packages/typescript-estree/src/tsconfig-parser.ts b/packages/typescript-estree/src/tsconfig-parser.ts index ffa6a09d5e19..e6ef356b6f30 100644 --- a/packages/typescript-estree/src/tsconfig-parser.ts +++ b/packages/typescript-estree/src/tsconfig-parser.ts @@ -14,6 +14,8 @@ export const defaultCompilerOptions: ts.CompilerOptions = { allowNonTsExtensions: true, allowJs: true, checkJs: true, + noEmit: true, + // extendedDiagnostics: true, }; /** @@ -139,12 +141,6 @@ export function calculateProjectParserOptions( diag.category === ts.DiagnosticCategory.Error && diag.code !== 18003, ); - console.log( - program - .getSourceFiles() - .map(f => f.fileName) - .filter(f => !f.includes('node_modules')), - ); if (configFileDiagnostics.length > 0) { diagnosticReporter(configFileDiagnostics[0]); } @@ -169,7 +165,13 @@ export function calculateProjectParserOptions( watchCompilerHost.onCachedDirectoryStructureHostCreate; watchCompilerHost.onCachedDirectoryStructureHostCreate = (host): void => { const oldReadDirectory = host.readDirectory; - host.readDirectory = (path, extensions, exclude, include, depth): string[] => + host.readDirectory = ( + path, + extensions, + exclude, + include, + depth, + ): string[] => oldReadDirectory( path, !extensions diff --git a/packages/typescript-estree/tests/fixtures/duplicateFileNames/test.js b/packages/typescript-estree/tests/fixtures/duplicateFileNames/test.js deleted file mode 100644 index ef92ec1ef9f2..000000000000 --- a/packages/typescript-estree/tests/fixtures/duplicateFileNames/test.js +++ /dev/null @@ -1 +0,0 @@ -console.log('goodbye'); diff --git a/packages/typescript-estree/tests/fixtures/duplicateFileNames/test.ts b/packages/typescript-estree/tests/fixtures/duplicateFileNames/test.ts deleted file mode 100644 index 652fb67bad79..000000000000 --- a/packages/typescript-estree/tests/fixtures/duplicateFileNames/test.ts +++ /dev/null @@ -1 +0,0 @@ -console.log('henlo'); diff --git a/packages/typescript-estree/tests/fixtures/duplicateFileNames/tsconfig.json b/packages/typescript-estree/tests/fixtures/duplicateFileNames/tsconfig.json deleted file mode 100644 index 638ea7325469..000000000000 --- a/packages/typescript-estree/tests/fixtures/duplicateFileNames/tsconfig.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "include": [ - "test.js", - "test.ts", - ] -} diff --git a/packages/typescript-estree/tests/lib/parse.ts b/packages/typescript-estree/tests/lib/parse.ts index d5bf415071d6..6ed8db76cbc9 100644 --- a/packages/typescript-estree/tests/lib/parse.ts +++ b/packages/typescript-estree/tests/lib/parse.ts @@ -297,30 +297,4 @@ describe('parse()', () => { }); }); }); - - describe('duplicate filenames', () => { - const PROJECT_DIR = resolve(FIXTURES_DIR, '../duplicateFileNames'); - const code = 'var a = true'; - const config: TSESTreeOptions = { - comment: true, - tokens: true, - range: true, - loc: true, - tsconfigRootDir: PROJECT_DIR, - project: './tsconfig.json', - }; - const testParse = (filePath: string): void => { - parser.parseAndGenerateServices(code, { - ...config, - filePath: relative(process.cwd(), join(PROJECT_DIR, filePath)), - }); - }; - - it('doesnt error on duplicate filenames', () => { - expect(() => { - testParse('test.js'); - testParse('test.ts'); - }).not.toThrow(); - }); - }); }); From 2b9d8829ef8a166cdd63a5e5520d5686af7afb48 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Fri, 6 Sep 2019 22:40:34 -0700 Subject: [PATCH 3/4] docs: document duplicate files not being supported --- packages/parser/README.md | 4 ++- .../src/WatchCompilerHostOfConfigFile.ts | 27 ++++++++++++++----- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/packages/parser/README.md b/packages/parser/README.md index 042fb4f5f540..9fcb9d043633 100644 --- a/packages/parser/README.md +++ b/packages/parser/README.md @@ -66,7 +66,9 @@ The following additional configuration options are available by specifying them ]; ``` - - Note that if you use project references, TypeScript will not automatically use project references to resolve files. This means that you will have to add each referenced tsconfig to the `project` field either separately, or via a glob. + - If you use project references, TypeScript will not automatically use project references to resolve files. This means that you will have to add each referenced tsconfig to the `project` field either separately, or via a glob. + + - TypeScript will ignore files with duplicate filenames in the same folder (for example, `src/file.ts` and `src/file.js`). TypeScript purposely ignore all but one of the files, only keeping the one file with the highest priority extension (the extension priority order (from highest to lowest) is `.ts`, `.tsx`, `.d.ts`, `.js`, `.jsx`). For more info see #955. - Note that if this setting is specified and `createDefaultProgram` is not, you must only lint files that are included in the projects as defined by the provided `tsconfig.json` files. If your existing configuration does not include all of the files you would like to lint, you can create a separate `tsconfig.eslint.json` as follows: diff --git a/packages/typescript-estree/src/WatchCompilerHostOfConfigFile.ts b/packages/typescript-estree/src/WatchCompilerHostOfConfigFile.ts index d5185cb2eef3..46a84235bf6b 100644 --- a/packages/typescript-estree/src/WatchCompilerHostOfConfigFile.ts +++ b/packages/typescript-estree/src/WatchCompilerHostOfConfigFile.ts @@ -9,7 +9,13 @@ interface DirectoryStructureHost { // TODO: GH#18217 Optional methods are frequently used as non-optional directoryExists?(path: string): boolean; getDirectories?(path: string): string[]; - readDirectory?(path: string, extensions?: ReadonlyArray, exclude?: ReadonlyArray, include?: ReadonlyArray, depth?: number): string[]; + readDirectory?( + path: string, + extensions?: ReadonlyArray, + exclude?: ReadonlyArray, + include?: ReadonlyArray, + depth?: number, + ): string[]; realpath?(path: string): string; createDirectory?(path: string): void; @@ -17,13 +23,20 @@ interface DirectoryStructureHost { } interface CachedDirectoryStructureHost extends DirectoryStructureHost { - readDirectory(path: string, extensions?: ReadonlyArray, exclude?: ReadonlyArray, include?: ReadonlyArray, depth?: number): string[]; + readDirectory( + path: string, + extensions?: ReadonlyArray, + exclude?: ReadonlyArray, + include?: ReadonlyArray, + depth?: number, + ): string[]; } -interface WatchCompilerHostOfConfigFile extends ts.WatchCompilerHostOfConfigFile { - onCachedDirectoryStructureHostCreate(host: CachedDirectoryStructureHost): void; +interface WatchCompilerHostOfConfigFile + extends ts.WatchCompilerHostOfConfigFile { + onCachedDirectoryStructureHostCreate( + host: CachedDirectoryStructureHost, + ): void; } -export { - WatchCompilerHostOfConfigFile, -} +export { WatchCompilerHostOfConfigFile }; From 10f96510292981d1dbf8a61fd03c39bbc6c0c39a Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Fri, 6 Sep 2019 22:50:49 -0700 Subject: [PATCH 4/4] chore: add links for internal types --- .../src/WatchCompilerHostOfConfigFile.ts | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/packages/typescript-estree/src/WatchCompilerHostOfConfigFile.ts b/packages/typescript-estree/src/WatchCompilerHostOfConfigFile.ts index 46a84235bf6b..7fb4663b985c 100644 --- a/packages/typescript-estree/src/WatchCompilerHostOfConfigFile.ts +++ b/packages/typescript-estree/src/WatchCompilerHostOfConfigFile.ts @@ -1,14 +1,11 @@ -// These types are internal to typescript +// These types are internal to TS. +// They have been trimmed down to only include the relevant bits +// We use some special internal TS apis to help us do our parsing flexibly import * as ts from 'typescript'; // leave this as * as ts so people using util package don't need syntheticDefaultImports +// https://github.com/microsoft/TypeScript/blob/b84e65db4ea5c39dbaa2ccd6594efe4653318251/src/compiler/watchUtilities.ts#L6-L18 interface DirectoryStructureHost { - fileExists(path: string): boolean; - readFile(path: string, encoding?: string): string | undefined; - - // TODO: GH#18217 Optional methods are frequently used as non-optional - directoryExists?(path: string): boolean; - getDirectories?(path: string): string[]; readDirectory?( path: string, extensions?: ReadonlyArray, @@ -16,12 +13,9 @@ interface DirectoryStructureHost { include?: ReadonlyArray, depth?: number, ): string[]; - realpath?(path: string): string; - - createDirectory?(path: string): void; - writeFile?(path: string, data: string, writeByteOrderMark?: boolean): void; } +// https://github.com/microsoft/TypeScript/blob/b84e65db4ea5c39dbaa2ccd6594efe4653318251/src/compiler/watchUtilities.ts#L25-L35 interface CachedDirectoryStructureHost extends DirectoryStructureHost { readDirectory( path: string, @@ -32,6 +26,7 @@ interface CachedDirectoryStructureHost extends DirectoryStructureHost { ): string[]; } +// https://github.com/microsoft/TypeScript/blob/5d36aab06f12b0a3ba6197eb14e98204ec0195fb/src/compiler/watch.ts#L548-L554 interface WatchCompilerHostOfConfigFile extends ts.WatchCompilerHostOfConfigFile { onCachedDirectoryStructureHostCreate( pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy