Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(typescript-estree): allow providing code as a ts.SourceFile (#5892)
- Loading branch information
1 parent
bda806d
commit af41b7f
Showing
12 changed files
with
100 additions
and
29 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import * as ts from 'typescript'; | ||
|
||
export function isSourceFile(code: unknown): code is ts.SourceFile { | ||
if (typeof code !== 'object' || code == null) { | ||
return false; | ||
} | ||
|
||
const maybeSourceFile = code as Partial<ts.SourceFile>; | ||
return ( | ||
maybeSourceFile.kind === ts.SyntaxKind.SourceFile && | ||
typeof maybeSourceFile.getFullText === 'function' | ||
); | ||
} | ||
|
||
export function getCodeText(code: string | ts.SourceFile): string { | ||
return isSourceFile(code) ? code.getFullText(code) : code; | ||
} |
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,37 @@ | ||
import * as ts from 'typescript'; | ||
|
||
import { getCodeText, isSourceFile } from '../../src/source-files'; | ||
|
||
describe('isSourceFile', () => { | ||
it.each([null, undefined, {}, { getFullText: (): string => '', text: '' }])( | ||
`returns false when given %j`, | ||
input => { | ||
expect(isSourceFile(input)).toBe(false); | ||
}, | ||
); | ||
|
||
it('returns true when given a real source file', () => { | ||
const input = ts.createSourceFile('test.ts', '', ts.ScriptTarget.ESNext); | ||
|
||
expect(isSourceFile(input)).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('getCodeText', () => { | ||
it('returns the code when code is provided as a string', () => { | ||
const code = '// Hello world'; | ||
|
||
expect(getCodeText(code)).toBe(code); | ||
}); | ||
|
||
it('returns the code when code is provided as a source file', () => { | ||
const code = '// Hello world'; | ||
const sourceFile = ts.createSourceFile( | ||
'test.ts', | ||
code, | ||
ts.ScriptTarget.ESNext, | ||
); | ||
|
||
expect(getCodeText(sourceFile)).toBe(code); | ||
}); | ||
}); |
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