Skip to content

Commit

Permalink
docs(typescript-estree): document that duplicate filenames are unsupp…
Browse files Browse the repository at this point in the history
…orted (#957)
  • Loading branch information
bradzacher authored and JamesHenry committed Sep 12, 2019
1 parent b99e831 commit b49dbfd
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 21 deletions.
14 changes: 14 additions & 0 deletions .vscode/launch.json
Expand Up @@ -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"
}
]
}
4 changes: 3 additions & 1 deletion packages/parser/README.md
Expand Up @@ -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:

Expand Down
37 changes: 37 additions & 0 deletions packages/typescript-estree/src/WatchCompilerHostOfConfigFile.ts
@@ -0,0 +1,37 @@
// 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 {
readDirectory?(
path: string,
extensions?: ReadonlyArray<string>,
exclude?: ReadonlyArray<string>,
include?: ReadonlyArray<string>,
depth?: number,
): string[];
}

// https://github.com/microsoft/TypeScript/blob/b84e65db4ea5c39dbaa2ccd6594efe4653318251/src/compiler/watchUtilities.ts#L25-L35
interface CachedDirectoryStructureHost extends DirectoryStructureHost {
readDirectory(
path: string,
extensions?: ReadonlyArray<string>,
exclude?: ReadonlyArray<string>,
include?: ReadonlyArray<string>,
depth?: number,
): string[];
}

// https://github.com/microsoft/TypeScript/blob/5d36aab06f12b0a3ba6197eb14e98204ec0195fb/src/compiler/watch.ts#L548-L554
interface WatchCompilerHostOfConfigFile<T extends ts.BuilderProgram>
extends ts.WatchCompilerHostOfConfigFile<T> {
onCachedDirectoryStructureHostCreate(
host: CachedDirectoryStructureHost,
): void;
}

export { WatchCompilerHostOfConfigFile };
35 changes: 15 additions & 20 deletions 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
Expand All @@ -13,6 +14,8 @@ export const defaultCompilerOptions: ts.CompilerOptions = {
allowNonTsExtensions: true,
allowJs: true,
checkJs: true,
noEmit: true,
// extendedDiagnostics: true,
};

/**
Expand Down Expand Up @@ -115,7 +118,7 @@ export function calculateProjectParserOptions(
ts.createSemanticDiagnosticsBuilderProgram,
diagnosticReporter,
/*reportWatchStatus*/ () => {},
);
) as WatchCompilerHostOfConfigFile<ts.SemanticDiagnosticsBuilderProgram>;

// ensure readFile reads the code being linted instead of the copy on disk
const oldReadFile = watchCompilerHost.readFile;
Expand Down Expand Up @@ -144,8 +147,7 @@ export function calculateProjectParserOptions(
};

// 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 {
Expand All @@ -156,26 +158,20 @@ 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,
) =>
path,
extensions,
exclude,
include,
depth,
): string[] =>
oldReadDirectory(
path,
!extensions
Expand All @@ -187,7 +183,6 @@ export function calculateProjectParserOptions(
);
oldOnDirectoryStructureHostCreate(host);
};
/* eslint-enable @typescript-eslint/no-explicit-any */

// create program
const programWatch = ts.createWatchProgram(watchCompilerHost);
Expand Down

0 comments on commit b49dbfd

Please sign in to comment.