-
Notifications
You must be signed in to change notification settings - Fork 12.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. Fix lint. 2. Make code easier to read. 3. Turns some asserts into bails instead.
- Loading branch information
Showing
3 changed files
with
307 additions
and
307 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,56 @@ | ||
/* @internal */ | ||
namespace ts.codefix { | ||
const errorCodes = [ | ||
Diagnostics._0_is_defined_as_an_accessor_in_class_1_but_is_overridden_here_in_2_as_an_instance_property.code, | ||
Diagnostics._0_is_defined_as_a_property_in_class_1_but_is_overridden_here_in_2_as_an_accessor.code, | ||
]; | ||
const fixId = "fixPropertyOverrideAccessor"; | ||
registerCodeFix({ | ||
errorCodes, | ||
getCodeActions(context) { | ||
const r = doChange(context); | ||
if (r) { | ||
return [createCodeFixAction(fixId, r.edits, Diagnostics.Generate_get_and_set_accessors, fixId, Diagnostics.Generate_get_and_set_accessors_for_all_overriding_properties)]; | ||
} | ||
}, | ||
fixIds: [fixId], | ||
|
||
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => { | ||
const codeFixContext = { ...context, span: { start: diag.start, length: diag.length }, errorCode: diag.code }; | ||
const r = doChange(codeFixContext) | ||
if (r) { | ||
for (const e of r.edits) { | ||
changes.pushRaw(context.sourceFile, e) | ||
} | ||
} | ||
}), | ||
}); | ||
|
||
function doChange(context: CodeFixContext) { | ||
let startPosition: number | ||
let endPosition: number | ||
if (context.errorCode === Diagnostics._0_is_defined_as_an_accessor_in_class_1_but_is_overridden_here_in_2_as_an_instance_property.code) { | ||
startPosition = context.span.start | ||
endPosition = context.span.start + context.span.length | ||
} | ||
else if (context.errorCode === Diagnostics._0_is_defined_as_a_property_in_class_1_but_is_overridden_here_in_2_as_an_accessor.code) { | ||
// TODO: A lot of these should be bails instead of asserts | ||
const checker = context.program.getTypeChecker() | ||
const node = getTokenAtPosition(context.sourceFile, context.span.start).parent; | ||
Debug.assert(isAccessor(node), "it wasn't an accessor"); | ||
const name = unescapeLeadingUnderscores(getTextOfPropertyName(node.name)) | ||
const containingClass = node.parent; | ||
Debug.assert(isClassLike(containingClass), "it wasn't classlike") | ||
const bases = getAllSupers(containingClass, checker) | ||
const base = singleOrUndefined(bases) | ||
Debug.assert(!!base, "Porbably more than one super:" + bases.length) | ||
const baseType = checker.getTypeAtLocation(base) | ||
const baseProp = checker.getPropertyOfType(baseType, name) | ||
Debug.assert(!!baseProp, "Couldn't find base property for " + name) | ||
startPosition = baseProp.valueDeclaration.pos | ||
endPosition = baseProp.valueDeclaration.end | ||
} | ||
else { | ||
Debug.fail("fixPropertyOverrideAccessor codefix got unexpected error code " + context.errorCode); | ||
} | ||
const refactorContext = { ...context, file: context.sourceFile, startPosition, endPosition } | ||
return getEditsForAction(refactorContext, Diagnostics.Generate_get_and_set_accessors.message); | ||
} | ||
} | ||
/* @internal */ | ||
namespace ts.codefix { | ||
const errorCodes = [ | ||
Diagnostics._0_is_defined_as_an_accessor_in_class_1_but_is_overridden_here_in_2_as_an_instance_property.code, | ||
Diagnostics._0_is_defined_as_a_property_in_class_1_but_is_overridden_here_in_2_as_an_accessor.code, | ||
]; | ||
const fixId = "fixPropertyOverrideAccessor"; | ||
registerCodeFix({ | ||
errorCodes, | ||
getCodeActions(context) { | ||
const edits = doChange(context.sourceFile, context.span.start, context.span.length, context.errorCode, context); | ||
if (edits) { | ||
return [createCodeFixAction(fixId, edits, Diagnostics.Generate_get_and_set_accessors, fixId, Diagnostics.Generate_get_and_set_accessors_for_all_overriding_properties)]; | ||
} | ||
}, | ||
fixIds: [fixId], | ||
|
||
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => { | ||
const edits = doChange(diag.file, diag.start, diag.length, diag.code, context); | ||
if (edits) { | ||
for (const edit of edits) { | ||
changes.pushRaw(context.sourceFile, edit); | ||
} | ||
} | ||
}), | ||
}); | ||
|
||
function doChange(file: SourceFile, start: number, length: number, code: number, context: CodeFixContext | CodeFixAllContext) { | ||
let startPosition: number; | ||
let endPosition: number; | ||
if (code === Diagnostics._0_is_defined_as_an_accessor_in_class_1_but_is_overridden_here_in_2_as_an_instance_property.code) { | ||
startPosition = start; | ||
endPosition = start + length; | ||
} | ||
else if (code === Diagnostics._0_is_defined_as_a_property_in_class_1_but_is_overridden_here_in_2_as_an_accessor.code) { | ||
const checker = context.program.getTypeChecker(); | ||
const node = getTokenAtPosition(file, start).parent; | ||
Debug.assert(isAccessor(node), "error span of fixPropertyOverrideAccessor should only be on an accessor"); | ||
const containingClass = node.parent; | ||
Debug.assert(isClassLike(containingClass), "erroneous accessors should only be inside classes"); | ||
const base = singleOrUndefined(getAllSupers(containingClass, checker)); | ||
if (!base) return []; | ||
|
||
const name = unescapeLeadingUnderscores(getTextOfPropertyName(node.name)); | ||
const baseProp = checker.getPropertyOfType(checker.getTypeAtLocation(base), name); | ||
if (!baseProp || !baseProp.valueDeclaration) return []; | ||
|
||
startPosition = baseProp.valueDeclaration.pos; | ||
endPosition = baseProp.valueDeclaration.end; | ||
} | ||
else { | ||
Debug.fail("fixPropertyOverrideAccessor codefix got unexpected error code " + code); | ||
} | ||
return generateAccessorFromProperty(file, startPosition, endPosition, context, Diagnostics.Generate_get_and_set_accessors.message); | ||
} | ||
} |
Oops, something went wrong.