Skip to content

API - DocumentRegistry - sourceFile.scriptTarget can possibly be undefined #26535

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

Closed
dsherret opened this issue Aug 18, 2018 · 5 comments
Closed
Labels
Bug A bug in TypeScript Help Wanted You can do this

Comments

@dsherret
Copy link
Contributor

Low priority issue, but the following is possible which can lead to errors when using the compiler API.

TypeScript Version: 3.1.0-dev.20180818

Search Terms: DocumentRegistry, languageVersion undefined

Code

import * as ts from "typescript";

const registry = ts.createDocumentRegistry();
const sourceFile = registry.acquireDocument("/file.ts", {}, ts.ScriptSnapshot.fromString(""), "1");

console.log(sourceFile.languageVersion); // undefined, but languageVersion is not nullable

Caused by the non-null assertion here.

Expected behavior: Not sure. Probably it should default to ScriptTarget.Latest or the compiler options should pass through the function that sets the default values.

Actual behavior: languageVersion is undefined because it was not set on the compiler options.

Related issues: Maybe #18217 covers this already.

@dsherret
Copy link
Contributor Author

I can fix this if you'd like. Just let me know what the expected behaviour should be.

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this labels Aug 20, 2018
@RyanCavanaugh RyanCavanaugh added this to the Community milestone Aug 20, 2018
@RyanCavanaugh
Copy link
Member

@sheetalkamat @rbuckton opinions on expected behavior?

@sheetalkamat
Copy link
Member

I think we need to change https://github.com/Microsoft/TypeScript/blob/master/src/services/documentRegistry.ts#L187 to

const scriptTarget = scriptKind === ScriptKind.JSON ? ScriptTarget.JSON : compilationSettings.target ||  ScriptTarget.ES5;

@dsherret
Copy link
Contributor Author

dsherret commented Aug 21, 2018

@sheetalkamat thanks! Opened a PR with ES5.

I took a look around the code and it defaults to ES3 in program.ts though. Maybe we should do the same here? What's the rationale for choosing one or the other?

@sheetalkamat
Copy link
Member

Language service defaults to ES5 while tsc defaults to ES3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

3 participants