Merge pull request #7595 from Microsoft/revertParseCompilerOptionsFromJson
revert changes in the signature of 'convertCompilerOptionsFromJson'
This commit is contained in:
commit
4e06457b1d
5 changed files with 131 additions and 14 deletions
|
@ -590,9 +590,9 @@ namespace ts {
|
|||
*/
|
||||
export function parseJsonConfigFileContent(json: any, host: ParseConfigHost, basePath: string, existingOptions: CompilerOptions = {}, configFileName?: string): ParsedCommandLine {
|
||||
const errors: Diagnostic[] = [];
|
||||
const compilerOptions: CompilerOptions = convertCompilerOptionsFromJson(optionDeclarations, json["compilerOptions"], basePath, errors, configFileName);
|
||||
const compilerOptions: CompilerOptions = convertCompilerOptionsFromJsonWorker(json["compilerOptions"], basePath, errors, configFileName);
|
||||
const options = extend(existingOptions, compilerOptions);
|
||||
const typingOptions: TypingOptions = convertTypingOptionsFromJson(typingOptionDeclarations, json["typingOptions"], basePath, errors, configFileName);
|
||||
const typingOptions: TypingOptions = convertTypingOptionsFromJsonWorker(json["typingOptions"], basePath, errors, configFileName);
|
||||
|
||||
const fileNames = getFileNames(errors);
|
||||
|
||||
|
@ -666,28 +666,38 @@ namespace ts {
|
|||
}
|
||||
}
|
||||
|
||||
/* @internal */
|
||||
export function convertCompilerOptionsFromJson(optionsDeclarations: CommandLineOption[], jsonOptions: any,
|
||||
export function convertCompilerOptionsFromJson(jsonOptions: any, basePath: string, configFileName?: string): { options: CompilerOptions, errors: Diagnostic[] } {
|
||||
const errors: Diagnostic[] = [];
|
||||
const options = convertCompilerOptionsFromJsonWorker(jsonOptions, basePath, errors, configFileName);
|
||||
return { options, errors };
|
||||
}
|
||||
|
||||
export function convertTypingOptionsFromJson(jsonOptions: any, basePath: string, configFileName?: string): { options: CompilerOptions, errors: Diagnostic[] } {
|
||||
const errors: Diagnostic[] = [];
|
||||
const options = convertTypingOptionsFromJsonWorker(jsonOptions, basePath, errors, configFileName);
|
||||
return { options, errors };
|
||||
}
|
||||
|
||||
function convertCompilerOptionsFromJsonWorker(jsonOptions: any,
|
||||
basePath: string, errors: Diagnostic[], configFileName?: string): CompilerOptions {
|
||||
|
||||
const options: CompilerOptions = getBaseFileName(configFileName) === "jsconfig.json" ? { allowJs: true } : {};
|
||||
convertOptionsFromJson<CompilerOptions>(optionDeclarations, jsonOptions, basePath, options, Diagnostics.Unknown_compiler_option_0, errors);
|
||||
convertOptionsFromJson(optionDeclarations, jsonOptions, basePath, options, Diagnostics.Unknown_compiler_option_0, errors);
|
||||
return options;
|
||||
}
|
||||
|
||||
/* @internal */
|
||||
export function convertTypingOptionsFromJson(optionsDeclarations: CommandLineOption[], jsonOptions: any,
|
||||
function convertTypingOptionsFromJsonWorker(jsonOptions: any,
|
||||
basePath: string, errors: Diagnostic[], configFileName?: string): TypingOptions {
|
||||
|
||||
const options: TypingOptions = getBaseFileName(configFileName) === "jsconfig.json"
|
||||
? { enableAutoDiscovery: true, include: [], exclude: [] }
|
||||
: { enableAutoDiscovery: false, include: [], exclude: [] };
|
||||
convertOptionsFromJson<TypingOptions>(typingOptionDeclarations, jsonOptions, basePath, options, Diagnostics.Unknown_typing_option_0, errors);
|
||||
convertOptionsFromJson(typingOptionDeclarations, jsonOptions, basePath, options, Diagnostics.Unknown_typing_option_0, errors);
|
||||
return options;
|
||||
}
|
||||
|
||||
function convertOptionsFromJson<T extends CompilerOptions | TypingOptions>(optionDeclarations: CommandLineOption[], jsonOptions: any, basePath: string,
|
||||
defaultOptions: T, diagnosticMessage: DiagnosticMessage, errors: Diagnostic[]) {
|
||||
function convertOptionsFromJson(optionDeclarations: CommandLineOption[], jsonOptions: any, basePath: string,
|
||||
defaultOptions: CompilerOptions | TypingOptions, diagnosticMessage: DiagnosticMessage, errors: Diagnostic[]) {
|
||||
|
||||
if (!jsonOptions) {
|
||||
return ;
|
||||
|
|
70
tests/baselines/reference/APISample_parseConfig.js
Normal file
70
tests/baselines/reference/APISample_parseConfig.js
Normal file
|
@ -0,0 +1,70 @@
|
|||
//// [APISample_parseConfig.ts]
|
||||
|
||||
/*
|
||||
* Note: This test is a public API sample. The sample sources can be found
|
||||
at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#a-minimal-compiler
|
||||
* Please log a "breaking change" issue for any API breaking change affecting this issue
|
||||
*/
|
||||
|
||||
declare var process: any;
|
||||
declare var console: any;
|
||||
declare var os: any;
|
||||
|
||||
import ts = require("typescript");
|
||||
|
||||
function printError(error: ts.Diagnostic): void {
|
||||
if (!error) {
|
||||
return;
|
||||
}
|
||||
console.log(`${error.file && error.file.fileName}: ${error.messageText}`);
|
||||
}
|
||||
|
||||
export function createProgram(rootFiles: string[], compilerOptionsJson: string): ts.Program {
|
||||
const { config, error } = ts.parseConfigFileTextToJson("tsconfig.json", compilerOptionsJson)
|
||||
if (error) {
|
||||
printError(error);
|
||||
return undefined;
|
||||
}
|
||||
const basePath: string = process.cwd();
|
||||
const settings = ts.convertCompilerOptionsFromJson(config.config["compilerOptions"], basePath);
|
||||
if (!settings.options) {
|
||||
for (const err of settings.errors) {
|
||||
printError(err);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
return ts.createProgram(rootFiles, settings.options);
|
||||
}
|
||||
|
||||
//// [APISample_parseConfig.js]
|
||||
/*
|
||||
* Note: This test is a public API sample. The sample sources can be found
|
||||
at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#a-minimal-compiler
|
||||
* Please log a "breaking change" issue for any API breaking change affecting this issue
|
||||
*/
|
||||
"use strict";
|
||||
var ts = require("typescript");
|
||||
function printError(error) {
|
||||
if (!error) {
|
||||
return;
|
||||
}
|
||||
console.log((error.file && error.file.fileName) + ": " + error.messageText);
|
||||
}
|
||||
function createProgram(rootFiles, compilerOptionsJson) {
|
||||
var _a = ts.parseConfigFileTextToJson("tsconfig.json", compilerOptionsJson), config = _a.config, error = _a.error;
|
||||
if (error) {
|
||||
printError(error);
|
||||
return undefined;
|
||||
}
|
||||
var basePath = process.cwd();
|
||||
var settings = ts.convertCompilerOptionsFromJson(config.config["compilerOptions"], basePath);
|
||||
if (!settings.options) {
|
||||
for (var _i = 0, _b = settings.errors; _i < _b.length; _i++) {
|
||||
var err = _b[_i];
|
||||
printError(err);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
return ts.createProgram(rootFiles, settings.options);
|
||||
}
|
||||
exports.createProgram = createProgram;
|
39
tests/cases/compiler/APISample_parseConfig.ts
Normal file
39
tests/cases/compiler/APISample_parseConfig.ts
Normal file
|
@ -0,0 +1,39 @@
|
|||
// @module: commonjs
|
||||
// @includebuiltfile: typescript_standalone.d.ts
|
||||
// @stripInternal:true
|
||||
|
||||
/*
|
||||
* Note: This test is a public API sample. The sample sources can be found
|
||||
at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#a-minimal-compiler
|
||||
* Please log a "breaking change" issue for any API breaking change affecting this issue
|
||||
*/
|
||||
|
||||
declare var process: any;
|
||||
declare var console: any;
|
||||
declare var os: any;
|
||||
|
||||
import ts = require("typescript");
|
||||
|
||||
function printError(error: ts.Diagnostic): void {
|
||||
if (!error) {
|
||||
return;
|
||||
}
|
||||
console.log(`${error.file && error.file.fileName}: ${error.messageText}`);
|
||||
}
|
||||
|
||||
export function createProgram(rootFiles: string[], compilerOptionsJson: string): ts.Program {
|
||||
const { config, error } = ts.parseConfigFileTextToJson("tsconfig.json", compilerOptionsJson)
|
||||
if (error) {
|
||||
printError(error);
|
||||
return undefined;
|
||||
}
|
||||
const basePath: string = process.cwd();
|
||||
const settings = ts.convertCompilerOptionsFromJson(config.config["compilerOptions"], basePath);
|
||||
if (!settings.options) {
|
||||
for (const err of settings.errors) {
|
||||
printError(err);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
return ts.createProgram(rootFiles, settings.options);
|
||||
}
|
|
@ -4,8 +4,7 @@
|
|||
namespace ts {
|
||||
describe('convertCompilerOptionsFromJson', () => {
|
||||
function assertCompilerOptions(json: any, configFileName: string, expectedResult: { compilerOptions: CompilerOptions, errors: Diagnostic[] }) {
|
||||
const actualErrors: Diagnostic[] = [];
|
||||
const actualCompilerOptions: CompilerOptions = convertCompilerOptionsFromJson(optionDeclarations, json["compilerOptions"], "/apath/", actualErrors, configFileName);
|
||||
const { options: actualCompilerOptions, errors: actualErrors} = convertCompilerOptionsFromJson(json["compilerOptions"], "/apath/", configFileName);
|
||||
|
||||
const parsedCompilerOptions = JSON.stringify(actualCompilerOptions);
|
||||
const expectedCompilerOptions = JSON.stringify(expectedResult.compilerOptions);
|
||||
|
|
|
@ -4,8 +4,7 @@
|
|||
namespace ts {
|
||||
describe('convertTypingOptionsFromJson', () => {
|
||||
function assertTypingOptions(json: any, configFileName: string, expectedResult: { typingOptions: TypingOptions, errors: Diagnostic[] }) {
|
||||
const actualErrors: Diagnostic[] = [];
|
||||
const actualTypingOptions = convertTypingOptionsFromJson(typingOptionDeclarations, json["typingOptions"], "/apath/", actualErrors, configFileName);
|
||||
const { options: actualTypingOptions, errors: actualErrors } = convertTypingOptionsFromJson(json["typingOptions"], "/apath/", configFileName);
|
||||
const parsedTypingOptions = JSON.stringify(actualTypingOptions);
|
||||
const expectedTypingOptions = JSON.stringify(expectedResult.typingOptions);
|
||||
assert.equal(parsedTypingOptions, expectedTypingOptions);
|
||||
|
|
Loading…
Reference in a new issue