- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 543
 
Add full diagnostics to tserror #1706
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
      
      
            cspotcode
  merged 18 commits into
  TypeStrong:main
from
paulbrimicombe:add-error-locations-to-tserror
  
      
      
   
  Apr 17, 2022 
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            18 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      4938d99
              
                Add error locations to TSErrors
              
              
                paulbrimicombe 428e506
              
                Simplify tests / code formatting
              
              
                paulbrimicombe 7fb714a
              
                Expose the full TypeScript diagnostics on TSErrors
              
              
                paulbrimicombe f7afcab
              
                Test re-org and deduplication
              
              
                cspotcode e0560d8
              
                lintfix
              
              
                cspotcode b5d6ef6
              
                fix
              
              
                cspotcode a5f64f5
              
                bang head against wall
              
              
                cspotcode 47f769b
              
                lintfix
              
              
                cspotcode 992796b
              
                fix
              
              
                cspotcode 52fcbda
              
                fix
              
              
                cspotcode 5143a48
              
                fix
              
              
                cspotcode 9a0d071
              
                fix
              
              
                cspotcode 08a95c6
              
                fix
              
              
                cspotcode 30b78bd
              
                tweaks
              
              
                cspotcode 217e955
              
                Revert "tweaks"
              
              
                cspotcode 715d3ac
              
                fix
              
              
                cspotcode cd107ed
              
                finally
              
              
                cspotcode b6e01c8
              
                fix
              
              
                cspotcode 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
      
      Oops, something went wrong.
      
    
  
  
    
      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 | 
|---|---|---|
| @@ -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 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
    
  
  
    
              
  
    
      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
    
  
  
    
              
  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.
  
    
  
    
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 this argument optional to avoid a breaking change to our API surface, since
TSErroris part of our API?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.
Yes, that's exactly why it's optional — I didn't want to make this a breaking change.