Skip to content

Commit 7b6adae

Browse files
author
Josh Goldberg
committed
Extracted compilerOptions setting to helper function
1 parent 8d28f92 commit 7b6adae

File tree

2 files changed

+43
-45
lines changed

2 files changed

+43
-45
lines changed

src/services/codefixes/fixEnableExperimentalDecorators.ts

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,42 +19,6 @@ namespace ts.codefix {
1919
});
2020

2121
function makeChange(changeTracker: textChanges.ChangeTracker, configFile: TsConfigSourceFile) {
22-
const tsconfigObjectLiteral = getTsConfigObjectLiteralExpression(configFile);
23-
if (tsconfigObjectLiteral === undefined) {
24-
return;
25-
}
26-
27-
const compilerOptionsProperty = findJsonProperty(tsconfigObjectLiteral, "compilerOptions");
28-
if (compilerOptionsProperty === undefined) {
29-
changeTracker.insertNodeAtObjectStart(configFile, tsconfigObjectLiteral, createCompilerOptionsAssignment());
30-
return;
31-
}
32-
33-
const compilerOptions = compilerOptionsProperty.initializer;
34-
if (!isObjectLiteralExpression(compilerOptions)) {
35-
return;
36-
}
37-
38-
const experimentalDecoratorsProperty = findJsonProperty(compilerOptions, "experimentalDecorators");
39-
40-
if (experimentalDecoratorsProperty === undefined) {
41-
changeTracker.insertNodeAtObjectStart(configFile, compilerOptions, createExperimentalDecoratorsAssignment());
42-
}
43-
else {
44-
changeTracker.replaceNodeWithText(configFile, experimentalDecoratorsProperty.initializer, "true");
45-
}
46-
}
47-
48-
function createCompilerOptionsAssignment() {
49-
return createJsonPropertyAssignment(
50-
"compilerOptions",
51-
createObjectLiteral([
52-
createExperimentalDecoratorsAssignment(),
53-
]),
54-
);
55-
}
56-
57-
function createExperimentalDecoratorsAssignment() {
58-
return createJsonPropertyAssignment("experimentalDecorators", createTrue());
22+
setJsonCompilerOptionValue(changeTracker, configFile, "experimentalDecorators", createTrue());
5923
}
6024
}

src/services/codefixes/helpers.ts

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
/* @internal */
22
namespace ts.codefix {
3-
export function createJsonPropertyAssignment(name: string, initializer: Expression) {
4-
return createPropertyAssignment(createStringLiteral(name), initializer);
5-
}
6-
7-
export function findJsonProperty(obj: ObjectLiteralExpression, name: string): PropertyAssignment | undefined {
8-
return find(obj.properties, (p): p is PropertyAssignment => isPropertyAssignment(p) && !!p.name && isStringLiteral(p.name) && p.name.text === name);
9-
}
10-
113
/**
124
* Finds members of the resolved type that are missing in the class pointed to by class decl
135
* and generates source code for the missing members.
@@ -257,4 +249,46 @@ namespace ts.codefix {
257249
}
258250
return undefined;
259251
}
252+
253+
export function setJsonCompilerOptionValue(
254+
changeTracker: textChanges.ChangeTracker,
255+
configFile: TsConfigSourceFile,
256+
optionName: string,
257+
optionValue: Expression,
258+
) {
259+
const tsconfigObjectLiteral = getTsConfigObjectLiteralExpression(configFile);
260+
if (!tsconfigObjectLiteral) return undefined;
261+
262+
const compilerOptionsProperty = findJsonProperty(tsconfigObjectLiteral, "compilerOptions");
263+
if (compilerOptionsProperty === undefined) {
264+
changeTracker.insertNodeAtObjectStart(configFile, tsconfigObjectLiteral, createJsonPropertyAssignment(
265+
"compilerOptions",
266+
createObjectLiteral([
267+
createJsonPropertyAssignment(optionName, optionValue),
268+
])));
269+
return;
270+
}
271+
272+
const compilerOptions = compilerOptionsProperty.initializer;
273+
if (!isObjectLiteralExpression(compilerOptions)) {
274+
return;
275+
}
276+
277+
const optionProperty = findJsonProperty(compilerOptions, optionName);
278+
279+
if (optionProperty === undefined) {
280+
changeTracker.insertNodeAtObjectStart(configFile, compilerOptions, createJsonPropertyAssignment(optionName, optionValue));
281+
}
282+
else {
283+
changeTracker.replaceNode(configFile, optionProperty.initializer, optionValue);
284+
}
285+
}
286+
287+
export function createJsonPropertyAssignment(name: string, initializer: Expression) {
288+
return createPropertyAssignment(createStringLiteral(name), initializer);
289+
}
290+
291+
export function findJsonProperty(obj: ObjectLiteralExpression, name: string): PropertyAssignment | undefined {
292+
return find(obj.properties, (p): p is PropertyAssignment => isPropertyAssignment(p) && !!p.name && isStringLiteral(p.name) && p.name.text === name);
293+
}
260294
}

0 commit comments

Comments
 (0)