|
1 |
| -/* @internal */ |
2 |
| -namespace ts.codefix { |
3 |
| - const errorCodes = [ |
4 |
| - Diagnostics._0_is_defined_as_an_accessor_in_class_1_but_is_overridden_here_in_2_as_an_instance_property.code, |
5 |
| - Diagnostics._0_is_defined_as_a_property_in_class_1_but_is_overridden_here_in_2_as_an_accessor.code, |
6 |
| - ]; |
7 |
| - const fixId = "fixPropertyOverrideAccessor"; |
8 |
| - registerCodeFix({ |
9 |
| - errorCodes, |
10 |
| - getCodeActions(context) { |
11 |
| - const r = doChange(context); |
12 |
| - if (r) { |
13 |
| - return [createCodeFixAction(fixId, r.edits, Diagnostics.Generate_get_and_set_accessors, fixId, Diagnostics.Generate_get_and_set_accessors_for_all_overriding_properties)]; |
14 |
| - } |
15 |
| - }, |
16 |
| - fixIds: [fixId], |
17 |
| - |
18 |
| - getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => { |
19 |
| - const codeFixContext = { ...context, span: { start: diag.start, length: diag.length }, errorCode: diag.code }; |
20 |
| - const r = doChange(codeFixContext) |
21 |
| - if (r) { |
22 |
| - for (const e of r.edits) { |
23 |
| - changes.pushRaw(context.sourceFile, e) |
24 |
| - } |
25 |
| - } |
26 |
| - }), |
27 |
| - }); |
28 |
| - |
29 |
| - function doChange(context: CodeFixContext) { |
30 |
| - let startPosition: number |
31 |
| - let endPosition: number |
32 |
| - if (context.errorCode === Diagnostics._0_is_defined_as_an_accessor_in_class_1_but_is_overridden_here_in_2_as_an_instance_property.code) { |
33 |
| - startPosition = context.span.start |
34 |
| - endPosition = context.span.start + context.span.length |
35 |
| - } |
36 |
| - else if (context.errorCode === Diagnostics._0_is_defined_as_a_property_in_class_1_but_is_overridden_here_in_2_as_an_accessor.code) { |
37 |
| - // TODO: A lot of these should be bails instead of asserts |
38 |
| - const checker = context.program.getTypeChecker() |
39 |
| - const node = getTokenAtPosition(context.sourceFile, context.span.start).parent; |
40 |
| - Debug.assert(isAccessor(node), "it wasn't an accessor"); |
41 |
| - const name = unescapeLeadingUnderscores(getTextOfPropertyName(node.name)) |
42 |
| - const containingClass = node.parent; |
43 |
| - Debug.assert(isClassLike(containingClass), "it wasn't classlike") |
44 |
| - const bases = getAllSupers(containingClass, checker) |
45 |
| - const base = singleOrUndefined(bases) |
46 |
| - Debug.assert(!!base, "Porbably more than one super:" + bases.length) |
47 |
| - const baseType = checker.getTypeAtLocation(base) |
48 |
| - const baseProp = checker.getPropertyOfType(baseType, name) |
49 |
| - Debug.assert(!!baseProp, "Couldn't find base property for " + name) |
50 |
| - startPosition = baseProp.valueDeclaration.pos |
51 |
| - endPosition = baseProp.valueDeclaration.end |
52 |
| - } |
53 |
| - else { |
54 |
| - Debug.fail("fixPropertyOverrideAccessor codefix got unexpected error code " + context.errorCode); |
55 |
| - } |
56 |
| - const refactorContext = { ...context, file: context.sourceFile, startPosition, endPosition } |
57 |
| - return getEditsForAction(refactorContext, Diagnostics.Generate_get_and_set_accessors.message); |
58 |
| - } |
59 |
| -} |
| 1 | +/* @internal */ |
| 2 | +namespace ts.codefix { |
| 3 | + const errorCodes = [ |
| 4 | + Diagnostics._0_is_defined_as_an_accessor_in_class_1_but_is_overridden_here_in_2_as_an_instance_property.code, |
| 5 | + Diagnostics._0_is_defined_as_a_property_in_class_1_but_is_overridden_here_in_2_as_an_accessor.code, |
| 6 | + ]; |
| 7 | + const fixId = "fixPropertyOverrideAccessor"; |
| 8 | + registerCodeFix({ |
| 9 | + errorCodes, |
| 10 | + getCodeActions(context) { |
| 11 | + const edits = doChange(context.sourceFile, context.span.start, context.span.length, context.errorCode, context); |
| 12 | + if (edits) { |
| 13 | + return [createCodeFixAction(fixId, edits, Diagnostics.Generate_get_and_set_accessors, fixId, Diagnostics.Generate_get_and_set_accessors_for_all_overriding_properties)]; |
| 14 | + } |
| 15 | + }, |
| 16 | + fixIds: [fixId], |
| 17 | + |
| 18 | + getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => { |
| 19 | + const edits = doChange(diag.file, diag.start, diag.length, diag.code, context); |
| 20 | + if (edits) { |
| 21 | + for (const edit of edits) { |
| 22 | + changes.pushRaw(context.sourceFile, edit); |
| 23 | + } |
| 24 | + } |
| 25 | + }), |
| 26 | + }); |
| 27 | + |
| 28 | + function doChange(file: SourceFile, start: number, length: number, code: number, context: CodeFixContext | CodeFixAllContext) { |
| 29 | + let startPosition: number; |
| 30 | + let endPosition: number; |
| 31 | + if (code === Diagnostics._0_is_defined_as_an_accessor_in_class_1_but_is_overridden_here_in_2_as_an_instance_property.code) { |
| 32 | + startPosition = start; |
| 33 | + endPosition = start + length; |
| 34 | + } |
| 35 | + else if (code === Diagnostics._0_is_defined_as_a_property_in_class_1_but_is_overridden_here_in_2_as_an_accessor.code) { |
| 36 | + const checker = context.program.getTypeChecker(); |
| 37 | + const node = getTokenAtPosition(file, start).parent; |
| 38 | + Debug.assert(isAccessor(node), "error span of fixPropertyOverrideAccessor should only be on an accessor"); |
| 39 | + const containingClass = node.parent; |
| 40 | + Debug.assert(isClassLike(containingClass), "erroneous accessors should only be inside classes"); |
| 41 | + const base = singleOrUndefined(getAllSupers(containingClass, checker)); |
| 42 | + if (!base) return []; |
| 43 | + |
| 44 | + const name = unescapeLeadingUnderscores(getTextOfPropertyName(node.name)); |
| 45 | + const baseProp = checker.getPropertyOfType(checker.getTypeAtLocation(base), name); |
| 46 | + if (!baseProp || !baseProp.valueDeclaration) return []; |
| 47 | + |
| 48 | + startPosition = baseProp.valueDeclaration.pos; |
| 49 | + endPosition = baseProp.valueDeclaration.end; |
| 50 | + } |
| 51 | + else { |
| 52 | + Debug.fail("fixPropertyOverrideAccessor codefix got unexpected error code " + code); |
| 53 | + } |
| 54 | + return generateAccessorFromProperty(file, startPosition, endPosition, context, Diagnostics.Generate_get_and_set_accessors.message); |
| 55 | + } |
| 56 | +} |
0 commit comments