forked from nonara/ts-patch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix missing semantic diagnostics in program
Fixes nonara#139
- Loading branch information
Showing
7 changed files
with
211 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"name": "transformer-extras-test", | ||
"main": "src/index.ts", | ||
"dependencies": { | ||
"ts-node" : "^10.9.1" | ||
} | ||
} |
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,34 @@ | ||
const path = require('path'); | ||
|
||
(() => { | ||
const tsInstance = require('ts-patch/compiler'); | ||
|
||
const configPath = path.join(process.cwd(), `tsconfig.json`); | ||
const configText = tsInstance.sys.readFile(configPath); | ||
const configParseResult = tsInstance.parseConfigFileTextToJson(configPath, configText); | ||
const config = configParseResult.config; | ||
|
||
config.compilerOptions.noEmit = false; | ||
config.compilerOptions.skipLibCheck = true; | ||
config.compilerOptions.outDir = 'dist'; | ||
|
||
const sourceFilePath = path.join(__dirname, 'index.ts'); | ||
const program = tsInstance.createProgram({ | ||
rootNames: [ sourceFilePath ], | ||
options: config.compilerOptions, | ||
}); | ||
|
||
const emitResult = program.emit(); | ||
const sourceFile = program.getSourceFile(sourceFilePath); | ||
const semanticDiagnostics = program.getSemanticDiagnostics(sourceFile); | ||
|
||
process.stdout.write(`emitResultDiagnostics:${diagnosticsToJsonString(emitResult.diagnostics)}\n`); | ||
process.stdout.write(`semanticDiagnostics:${diagnosticsToJsonString(semanticDiagnostics)}\n`); | ||
})(); | ||
|
||
function diagnosticsToJsonString(diagnostics): string { | ||
return JSON.stringify(diagnostics.map(diagnostic => { | ||
const { file, start, length, messageText, category, code } = diagnostic; | ||
return { file: file.fileName, start, length, messageText, category, 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 @@ | ||
export const a: string = 42; |
44 changes: 44 additions & 0 deletions
44
test/assets/projects/transformer-extras/src/transformer.ts
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,44 @@ | ||
// @ts-nocheck | ||
import type * as ts from 'typescript' | ||
import type { TransformerExtras } from '../../../../../dist' | ||
|
||
export default function(program: ts.Program, pluginOptions: unknown, transformerExtras?: TransformerExtras) { | ||
return (ctx: ts.TransformationContext) => { | ||
return (sourceFile: ts.SourceFile) => { | ||
transformerExtras?.addDiagnostic({ | ||
file: sourceFile, | ||
code: 42, | ||
messageText: 'It\'s a warning message!', | ||
category: 0, | ||
start: 0, | ||
length: 1, | ||
}); | ||
transformerExtras?.addDiagnostic({ | ||
file: sourceFile, | ||
code: 42, | ||
messageText: 'It\'s an error message!', | ||
category: 1, | ||
start: 1, | ||
length: 2, | ||
}); | ||
transformerExtras?.addDiagnostic({ | ||
file: sourceFile, | ||
code: 42, | ||
messageText: 'It\'s a suggestion message!', | ||
category: 2, | ||
start: 2, | ||
length: 3, | ||
}); | ||
transformerExtras?.addDiagnostic({ | ||
file: sourceFile, | ||
code: 42, | ||
messageText: 'It\'s a message!', | ||
category: 3, | ||
start: 3, | ||
length: 4, | ||
}); | ||
|
||
return sourceFile; | ||
}; | ||
}; | ||
} |
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,15 @@ | ||
{ | ||
"include": [ | ||
"src" | ||
], | ||
"compilerOptions": { | ||
"outDir": "dist", | ||
"module": "commonjs", | ||
"target": "esnext", | ||
"plugins" : [ | ||
{ | ||
"transform": "./src/transformer.ts" | ||
} | ||
] | ||
} | ||
} |
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,89 @@ | ||
import { prepareTestProject } from '../src/project'; | ||
import { execSync } from 'child_process'; | ||
import path from 'path'; | ||
|
||
/* ****************************************************************************************************************** * | ||
* Tests | ||
* ****************************************************************************************************************** */ | ||
|
||
describe('Transformer Extras addDiagnostics', () => { | ||
let projectPath: string; | ||
let output: string[]; | ||
|
||
beforeAll(() => { | ||
const prepRes = prepareTestProject({ projectName: 'transformer-extras' }); | ||
projectPath = prepRes.tmpProjectPath; | ||
|
||
let commandOutput: string; | ||
try { | ||
commandOutput = execSync('ts-node src/compiler.ts', { | ||
cwd: projectPath, | ||
env: { | ||
...process.env, | ||
PATH: `${projectPath}/node_modules/.bin${path.delimiter}${process.env.PATH}` | ||
} | ||
}).toString(); | ||
} | ||
catch (e) { | ||
const err = new Error(e.stdout.toString() + '\n' + e.stderr.toString()); | ||
console.error(err); | ||
throw e; | ||
} | ||
|
||
output = commandOutput.trim().split('\n'); | ||
}); | ||
|
||
test('Provide emit result diagnostics and semantic diagnostics and merge it with original diagnostics', () => { | ||
const [ emitResultDiagnosticsText, semanticDiagnosticsText ] = output; | ||
|
||
const emitResultDiagnostics = JSON.parse(emitResultDiagnosticsText.split('emitResultDiagnostics:')[1]); | ||
const semanticDiagnostics = JSON.parse(semanticDiagnosticsText.split('semanticDiagnostics:')[1]); | ||
|
||
const filePath = path.join(projectPath, 'src/index.ts'); | ||
const expectedEmitResultDiagnostics = [ | ||
{ | ||
file: expect.stringContaining(filePath), | ||
code: 42, | ||
start: 0, | ||
length: 1, | ||
messageText: 'It\'s a warning message!', | ||
category: 0 | ||
}, { | ||
file: expect.stringContaining(filePath), | ||
code: 42, | ||
start: 1, | ||
length: 2, | ||
messageText: 'It\'s an error message!', | ||
category: 1 | ||
}, { | ||
file: expect.stringContaining(filePath), | ||
code: 42, | ||
start: 2, | ||
length: 3, | ||
messageText: 'It\'s a suggestion message!', | ||
category: 2 | ||
}, { | ||
file: expect.stringContaining(filePath), | ||
code: 42, | ||
start: 3, | ||
length: 4, | ||
messageText: 'It\'s a message!', | ||
category: 3 | ||
} | ||
]; | ||
const expectedSemanticDiagnostics = [ | ||
{ | ||
file: expect.stringContaining(filePath), | ||
code: 2322, | ||
category: 1, | ||
length: 1, | ||
messageText: 'Type \'number\' is not assignable to type \'string\'.', | ||
start: 13, | ||
}, | ||
...expectedEmitResultDiagnostics, | ||
] | ||
|
||
expect(emitResultDiagnostics).toEqual(expectedEmitResultDiagnostics); | ||
expect(semanticDiagnostics).toEqual(expectedSemanticDiagnostics); | ||
}); | ||
}); |