|
1 | 1 | /* @internal */ |
2 | 2 | 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 | | - |
11 | 3 | /** |
12 | 4 | * Finds members of the resolved type that are missing in the class pointed to by class decl |
13 | 5 | * and generates source code for the missing members. |
@@ -257,4 +249,46 @@ namespace ts.codefix { |
257 | 249 | } |
258 | 250 | return undefined; |
259 | 251 | } |
| 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 | + } |
260 | 294 | } |
0 commit comments