Extracted compilerOptions setting to helper function

This commit is contained in:
Josh Goldberg
2019-01-11 15:05:24 -05:00
parent 8d28f9230c
commit 7b6adae6dd
2 changed files with 43 additions and 45 deletions
@@ -19,42 +19,6 @@ namespace ts.codefix {
});
function makeChange(changeTracker: textChanges.ChangeTracker, configFile: TsConfigSourceFile) {
const tsconfigObjectLiteral = getTsConfigObjectLiteralExpression(configFile);
if (tsconfigObjectLiteral === undefined) {
return;
}
const compilerOptionsProperty = findJsonProperty(tsconfigObjectLiteral, "compilerOptions");
if (compilerOptionsProperty === undefined) {
changeTracker.insertNodeAtObjectStart(configFile, tsconfigObjectLiteral, createCompilerOptionsAssignment());
return;
}
const compilerOptions = compilerOptionsProperty.initializer;
if (!isObjectLiteralExpression(compilerOptions)) {
return;
}
const experimentalDecoratorsProperty = findJsonProperty(compilerOptions, "experimentalDecorators");
if (experimentalDecoratorsProperty === undefined) {
changeTracker.insertNodeAtObjectStart(configFile, compilerOptions, createExperimentalDecoratorsAssignment());
}
else {
changeTracker.replaceNodeWithText(configFile, experimentalDecoratorsProperty.initializer, "true");
}
}
function createCompilerOptionsAssignment() {
return createJsonPropertyAssignment(
"compilerOptions",
createObjectLiteral([
createExperimentalDecoratorsAssignment(),
]),
);
}
function createExperimentalDecoratorsAssignment() {
return createJsonPropertyAssignment("experimentalDecorators", createTrue());
setJsonCompilerOptionValue(changeTracker, configFile, "experimentalDecorators", createTrue());
}
}
+42 -8
View File
@@ -1,13 +1,5 @@
/* @internal */
namespace ts.codefix {
export function createJsonPropertyAssignment(name: string, initializer: Expression) {
return createPropertyAssignment(createStringLiteral(name), initializer);
}
export function findJsonProperty(obj: ObjectLiteralExpression, name: string): PropertyAssignment | undefined {
return find(obj.properties, (p): p is PropertyAssignment => isPropertyAssignment(p) && !!p.name && isStringLiteral(p.name) && p.name.text === name);
}
/**
* Finds members of the resolved type that are missing in the class pointed to by class decl
* and generates source code for the missing members.
@@ -257,4 +249,46 @@ namespace ts.codefix {
}
return undefined;
}
export function setJsonCompilerOptionValue(
changeTracker: textChanges.ChangeTracker,
configFile: TsConfigSourceFile,
optionName: string,
optionValue: Expression,
) {
const tsconfigObjectLiteral = getTsConfigObjectLiteralExpression(configFile);
if (!tsconfigObjectLiteral) return undefined;
const compilerOptionsProperty = findJsonProperty(tsconfigObjectLiteral, "compilerOptions");
if (compilerOptionsProperty === undefined) {
changeTracker.insertNodeAtObjectStart(configFile, tsconfigObjectLiteral, createJsonPropertyAssignment(
"compilerOptions",
createObjectLiteral([
createJsonPropertyAssignment(optionName, optionValue),
])));
return;
}
const compilerOptions = compilerOptionsProperty.initializer;
if (!isObjectLiteralExpression(compilerOptions)) {
return;
}
const optionProperty = findJsonProperty(compilerOptions, optionName);
if (optionProperty === undefined) {
changeTracker.insertNodeAtObjectStart(configFile, compilerOptions, createJsonPropertyAssignment(optionName, optionValue));
}
else {
changeTracker.replaceNode(configFile, optionProperty.initializer, optionValue);
}
}
export function createJsonPropertyAssignment(name: string, initializer: Expression) {
return createPropertyAssignment(createStringLiteral(name), initializer);
}
export function findJsonProperty(obj: ObjectLiteralExpression, name: string): PropertyAssignment | undefined {
return find(obj.properties, (p): p is PropertyAssignment => isPropertyAssignment(p) && !!p.name && isStringLiteral(p.name) && p.name.text === name);
}
}