Repository navigation
Fix #3245: ensure transpile diagnostics only include syntactic and compiler options diagnostics #3274
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
Fix #3245: ensure transpile diagnostics only include syntactic and compiler options diagnostics #3274
Changes from all commits
e7eef83
2cbe14e
c83054c
6902b05
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1064,6 +1064,7 @@ module ts { | |
| getGlobalDiagnostics(): Diagnostic[]; | ||
| getSemanticDiagnostics(sourceFile?: SourceFile): Diagnostic[]; | ||
| getDeclarationDiagnostics(sourceFile?: SourceFile): Diagnostic[]; | ||
| /* @internal */ getCompilerOptionsDiagnostics(): Diagnostic[]; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is bad. We now have multiple ways to get the same diagnostics. Previously, diagnostics were entirely split, and any given diagnostic would only be in one of the four "getXXXDiagnostics" methods. Now both getGlobalDiagnostics and getCompilerOptions diagnostics will return the same errors. This makes it much harder for clients to know which one they should call. If you do want to expose getCompilerOptionsDiagnostics, then i would remove the compiler options diagnostics from getGlobalDiagnostics.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is why i made it internal.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not just concerned about external clients. I'm also concerned about our own code :) This is now more confusing for us in terms of knowing which of these functions we should be calling. Previously it was simple. To get all errors, with no duplicates, you just called each of these functions. Now you have to know "oh... if i'm transpiling, then i should call "getCompilerOptionsDiagnostics" but not "getGlobalDiagnostics". This is not discoverable and will cause confusion. Also, do we need to fix up anything with 'emit' in the LS case? If the user has hte option set to just transpile, should we be changing how we get errors?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. transpile is not an option. it is an entry point. the LS never uses transpile, nor does the commandline compiler. |
||
|
|
||
| /** | ||
| * Gets a type checker that can be used to semantically analyze source fils in the program. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1774,6 +1774,21 @@ module ts { | |
|
|
||
| return result; | ||
| } | ||
|
|
||
| const carriageReturnLineFeed = "\r\n"; | ||
| const lineFeed = "\n"; | ||
| export function getNewLineCharacter(options: CompilerOptions): string { | ||
| if (options.newLine === NewLineKind.CarriageReturnLineFeed) { | ||
| return carriageReturnLineFeed; | ||
| } | ||
| else if (options.newLine === NewLineKind.LineFeed) { | ||
| return lineFeed; | ||
| } | ||
| else if (sys) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe just have a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think sys is always defined now, it is just may have no value if you are in services. |
||
| return sys.newLine | ||
| } | ||
| return carriageReturnLineFeed; | ||
| } | ||
| } | ||
|
|
||
| module ts { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1763,6 +1763,8 @@ module ts { | |
| * Extra compiler options that will unconditionally be used bu this function are: | ||
| * - isolatedModules = true | ||
| * - allowNonTsExtensions = true | ||
| * - noLib = true | ||
| * - noResolve = true | ||
| */ | ||
| export function transpile(input: string, compilerOptions?: CompilerOptions, fileName?: string, diagnostics?: Diagnostic[]): string { | ||
| let options = compilerOptions ? clone(compilerOptions) : getDefaultCompilerOptions(); | ||
|
|
@@ -1772,15 +1774,25 @@ module ts { | |
| // Filename can be non-ts file. | ||
| options.allowNonTsExtensions = true; | ||
|
|
||
| // We are not returning a sourceFile for lib file when asked by the program, | ||
| // so pass --noLib to avoid reporting a file not found error. | ||
| options.noLib = true; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure what this comment means.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure how to explain this more.. i updated the comment message, the reference here is to getSourceFile method in the host implementation.. see: getSourceFile: (fileName, target) => fileName === inputFileName ? sourceFile : undefined, |
||
|
|
||
| // We are not doing a full typecheck, we are not resolving the whole context, | ||
| // so pass --noResolve to avoid reporting missing file errors. | ||
| options.noResolve = true; | ||
|
|
||
| // Parse | ||
| var inputFileName = fileName || "module.ts"; | ||
| var sourceFile = createSourceFile(inputFileName, input, options.target); | ||
| let inputFileName = fileName || "module.ts"; | ||
| let sourceFile = createSourceFile(inputFileName, input, options.target); | ||
|
|
||
| // Store syntactic diagnostics | ||
| if (diagnostics && sourceFile.parseDiagnostics) { | ||
| diagnostics.push(...sourceFile.parseDiagnostics); | ||
| } | ||
|
|
||
| let newLine = getNewLineCharacter(options); | ||
|
|
||
| // Output | ||
| let outputText: string; | ||
|
|
||
|
|
@@ -1795,13 +1807,13 @@ module ts { | |
| useCaseSensitiveFileNames: () => false, | ||
| getCanonicalFileName: fileName => fileName, | ||
| getCurrentDirectory: () => "", | ||
| getNewLine: () => (sys && sys.newLine) || "\r\n" | ||
| getNewLine: () => newLine | ||
| }; | ||
|
|
||
| var program = createProgram([inputFileName], options, compilerHost); | ||
|
|
||
| if (diagnostics) { | ||
| diagnostics.push(...program.getGlobalDiagnostics()); | ||
| diagnostics.push(...program.getCompilerOptionsDiagnostics()); | ||
| } | ||
|
|
||
| // Emit | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /// <reference path="..\..\..\src\harness\harness.ts" /> | ||
|
|
||
| module ts { | ||
| describe("Transpile", () => { | ||
|
|
||
| function runTest(input: string, compilerOptions: ts.CompilerOptions = {}, expectedOutput?: string, expectedDiagnosticCodes: number[] = []): void { | ||
| let diagnostics: Diagnostic[] = []; | ||
| let result = transpile(input, compilerOptions, "file.ts", diagnostics); | ||
|
|
||
| for (let i = 0; i < expectedDiagnosticCodes.length; i++) { | ||
| assert.equal(expectedDiagnosticCodes[i], diagnostics[i] && diagnostics[i].code, `Could not find expeced diagnostic.`); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When this fails what do we see? Also just use a string literal here.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it shows a message and the two codes.. not sure i see what else to put in htere..
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rather you checked the sizes instead of
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. size is checked in the next line. |
||
| } | ||
| assert.equal(diagnostics.length, expectedDiagnosticCodes.length, "Resuting diagnostics count does not match expected"); | ||
|
|
||
| if (expectedOutput !== undefined) { | ||
| assert.equal(result, expectedOutput); | ||
| } | ||
| } | ||
|
|
||
| it("Generates correct compilerOptions diagnostics", () => { | ||
| // Expecting 5047: "Option 'isolatedModules' can only be used when either option'--module' is provided or option 'target' is 'ES6' or higher." | ||
| runTest(`var x = 0;`, {}, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ [5047]); | ||
| }); | ||
|
|
||
| it("Generates no diagnostics with valid inputs", () => { | ||
| // No errors | ||
| runTest(`var x = 0;`, { module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []); | ||
| }); | ||
|
|
||
| it("Generates no diagnostics for missing file references", () => { | ||
| runTest(`/// <reference path="file2.ts" /> | ||
| var x = 0;`, | ||
| { module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []); | ||
| }); | ||
|
|
||
| it("Generates no diagnostics for missing module imports", () => { | ||
| runTest(`import {a} from "module2";`, | ||
| { module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []); | ||
| }); | ||
|
|
||
| it("Generates expected syntactic diagnostics", () => { | ||
| runTest(`a b`, | ||
| { module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ [1005]); /// 1005: ';' Expected | ||
| }); | ||
|
|
||
| it("Does not generate semantic diagnostics", () => { | ||
| runTest(`var x: string = 0;`, | ||
| { module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []); | ||
| }); | ||
|
|
||
| it("Generates module output", () => { | ||
| runTest(`var x = 0;`, { module: ModuleKind.AMD }, `define(["require", "exports"], function (require, exports) {\r\n var x = 0;\r\n});\r\n`); | ||
| }); | ||
|
|
||
| it("Uses correct newLine character", () => { | ||
| runTest(`var x = 0;`, { module: ModuleKind.CommonJS, newLine: NewLineKind.LineFeed }, `var x = 0;\n`, /*expectedDiagnosticCodes*/ []); | ||
| }); | ||
| }); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason we can't just call
getGlobalDiagnostics? Does it cache?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It also gets the checker global diagnostics like cannot find Array. I did not want to pass a flag to it, do adding the new function seemed more appealing.