-
Notifications
You must be signed in to change notification settings - Fork 196
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
getDiagnostics throws when the filename does not have an extension #358
Comments
Hi @lazarljubenovic, There's definitely something going on here, but I wasn't able to reproduce when trying this: const project = new Project();
const file = project.createSourceFile("test.ts", "const a: number = 0 as any as number");
console.log(file.getDiagnostics()); Can you see something I'm missing in that? Maybe it has to do with the way the project is setup. |
Oh, wow. My filename did not have the I guess it should either throw earlier or append |
I had a slight thought that might be the reason. That would be nice, but right now the TypeScript compiler API doesn't error if supplying a file name without a |
Ah... TS internals are weird. Either way, thanks for helping. |
@dsherret seems is not throwing: import * as ts from 'typescript'
export function main(code: string, log: (msg: string) => void) {
const program = createProgram([
{ fileName: 'no_extension', content: 'const a: number = 0 as any as number' },
])
program.getTypeChecker()
program.emit()
log(program.getOptionsDiagnostics().length+'')
log(program.getConfigFileParsingDiagnostics().length+'')
log(program.getGlobalDiagnostics().length+'')
log(program.getDeclarationDiagnostics().length+'')
log(program.getSemanticDiagnostics().length+'')
log(program.getSyntacticDiagnostics().length+'')
}
/** creates a dummy ts.Program in memory with given source files inside */
export function createProgram(files: {
fileName: string, content: string,
sourceFile?: ts.SourceFile
}[], compilerOptions?: ts.CompilerOptions): ts.Program {
const tsConfigJson = ts.parseConfigFileTextToJson('tsconfig.json',
compilerOptions ? JSON.stringify(compilerOptions) : `{
"compilerOptions": {
"target": "es2018",
"module": "commonjs",
"lib": ["es2018"],
"rootDir": ".",
"strict": true,
"esModuleInterop": true,
}
`)
let { options, errors } = ts.convertCompilerOptionsFromJson(tsConfigJson.config.compilerOptions, '.')
if (errors.length) {
throw errors
}
const compilerHost = ts.createCompilerHost(options)
compilerHost.getSourceFile = function (fileName: string, languageVersion: ts.ScriptTarget,
onError?: (message: string) => void, shouldCreateNewSourceFile?: boolean): ts.SourceFile | undefined {
const file = files.find(f => f.fileName === fileName)
if (!file) return undefined
file.sourceFile = file.sourceFile || ts.createSourceFile(fileName, file.content, ts.ScriptTarget.ES2015, true)
return file.sourceFile
}
// in order to typechecker to work we need to implement the following method, the following implementation is enough:
compilerHost.resolveTypeReferenceDirectives = function(typeReferenceDirectiveNames: string[], containingFile: string): (ts.ResolvedTypeReferenceDirective | undefined)[] {
return []
}
return ts.createProgram(files.map(f => f.fileName), options, compilerHost)
} You can copy&paste it in my playground and run it online: |
@cancerberoSgx that code won't ever create a file because getSourceFile will always return undefined. This code shows the issue (btw, typescript-api-playground is sweet! Thanks for that!): import * as ts from 'typescript'
export function main(code: string, log: (msg: string) => void) {
const fileName = "no_extension";
const fileText = 'const a: number = 0 as any as number';
const options = {};
const compilerHost = ts.createCompilerHost(options);
const sourceFile = ts.createSourceFile(fileName, fileText, ts.ScriptTarget.Latest, true);
const program = ts.createProgram([fileName], options, compilerHost);
log(program.getSemanticDiagnostics(sourceFile).map(d => JSON.stringify(d)).join(", "));
} I noticed that the program in the compiler API will take file names without an extension and look for files like |
aja ! nice thanks! I'm not 100% clear in pure TypeScript compiler API thanks for that tip |
Me neither! 😄 I feel like I'm probably not doing something conventional here, but I'll look into it more later. |
Simple:
For stack trace check the details of the original issue below.
Original issue
Taking a stab at the silly #357, I'm getting an error.Idea
Test
Loaded file
Stack trace
Sorry for vague description, I'll create a repro later if you need it.
The text was updated successfully, but these errors were encountered: