Skip to content

Commit 0b3074f

Browse files
committed
Keep the original api and add new api that handles the JsonNode
Also handle the JsonNode when converting to parsedCommandLine
1 parent b3f816b commit 0b3074f

14 files changed

+633
-249
lines changed

src/compiler/commandLineParser.ts

+374-88
Large diffs are not rendered by default.

src/compiler/parser.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ namespace ts {
448448
return Parser.parseIsolatedEntityName(text, languageVersion);
449449
}
450450

451-
export type ParsedNodeResults<T extends Node> = { node: T; errors: Diagnostic[] };
451+
export type ParsedNodeResults<T extends Node> = { node?: T; errors: Diagnostic[] };
452452

453453
/**
454454
* Parse json text into SyntaxTree and return node and parse errors if any

src/compiler/tsc.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -366,15 +366,14 @@ namespace ts {
366366
return;
367367
}
368368

369-
const result = parseConfigFileTextToJson(configFileName, cachedConfigFileText);
369+
const result = parseJsonText(configFileName, cachedConfigFileText);
370370
reportDiagnostics(result.errors, /* compilerHost */ undefined);
371-
const configObject = result.config;
372-
if (!configObject) {
371+
if (!result.node) {
373372
sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
374373
return;
375374
}
376375
const cwd = sys.getCurrentDirectory();
377-
const configParseResult = parseJsonConfigFileContent(configObject, sys, getNormalizedAbsolutePath(getDirectoryPath(configFileName), cwd), commandLine.options, getNormalizedAbsolutePath(configFileName, cwd));
376+
const configParseResult = parseJsonNodeConfigFileContent(result.node, sys, getNormalizedAbsolutePath(getDirectoryPath(configFileName), cwd), commandLine.options, getNormalizedAbsolutePath(configFileName, cwd));
378377
if (configParseResult.errors.length > 0) {
379378
reportDiagnostics(configParseResult.errors, /* compilerHost */ undefined);
380379
sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);

src/compiler/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -3322,6 +3322,8 @@ namespace ts {
33223322
/* @internal */
33233323
export interface TsConfigOnlyOption extends CommandLineOptionBase {
33243324
type: "object";
3325+
optionDeclarations?: CommandLineOption[];
3326+
extraKeyDiagnosticMessage?: DiagnosticMessage;
33253327
}
33263328

33273329
/* @internal */

src/harness/harness.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1876,13 +1876,13 @@ namespace Harness {
18761876
for (let i = 0; i < testUnitData.length; i++) {
18771877
const data = testUnitData[i];
18781878
if (ts.getBaseFileName(data.name).toLowerCase() === "tsconfig.json") {
1879-
const configJson = ts.parseConfigFileTextToJson(data.name, data.content);
1880-
assert.isTrue(configJson.config !== undefined);
1879+
const configJson = ts.parseJsonText(data.name, data.content);
1880+
assert.isTrue(configJson.node !== undefined);
18811881
let baseDir = ts.normalizePath(ts.getDirectoryPath(data.name));
18821882
if (rootDir) {
18831883
baseDir = ts.getNormalizedAbsolutePath(baseDir, rootDir);
18841884
}
1885-
tsConfig = ts.parseJsonConfigFileContent(configJson.config, parseConfigHost, baseDir);
1885+
tsConfig = ts.parseJsonNodeConfigFileContent(configJson.node, parseConfigHost, baseDir);
18861886
tsConfig.options.configFilePath = data.name;
18871887

18881888
// delete entry from the list

src/harness/projectsRunner.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -210,22 +210,21 @@ class ProjectRunner extends RunnerBase {
210210

211211
let errors: ts.Diagnostic[];
212212
if (configFileName) {
213-
const result = ts.readConfigFile(configFileName, getSourceFileText);
214-
if (!result.config) {
213+
const result = ts.readConfigFileToJsonNode(configFileName, getSourceFileText);
214+
if (!result.node) {
215215
return {
216216
moduleKind,
217217
errors: result.errors
218218
};
219219
}
220220

221-
const configObject = result.config;
222221
const configParseHost: ts.ParseConfigHost = {
223222
useCaseSensitiveFileNames: Harness.IO.useCaseSensitiveFileNames(),
224223
fileExists,
225224
readDirectory,
226225
readFile
227226
};
228-
const configParseResult = ts.parseJsonConfigFileContent(configObject, configParseHost, ts.getDirectoryPath(configFileName), compilerOptions);
227+
const configParseResult = ts.parseJsonNodeConfigFileContent(result.node, configParseHost, ts.getDirectoryPath(configFileName), compilerOptions);
229228
if (configParseResult.errors.length > 0) {
230229
return {
231230
moduleKind,

src/harness/rwcRunner.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ namespace RWC {
7474
const tsconfigFile = ts.forEach(ioLog.filesRead, f => isTsConfigFile(f) ? f : undefined);
7575
if (tsconfigFile) {
7676
const tsconfigFileContents = getHarnessCompilerInputUnit(tsconfigFile.path);
77-
const parsedTsconfigFileContents = ts.parseConfigFileTextToJson(tsconfigFile.path, tsconfigFileContents.content);
77+
const parsedTsconfigFileContents = ts.parseJsonText(tsconfigFile.path, tsconfigFileContents.content);
7878
const configParseHost: ts.ParseConfigHost = {
7979
useCaseSensitiveFileNames: Harness.IO.useCaseSensitiveFileNames(),
8080
fileExists: Harness.IO.fileExists,
8181
readDirectory: Harness.IO.readDirectory,
8282
readFile: Harness.IO.readFile
8383
};
84-
const configParseResult = ts.parseJsonConfigFileContent(parsedTsconfigFileContents.config, configParseHost, ts.getDirectoryPath(tsconfigFile.path));
84+
const configParseResult = ts.parseJsonNodeConfigFileContent(parsedTsconfigFileContents.node, configParseHost, ts.getDirectoryPath(tsconfigFile.path));
8585
fileNames = configParseResult.fileNames;
8686
opts.options = ts.extend(opts.options, configParseResult.options);
8787
}

src/harness/unittests/configurationExtension.ts

+22-5
Original file line numberDiff line numberDiff line change
@@ -112,23 +112,40 @@ namespace ts {
112112
], ([testName, basePath, host]) => {
113113
function testSuccess(name: string, entry: string, expected: CompilerOptions, expectedFiles: string[]) {
114114
it(name, () => {
115-
const {config, errors} = ts.readConfigFile(entry, name => host.readFile(name));
116-
assert(config && !errors.length, flattenDiagnosticMessageText(errors[0] && errors[0].messageText, "\n"));
115+
const {config, error} = ts.readConfigFile(entry, name => host.readFile(name));
116+
assert(config && !error, flattenDiagnosticMessageText(error && error.messageText, "\n"));
117117
const parsed = ts.parseJsonConfigFileContent(config, host, basePath, {}, entry);
118118
assert(!parsed.errors.length, flattenDiagnosticMessageText(parsed.errors[0] && parsed.errors[0].messageText, "\n"));
119119
expected.configFilePath = entry;
120120
assert.deepEqual(parsed.options, expected);
121121
assert.deepEqual(parsed.fileNames, expectedFiles);
122122
});
123+
124+
it(name, () => {
125+
const {node, errors} = ts.readConfigFileToJsonNode(entry, name => host.readFile(name));
126+
assert(node && !errors.length, flattenDiagnosticMessageText(errors[0] && errors[0].messageText, "\n"));
127+
const parsed = ts.parseJsonNodeConfigFileContent(node, host, basePath, {}, entry);
128+
assert(!parsed.errors.length, flattenDiagnosticMessageText(parsed.errors[0] && parsed.errors[0].messageText, "\n"));
129+
expected.configFilePath = entry;
130+
assert.deepEqual(parsed.options, expected);
131+
assert.deepEqual(parsed.fileNames, expectedFiles);
132+
});
123133
}
124134

125-
function testFailure(name: string, entry: string, expectedDiagnostics: {code: number, category: DiagnosticCategory, messageText: string}[]) {
135+
function testFailure(name: string, entry: string, expectedDiagnostics: { code: number, category: DiagnosticCategory, messageText: string }[]) {
126136
it(name, () => {
127-
const {config, errors} = ts.readConfigFile(entry, name => host.readFile(name));
128-
assert(config && !errors.length, flattenDiagnosticMessageText(errors[0] && errors[0].messageText, "\n"));
137+
const {config, error} = ts.readConfigFile(entry, name => host.readFile(name));
138+
assert(config && !error, flattenDiagnosticMessageText(error && error.messageText, "\n"));
129139
const parsed = ts.parseJsonConfigFileContent(config, host, basePath, {}, entry);
130140
verifyDiagnostics(parsed.errors, expectedDiagnostics);
131141
});
142+
143+
it(name, () => {
144+
const {node, errors} = ts.readConfigFileToJsonNode(entry, name => host.readFile(name));
145+
assert(node && !errors.length, flattenDiagnosticMessageText(errors[0] && errors[0].messageText, "\n"));
146+
const parsed = ts.parseJsonNodeConfigFileContent(node, host, basePath, {}, entry);
147+
verifyDiagnostics(parsed.errors, expectedDiagnostics);
148+
});
132149
}
133150

134151
describe(testName, () => {

src/harness/unittests/convertCompilerOptionsFromJson.ts

+32
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
namespace ts {
55
describe("convertCompilerOptionsFromJson", () => {
66
function assertCompilerOptions(json: any, configFileName: string, expectedResult: { compilerOptions: CompilerOptions, errors: Diagnostic[] }) {
7+
assertCompilerOptionsWithJson(json, configFileName, expectedResult);
8+
assertCompilerOptionsWithJsonNode(json, configFileName, expectedResult);
9+
}
10+
11+
function assertCompilerOptionsWithJson(json: any, configFileName: string, expectedResult: { compilerOptions: CompilerOptions, errors: Diagnostic[] }) {
712
const { options: actualCompilerOptions, errors: actualErrors} = convertCompilerOptionsFromJson(json["compilerOptions"], "/apath/", configFileName);
813

914
const parsedCompilerOptions = JSON.stringify(actualCompilerOptions);
@@ -21,6 +26,33 @@ namespace ts {
2126
}
2227
}
2328

29+
function assertCompilerOptionsWithJsonNode(json: any, configFileName: string, expectedResult: { compilerOptions: CompilerOptions, errors: Diagnostic[] }) {
30+
const fileText = JSON.stringify(json);
31+
const { node, errors } = parseJsonText(configFileName, fileText);
32+
assert(!errors.length);
33+
assert(!!node);
34+
const host: ParseConfigHost = new Utils.MockParseConfigHost("/apath/", true, []);
35+
const { options: actualCompilerOptions, errors: actualParseErrors } = parseJsonNodeConfigFileContent(node, host, "/apath/", /*existingOptions*/ undefined, configFileName);
36+
expectedResult.compilerOptions["configFilePath"] = configFileName;
37+
38+
const parsedCompilerOptions = JSON.stringify(actualCompilerOptions);
39+
const expectedCompilerOptions = JSON.stringify(expectedResult.compilerOptions);
40+
assert.equal(parsedCompilerOptions, expectedCompilerOptions);
41+
42+
const actualErrors = filter(actualParseErrors, error => error.code !== Diagnostics.No_inputs_were_found_in_config_file_0_Specified_include_paths_were_1_and_exclude_paths_were_2.code);
43+
const expectedErrors = expectedResult.errors;
44+
assert.isTrue(expectedResult.errors.length === actualErrors.length, `Expected error: ${JSON.stringify(expectedResult.errors)}. Actual error: ${JSON.stringify(actualErrors)}.`);
45+
for (let i = 0; i < actualErrors.length; i++) {
46+
const actualError = actualErrors[i];
47+
const expectedError = expectedErrors[i];
48+
assert.equal(actualError.code, expectedError.code, `Expected error-code: ${JSON.stringify(expectedError.code)}. Actual error-code: ${JSON.stringify(actualError.code)}.`);
49+
assert.equal(actualError.category, expectedError.category, `Expected error-category: ${JSON.stringify(expectedError.category)}. Actual error-category: ${JSON.stringify(actualError.category)}.`);
50+
assert(actualError.file);
51+
assert(actualError.start);
52+
assert(actualError.length);
53+
}
54+
}
55+
2456
// tsconfig.json tests
2557
it("Convert correctly format tsconfig.json to compiler-options ", () => {
2658
assertCompilerOptions(

src/harness/unittests/convertTypingOptionsFromJson.ts

+31-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
namespace ts {
55
describe("convertTypingOptionsFromJson", () => {
66
function assertTypingOptions(json: any, configFileName: string, expectedResult: { typingOptions: TypingOptions, errors: Diagnostic[] }) {
7+
assertTypingOptionsWithJson(json, configFileName, expectedResult);
8+
assertTypingOptionsWithJsonNode(json, configFileName, expectedResult);
9+
}
10+
11+
function assertTypingOptionsWithJson(json: any, configFileName: string, expectedResult: { typingOptions: TypingOptions, errors: Diagnostic[] }) {
712
const { options: actualTypingOptions, errors: actualErrors } = convertTypingOptionsFromJson(json["typingOptions"], "/apath/", configFileName);
813
const parsedTypingOptions = JSON.stringify(actualTypingOptions);
914
const expectedTypingOptions = JSON.stringify(expectedResult.typingOptions);
@@ -19,6 +24,31 @@ namespace ts {
1924
}
2025
}
2126

27+
function assertTypingOptionsWithJsonNode(json: any, configFileName: string, expectedResult: { typingOptions: TypingOptions, errors: Diagnostic[] }) {
28+
const fileText = JSON.stringify(json);
29+
const { node, errors } = parseJsonText(configFileName, fileText);
30+
assert(!errors.length);
31+
assert(!!node);
32+
const host: ParseConfigHost = new Utils.MockParseConfigHost("/apath/", true, []);
33+
const { typingOptions: actualTypingOptions, errors: actualParseErrors } = parseJsonNodeConfigFileContent(node, host, "/apath/", /*existingOptions*/ undefined, configFileName);
34+
const parsedTypingOptions = JSON.stringify(actualTypingOptions);
35+
const expectedTypingOptions = JSON.stringify(expectedResult.typingOptions);
36+
assert.equal(parsedTypingOptions, expectedTypingOptions);
37+
38+
const actualErrors = filter(actualParseErrors, error => error.code !== Diagnostics.No_inputs_were_found_in_config_file_0_Specified_include_paths_were_1_and_exclude_paths_were_2.code);
39+
const expectedErrors = expectedResult.errors;
40+
assert.isTrue(expectedResult.errors.length === actualErrors.length, `Expected error: ${JSON.stringify(expectedResult.errors)}. Actual error: ${JSON.stringify(actualErrors)}.`);
41+
for (let i = 0; i < actualErrors.length; i++) {
42+
const actualError = actualErrors[i];
43+
const expectedError = expectedErrors[i];
44+
assert.equal(actualError.code, expectedError.code, `Expected error-code: ${JSON.stringify(expectedError.code)}. Actual error-code: ${JSON.stringify(actualError.code)}.`);
45+
assert.equal(actualError.category, expectedError.category, `Expected error-category: ${JSON.stringify(expectedError.category)}. Actual error-category: ${JSON.stringify(actualError.category)}.`);
46+
assert(actualError.file);
47+
assert(actualError.start);
48+
assert(actualError.length);
49+
}
50+
}
51+
2252
// tsconfig.json
2353
it("Convert correctly format tsconfig.json to typing-options ", () => {
2454
assertTypingOptions(
@@ -154,7 +184,7 @@ namespace ts {
154184
},
155185
errors: [
156186
{
157-
category: Diagnostics.Unknown_compiler_option_0.category,
187+
category: Diagnostics.Unknown_typing_option_0.category,
158188
code: Diagnostics.Unknown_typing_option_0.code,
159189
file: undefined,
160190
start: 0,

0 commit comments

Comments
 (0)