mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Extracted compilerOptions setting to helper function
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user