-
Notifications
You must be signed in to change notification settings - Fork 1
Override codefix in js #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: exprement-override
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,23 +20,24 @@ namespace ts.codefix { | |
Diagnostics.This_parameter_property_must_be_rewritten_as_a_property_declaration_with_an_override_modifier_because_it_overrides_a_member_in_base_class_0.code | ||
]; | ||
|
||
const errorCodeFixIdMap: Record<number, [DiagnosticMessage, string | undefined, DiagnosticMessage | undefined]> = { | ||
const errorCodeFixIdMap: Record<number, [message: DiagnosticMessage, fixId: string, fixAllMessage: DiagnosticMessage, allowInjs: boolean]> = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please convert this into an object type that has the same property names you just added to the tuple. |
||
[Diagnostics.This_member_must_have_an_override_modifier_because_it_overrides_a_member_in_the_base_class_0.code]: [ | ||
Diagnostics.Add_override_modifier, fixAddOverrideId, Diagnostics.Add_all_override_modifier, | ||
Diagnostics.Add_override_modifier, fixAddOverrideId, Diagnostics.Add_all_override_modifier, true | ||
], | ||
[Diagnostics.This_member_cannot_have_an_override_modifier_because_its_containing_class_0_does_not_extend_another_class.code]: [ | ||
Diagnostics.Remove_override_modifier, fixRemoveOverrideId, Diagnostics.Remove_all_override_modifier | ||
Diagnostics.Remove_override_modifier, fixRemoveOverrideId, Diagnostics.Remove_all_override_modifier, true | ||
], | ||
[Diagnostics.This_member_cannot_have_an_override_modifier_because_it_is_implemented_an_abstract_method_that_declared_in_the_base_class_0.code]: [ | ||
Diagnostics.Remove_override_modifier, fixRemoveOverrideId, Diagnostics.Remove_all_override_modifier | ||
Diagnostics.Remove_override_modifier, fixRemoveOverrideId, Diagnostics.Remove_all_override_modifier, true | ||
], | ||
[Diagnostics.This_member_cannot_have_an_override_modifier_because_it_is_not_declared_in_the_base_class_0.code]: [ | ||
Diagnostics.Remove_override_modifier, fixRemoveOverrideId, Diagnostics.Remove_all_override_modifier | ||
Diagnostics.Remove_override_modifier, fixRemoveOverrideId, Diagnostics.Remove_all_override_modifier, true | ||
], | ||
[Diagnostics.This_parameter_property_must_be_rewritten_as_a_property_declaration_with_an_override_modifier_because_it_overrides_a_member_in_base_class_0.code]: [ | ||
Diagnostics.Convert_to_property_declaration_and_add_override_modifier, | ||
fixConvertToPropertyDeclarationId, | ||
Diagnostics.Convert_all_to_property_declaration_and_add_override_modifier | ||
Diagnostics.Convert_all_to_property_declaration_and_add_override_modifier, | ||
false | ||
] | ||
}; | ||
|
||
|
@@ -48,8 +49,8 @@ namespace ts.codefix { | |
const info = errorCodeFixIdMap[errorCode]; | ||
if (!info) return emptyArray; | ||
|
||
const [ descriptions, fixId, fixAllDescriptions ] = info; | ||
if (isSourceFileJS(sourceFile)) return emptyArray; | ||
const [ descriptions, fixId, fixAllDescriptions, allowInjs ] = info; | ||
if (!allowInjs && isSourceFileJS(sourceFile)) return emptyArray; | ||
const changes = textChanges.ChangeTracker.with(context, changes => dispatchChanges(changes, context, errorCode, span.start)); | ||
|
||
return [ | ||
|
@@ -61,7 +62,7 @@ namespace ts.codefix { | |
codeFixAll(context, errorCodes, (changes, diag) => { | ||
const { code, start, file } = diag; | ||
const info = errorCodeFixIdMap[code]; | ||
if (!info || info[1] !== context.fixId || isSourceFileJS(file)) { | ||
if (!info || info[1] !== context.fixId || !info[3] && isSourceFileJS(file)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not your fault, but |
||
return; | ||
} | ||
|
||
|
@@ -90,15 +91,24 @@ namespace ts.codefix { | |
|
||
function doAddOverrideModifierChange(changeTracker: textChanges.ChangeTracker, sourceFile: SourceFile, pos: number) { | ||
const classElement = findContainerClassElement(sourceFile, pos); | ||
changeTracker.insertModifierBefore(sourceFile, SyntaxKind.OverrideKeyword, classElement); | ||
if (isSourceFileJS(sourceFile)) { | ||
changeTracker.addJSDocTags(sourceFile, classElement, [ factory.createJSDocOverrideTag(/*tagName*/ undefined) ]); | ||
} | ||
else { | ||
changeTracker.insertModifierBefore(sourceFile, SyntaxKind.OverrideKeyword, classElement); | ||
} | ||
} | ||
|
||
function doRemoveOverrideModifierChange(changeTracker: textChanges.ChangeTracker, sourceFile: SourceFile, pos: number) { | ||
const classElement = findContainerClassElement(sourceFile, pos); | ||
const overrideModifier = classElement.modifiers && find(classElement.modifiers, modifier => modifier.kind === SyntaxKind.OverrideKeyword); | ||
Debug.assertIsDefined(overrideModifier); | ||
|
||
changeTracker.deleteModifier(sourceFile, overrideModifier); | ||
if (isSourceFileJS(sourceFile)) { | ||
changeTracker.filterJSDocTags(sourceFile, classElement, not(isJSDocOverrideTag)); | ||
} | ||
else { | ||
const overrideModifier = classElement.modifiers && find(classElement.modifiers, modifier => modifier.kind === SyntaxKind.OverrideKeyword); | ||
Debug.assertIsDefined(overrideModifier); | ||
changeTracker.deleteModifier(sourceFile, overrideModifier); | ||
} | ||
} | ||
|
||
function doConvertToPropertyDeclaration(changeTracker: textChanges.ChangeTracker, sourceFile: SourceFile, pos: number) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -436,6 +436,56 @@ namespace ts.textChanges { | |
this.insertNodeAt(sourceFile, fnStart, tag, { preserveLeadingWhitespace: false, suffix: this.newLineCharacter + indent }); | ||
} | ||
|
||
|
||
public addJSDocTags(sourceFile: SourceFile, parent: HasJSDoc, newTags: readonly JSDocTag[]): void { | ||
const comments = mapDefined(parent.jsDoc, j => j.comment); | ||
const oldTags = flatMapToMutable(parent.jsDoc, j => j.tags); | ||
const unmergedNewTags = newTags.filter(newTag => !oldTags || !oldTags.some((tag, i) => { | ||
const merged = this.tryMergeJsdocTags(tag, newTag); | ||
if (merged) oldTags[i] = merged; | ||
return !!merged; | ||
})); | ||
const tag = factory.createJSDocComment(comments.join("\n"), factory.createNodeArray([...(oldTags || emptyArray), ...unmergedNewTags])); | ||
const jsDocNode = parent.kind === SyntaxKind.ArrowFunction ? this.getJsDocNodeForArrowFunction(parent) : parent; | ||
jsDocNode.jsDoc = parent.jsDoc; | ||
jsDocNode.jsDocCache = parent.jsDocCache; | ||
this.insertJsdocCommentBefore(sourceFile, jsDocNode, tag); | ||
} | ||
|
||
public filterJSDocTags(sourceFile: SourceFile, parent: HasJSDoc, cb: (tag: JSDocTag) => boolean): void { | ||
const comments = mapDefined(parent.jsDoc, j => j.comment); | ||
const oldTags = filter(flatMapToMutable(parent.jsDoc, j => j.tags), cb); | ||
const tag = factory.createJSDocComment(comments.join("\n"), factory.createNodeArray([...(oldTags || emptyArray)])); | ||
const jsDocNode = parent.kind === SyntaxKind.ArrowFunction ? this.getJsDocNodeForArrowFunction(parent) : parent; | ||
jsDocNode.jsDoc = parent.jsDoc; | ||
jsDocNode.jsDocCache = parent.jsDocCache; | ||
this.insertJsdocCommentBefore(sourceFile, jsDocNode, tag); | ||
} | ||
|
||
public getJsDocNodeForArrowFunction(signature: ArrowFunction): HasJSDoc { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this doesn't need to be a method, especially a public one. |
||
if (signature.parent.kind === SyntaxKind.PropertyDeclaration) { | ||
return <HasJSDoc>signature.parent; | ||
} | ||
return <HasJSDoc>signature.parent.parent; | ||
} | ||
|
||
public tryMergeJsdocTags(oldTag: JSDocTag, newTag: JSDocTag): JSDocTag | undefined { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
||
if (oldTag.kind !== newTag.kind) { | ||
return undefined; | ||
} | ||
switch (oldTag.kind) { | ||
case SyntaxKind.JSDocParameterTag: { | ||
const oldParam = oldTag as JSDocParameterTag; | ||
const newParam = newTag as JSDocParameterTag; | ||
return isIdentifier(oldParam.name) && isIdentifier(newParam.name) && oldParam.name.escapedText === newParam.name.escapedText | ||
? factory.createJSDocParameterTag(/*tagName*/ undefined, newParam.name, /*isBracketed*/ false, newParam.typeExpression, newParam.isNameFirst, oldParam.comment) | ||
: undefined; | ||
} | ||
case SyntaxKind.JSDocReturnTag: | ||
return factory.createJSDocReturnTag(/*tagName*/ undefined, (newTag as JSDocReturnTag).typeExpression, oldTag.comment); | ||
} | ||
} | ||
|
||
public replaceRangeWithText(sourceFile: SourceFile, range: TextRange, text: string): void { | ||
this.changes.push({ kind: ChangeKind.Text, sourceFile, range, text }); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
// @allowJs: true | ||
// @checkJs: true | ||
// @noEmit: true | ||
// @noImplicitOverride: true | ||
// @filename: a.js | ||
//// class B { } | ||
//// class D extends B { | ||
//// /** | ||
//// * @public | ||
//// * @override | ||
//// */ | ||
//// foo (v) {} | ||
//// } | ||
|
||
verify.codeFix({ | ||
description: "Remove 'override' modifier", | ||
index: 0, | ||
newFileContent: | ||
`class B { } | ||
class D extends B { | ||
/** | ||
* @public | ||
*/ | ||
foo (v) {} | ||
}`, | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.