Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow user to provide TS program instance in parser options (#3484
- Loading branch information
1 parent
9c78619
commit e855b18
Showing
13 changed files
with
330 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,5 +48,8 @@ | |
"_ts3.4/*" | ||
] | ||
} | ||
}, | ||
"devDependencies": { | ||
"typescript": "*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
packages/typescript-estree/src/create-program/useProvidedProgram.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import debug from 'debug'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import * as ts from 'typescript'; | ||
import { Extra } from '../parser-options'; | ||
import { | ||
ASTAndProgram, | ||
CORE_COMPILER_OPTIONS, | ||
getAstFromProgram, | ||
} from './shared'; | ||
|
||
const log = debug('typescript-eslint:typescript-estree:useProvidedProgram'); | ||
|
||
function useProvidedProgram( | ||
programInstance: ts.Program, | ||
extra: Extra, | ||
): ASTAndProgram | undefined { | ||
log('Retrieving ast for %s from provided program instance', extra.filePath); | ||
|
||
programInstance.getTypeChecker(); // ensure parent pointers are set in source files | ||
|
||
const astAndProgram = getAstFromProgram(programInstance, extra); | ||
|
||
if (!astAndProgram) { | ||
const relativeFilePath = path.relative( | ||
extra.tsconfigRootDir || process.cwd(), | ||
extra.filePath, | ||
); | ||
const errorLines = [ | ||
'"parserOptions.program" has been provided for @typescript-eslint/parser.', | ||
`The file was not found in the provided program instance: ${relativeFilePath}`, | ||
]; | ||
|
||
throw new Error(errorLines.join('\n')); | ||
} | ||
|
||
return astAndProgram; | ||
} | ||
|
||
/** | ||
* Utility offered by parser to help consumers construct their own program instance. | ||
* | ||
* @param configFile the path to the tsconfig.json file, relative to `projectDirectory` | ||
* @param projectDirectory the project directory to use as the CWD, defaults to `process.cwd()` | ||
*/ | ||
function createProgramFromConfigFile( | ||
configFile: string, | ||
projectDirectory?: string, | ||
): ts.Program { | ||
if (ts.sys === undefined) { | ||
throw new Error( | ||
'`createProgramFromConfigFile` is only supported in a Node-like environment.', | ||
); | ||
} | ||
|
||
const parsed = ts.getParsedCommandLineOfConfigFile( | ||
configFile, | ||
CORE_COMPILER_OPTIONS, | ||
{ | ||
onUnRecoverableConfigFileDiagnostic: diag => { | ||
throw new Error(formatDiagnostics([diag])); // ensures that `parsed` is defined. | ||
}, | ||
fileExists: fs.existsSync, | ||
getCurrentDirectory: () => | ||
(projectDirectory && path.resolve(projectDirectory)) || process.cwd(), | ||
readDirectory: ts.sys.readDirectory, | ||
readFile: file => fs.readFileSync(file, 'utf-8'), | ||
useCaseSensitiveFileNames: ts.sys.useCaseSensitiveFileNames, | ||
}, | ||
); | ||
const result = parsed!; // parsed is not undefined, since we throw on failure. | ||
if (result.errors.length) { | ||
throw new Error(formatDiagnostics(result.errors)); | ||
} | ||
const host = ts.createCompilerHost(result.options, true); | ||
return ts.createProgram(result.fileNames, result.options, host); | ||
} | ||
|
||
function formatDiagnostics(diagnostics: ts.Diagnostic[]): string | undefined { | ||
return ts.formatDiagnostics(diagnostics, { | ||
getCanonicalFileName: f => f, | ||
getCurrentDirectory: process.cwd, | ||
getNewLine: () => '\n', | ||
}); | ||
} | ||
|
||
export { useProvidedProgram, createProgramFromConfigFile }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.