-
-
Notifications
You must be signed in to change notification settings - Fork 531
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add full diagnostics to tserror (#1706)
* Add error locations to TSErrors * Simplify tests / code formatting * Expose the full TypeScript diagnostics on TSErrors * Test re-org and deduplication * lintfix * fix * bang head against wall * lintfix * fix * fix * fix * fix * fix * tweaks * Revert "tweaks" This reverts commit 30b78bd. * fix * finally * fix Co-authored-by: Andrew Bradley <[email protected]>
- Loading branch information
1 parent
dc907bc
commit deef947
Showing
9 changed files
with
108 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import type { TSError } from '..'; | ||
import { contextTsNodeUnderTest, ts } from './helpers'; | ||
import { context, expect } from './testlib'; | ||
import * as semver from 'semver'; | ||
import { once } from 'lodash'; | ||
const test = context(contextTsNodeUnderTest); | ||
|
||
test.suite('TSError diagnostics', ({ context }) => { | ||
const test = context( | ||
once(async (t) => { | ||
const service = t.context.tsNodeUnderTest.create({ | ||
compilerOptions: { target: 'es5' }, | ||
skipProject: true, | ||
}); | ||
try { | ||
service.compile('new Error(123)', 'test.ts'); | ||
} catch (err) { | ||
return { service, err }; | ||
} | ||
return { service, err: undefined }; | ||
}) | ||
); | ||
|
||
const diagnosticCode = 2345; | ||
const diagnosticMessage = semver.satisfies(ts.version, '2.7') | ||
? "Argument of type '123' " + | ||
"is not assignable to parameter of type 'string | undefined'." | ||
: "Argument of type 'number' " + | ||
"is not assignable to parameter of type 'string'."; | ||
const diagnosticErrorMessage = `TS${diagnosticCode}: ${diagnosticMessage}`; | ||
|
||
const cwdBefore = process.cwd(); | ||
test('should throw errors', ({ log, context: { err, service } }) => { | ||
log({ | ||
version: ts.version, | ||
serviceVersion: service.ts.version, | ||
cwdBefore, | ||
cwd: process.cwd(), | ||
configFilePath: service.configFilePath, | ||
config: service.config.options, | ||
}); | ||
expect(err).toBeDefined(); | ||
expect((err as Error).message).toMatch(diagnosticErrorMessage); | ||
}); | ||
|
||
test('should throw errors with diagnostic text', ({ context: { err } }) => { | ||
expect((err as TSError).diagnosticText).toMatch(diagnosticErrorMessage); | ||
}); | ||
|
||
test('should throw errors with diagnostic codes', ({ context: { err } }) => { | ||
expect((err as TSError).diagnosticCodes).toEqual([2345]); | ||
}); | ||
|
||
test('should throw errors with complete diagnostic information', ({ | ||
context: { err }, | ||
}) => { | ||
const diagnostics = (err as TSError).diagnostics; | ||
|
||
expect(diagnostics).toHaveLength(1); | ||
expect(diagnostics[0]).toMatchObject({ | ||
code: 2345, | ||
start: 10, | ||
length: 3, | ||
messageText: expect.stringMatching(diagnosticMessage), | ||
}); | ||
}); | ||
}); |
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
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