typescript-service
Advanced tools
@@ -0,1 +1,5 @@ | ||
## <small>2.0.1 (2018-08-26)</small> | ||
* fix: getSourceFile fail fixed if file is not defined in tsconfig ([3225ea7](https://github.com/unlight/typescript-service/commit/3225ea7)) | ||
## 2.0.0 (2018-08-24) | ||
@@ -2,0 +6,0 @@ |
@@ -47,5 +47,5 @@ "use strict"; | ||
var program = ts.createProgram(parsed.fileNames, parsed.options, host); | ||
return program; | ||
return { program: program, host: host }; | ||
} | ||
exports.createProgram = createProgram; | ||
//# sourceMappingURL=create-program.js.map |
@@ -5,19 +5,25 @@ "use strict"; | ||
var create_program_1 = require("./create-program"); | ||
var get_source_file_1 = require("./get-source-file"); | ||
function createService(_a) { | ||
var compilerOptions = _a.compilerOptions, configFile = _a.configFile; | ||
var program = create_program_1.createProgram({ configFile: configFile, compilerOptions: compilerOptions }); | ||
return { | ||
var _b = create_program_1.createProgram({ configFile: configFile, compilerOptions: compilerOptions }), program = _b.program, host = _b.host; | ||
var api = { | ||
getProgram: function () { return program; }, | ||
getSourceFile: function (fileName, sourceText) { | ||
if (sourceText === void 0) { sourceText = ts.sys.readFile(fileName); } | ||
return get_source_file_1.getSourceFile(program, fileName, sourceText); | ||
// todo: fix me optimization sourceText is not used | ||
var sourceFile = program.getSourceFile(fileName); | ||
if (sourceFile === undefined) { | ||
var rootFileNames = program.getRootFileNames().concat([fileName]); | ||
program = ts.createProgram(rootFileNames, program.getCompilerOptions(), host, program); | ||
sourceFile = program.getSourceFile(fileName); | ||
} | ||
return sourceFile; | ||
}, | ||
getDiagnostics: function (fileName, sourceText) { | ||
var sourceFile = get_source_file_1.getSourceFile(program, fileName, sourceText); | ||
var sourceFile = api.getSourceFile(fileName, sourceText); | ||
return program.getSyntacticDiagnostics(sourceFile).concat(program.getSemanticDiagnostics(sourceFile)); | ||
}, | ||
}; | ||
return api; | ||
} | ||
exports.createService = createService; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "typescript-service", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "", |
@@ -11,3 +11,3 @@ import * as ts from 'typescript'; | ||
export function createProgram({ configFile, projectDirectory = path.dirname(configFile), compilerOptions = {} }: CreateProgramArgument): ts.Program { | ||
export function createProgram({ configFile, projectDirectory = path.dirname(configFile), compilerOptions = {} }: CreateProgramArgument) { | ||
const { config, error } = ts.readConfigFile(configFile, ts.sys.readFile); | ||
@@ -52,3 +52,3 @@ if (error !== undefined) { | ||
return program; | ||
return { program, host }; | ||
} |
@@ -34,2 +34,12 @@ import * as assert from 'assert'; | ||
}); | ||
it('typescript checker (file which is not defined in tsconfig)', () => { | ||
const testFile = `${root}/test-project/file.spec.ts`; | ||
const sourceFile = service.getSourceFile(testFile); | ||
const checker = service.getProgram().getTypeChecker(); | ||
const [itstmt] = sourceFile.statements.filter(x => x.getText() === `it('example test');`); | ||
const itid = (itstmt as any).expression.expression; | ||
const symbol = checker.getSymbolAtLocation(itid); | ||
assert(symbol); | ||
}); | ||
}); | ||
@@ -36,0 +46,0 @@ |
import * as ts from 'typescript'; | ||
import { createProgram } from './create-program'; | ||
import { getSourceFile } from './get-source-file'; | ||
@@ -11,10 +10,17 @@ type createServiceOptions = { | ||
export function createService({ compilerOptions, configFile }: createServiceOptions) { | ||
const program = createProgram({ configFile, compilerOptions }); | ||
return { | ||
let { program, host } = createProgram({ configFile, compilerOptions }); | ||
const api = { | ||
getProgram: () => program, | ||
getSourceFile: (fileName: string, sourceText: string | undefined = ts.sys.readFile(fileName)) => { | ||
return getSourceFile(program, fileName, sourceText); | ||
getSourceFile: (fileName: string, sourceText?: string) => { | ||
// todo: fix me optimization sourceText is not used | ||
let sourceFile = program.getSourceFile(fileName); | ||
if (sourceFile === undefined) { | ||
const rootFileNames = [...program.getRootFileNames(), fileName]; | ||
program = ts.createProgram(rootFileNames, program.getCompilerOptions(), host, program); | ||
sourceFile = program.getSourceFile(fileName); | ||
} | ||
return sourceFile; | ||
}, | ||
getDiagnostics: (fileName: string, sourceText?: string) => { | ||
const sourceFile = getSourceFile(program, fileName, sourceText); | ||
const sourceFile = api.getSourceFile(fileName, sourceText); | ||
return [ | ||
@@ -26,2 +32,3 @@ ...program.getSyntacticDiagnostics(sourceFile), | ||
}; | ||
return api; | ||
} |
import { x } from './file'; | ||
it('example test'); | ||
console.assert(x); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
0
-100%21083
-0.16%24
-11.11%303
-1.94%