Skip to content

Commit ed4cbb2

Browse files
author
Keen Yee Liau
committed
use ts.parseConfigFileTextToJson instead of JSON.parse
1 parent 8842969 commit ed4cbb2

File tree

1 file changed

+5
-25
lines changed

1 file changed

+5
-25
lines changed

server/src/editorServices.ts

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -277,28 +277,6 @@ function getBaseFileName(path: string) {
277277
return i < 0 ? path : path.substring(i + 1);
278278
}
279279

280-
281-
export function parseAndReEmitConfigJSONFile(content: string) {
282-
const options: ts.TranspileOptions = {
283-
fileName: "config.js",
284-
compilerOptions: {
285-
target: ts.ScriptTarget.Latest,
286-
removeComments: true
287-
},
288-
reportDiagnostics: true
289-
};
290-
const { outputText, diagnostics } = ts.transpileModule("(" + content + ")", options);
291-
// Becasue the content was wrapped in "()", the start position of diagnostics needs to be subtract by 1
292-
// also, the emitted result will have "(" in the beginning and ");" in the end. We need to strip these
293-
// as well
294-
const trimmedOutput = outputText.trim();
295-
const configJsonObject = JSON.parse(trimmedOutput.substring(1, trimmedOutput.length - 2));
296-
for (const diagnostic of diagnostics) {
297-
diagnostic.start = diagnostic.start - 1;
298-
}
299-
return { configJsonObject, diagnostics };
300-
}
301-
302280
function reduceProperties<T, U>(map: ts.Map<T>, callback: (aggregate: U, value: T, key: string) => U, initial: U): U {
303281
let result = initial;
304282
for (const key in map) {
@@ -1841,9 +1819,11 @@ export class ProjectService {
18411819
// file references will be relative to dirPath (or absolute)
18421820
const dirPath = getDirectoryPath(configFilename);
18431821
const contents = this.host.readFile(configFilename);
1844-
const { configJsonObject, diagnostics } = parseAndReEmitConfigJSONFile(contents);
1845-
errors = concatenate(errors, diagnostics);
1846-
const parsedCommandLine = ts.parseJsonConfigFileContent(configJsonObject, this.host, dirPath, /*existingOptions*/ {}, configFilename);
1822+
const {config, error} = ts.parseConfigFileTextToJson(configFilename, contents);
1823+
if (error) {
1824+
errors.push(error);
1825+
}
1826+
const parsedCommandLine = ts.parseJsonConfigFileContent(config, this.host, dirPath, /*existingOptions*/ {}, configFilename);
18471827
errors = concatenate(errors, parsedCommandLine.errors);
18481828
// Debug.assert(!!parsedCommandLine.fileNames);
18491829

0 commit comments

Comments
 (0)