-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat: allow user to provide TS program instance in parser options #3484
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bradzacher
merged 19 commits into
typescript-eslint:master
from
uniqueiniquity:provideTSProgram
Jun 8, 2021
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
7c4f35d
feat(typescript-estree): allow ts program to be provided in options
4c6aca9
chore(types): add program option to ParserOptions
f07d72e
test(typescript-estree): add tests for program option
e4cbd2c
test(parser): add test for program option
648caaf
docs: document new program option
defed5d
fix(typescript-estree): fix condition in option application
b1f6213
test(typescript-estree): stop using machine-specific snapshot
4aa4ee0
refactor: address PR comments
ae7005f
fix(typescript-estree): ensure correct value for hasFullTypeInformation
98e515d
feat: provide utility from parser for creating program instance
def60e7
test: use createProgram in tests and add tests for it
71d74eb
docs: document createProgram utility
8b0151e
style: remove unused imports
00e6a3f
Merge remote-tracking branch 'upstream/master' into provideTSProgram
6361d99
test: use consolidated method for program creation
2dded1c
Merge branch 'master' into provideTSProgram
uniqueiniquity c6df390
refactor: respond to PR comments
8892427
Merge branch 'provideTSProgram' of https://github.com/uniqueiniquity/…
ae03746
Merge branch 'master' into provideTSProgram
bradzacher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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, | ||
uniqueiniquity marked this conversation as resolved.
Show resolved
Hide resolved
|
||
getCurrentDirectory: process.cwd, | ||
getNewLine: () => '\n', | ||
}); | ||
} | ||
|
||
export { useProvidedProgram, createProgramFromConfigFile }; |
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.