mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-30 01:04:49 -05:00
feat(36249): add quick-fix action to declare a property as private which starts from underscore (#36632)
This commit is contained in:
@@ -5113,6 +5113,10 @@
|
||||
"category": "Message",
|
||||
"code": 90034
|
||||
},
|
||||
"Declare private property '{0}'": {
|
||||
"category": "Message",
|
||||
"code": 90035
|
||||
},
|
||||
"Declare a private field named '{0}'.": {
|
||||
"category": "Message",
|
||||
"code": 90053
|
||||
|
||||
@@ -74,7 +74,7 @@ namespace ts.codefix {
|
||||
}
|
||||
else {
|
||||
const typeNode = getTypeNode(program.getTypeChecker(), parentDeclaration, token);
|
||||
addPropertyDeclaration(changes, declSourceFile, parentDeclaration, token.text, typeNode, makeStatic);
|
||||
addPropertyDeclaration(changes, declSourceFile, parentDeclaration, token.text, typeNode, makeStatic ? ModifierFlags.Static : 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -211,8 +211,17 @@ namespace ts.codefix {
|
||||
|
||||
function getActionsForAddMissingMemberInTypeScriptFile(context: CodeFixContext, declSourceFile: SourceFile, classDeclaration: ClassOrInterface, token: Identifier | PrivateIdentifier, makeStatic: boolean): CodeFixAction[] | undefined {
|
||||
const typeNode = getTypeNode(context.program.getTypeChecker(), classDeclaration, token);
|
||||
const addProp = createAddPropertyDeclarationAction(context, declSourceFile, classDeclaration, makeStatic, token.text, typeNode);
|
||||
return makeStatic || isPrivateIdentifier(token) ? [addProp] : [addProp, createAddIndexSignatureAction(context, declSourceFile, classDeclaration, token.text, typeNode)];
|
||||
const actions: CodeFixAction[] = [createAddPropertyDeclarationAction(context, declSourceFile, classDeclaration, token.text, typeNode, makeStatic ? ModifierFlags.Static : 0)];
|
||||
if (makeStatic || isPrivateIdentifier(token)) {
|
||||
return actions;
|
||||
}
|
||||
|
||||
if (startsWithUnderscore(token.text)) {
|
||||
actions.unshift(createAddPropertyDeclarationAction(context, declSourceFile, classDeclaration, token.text, typeNode, ModifierFlags.Private));
|
||||
}
|
||||
|
||||
actions.push(createAddIndexSignatureAction(context, declSourceFile, classDeclaration, token.text, typeNode));
|
||||
return actions;
|
||||
}
|
||||
|
||||
function getTypeNode(checker: TypeChecker, classDeclaration: ClassOrInterface, token: Node) {
|
||||
@@ -230,15 +239,18 @@ namespace ts.codefix {
|
||||
return typeNode || createKeywordTypeNode(SyntaxKind.AnyKeyword);
|
||||
}
|
||||
|
||||
function createAddPropertyDeclarationAction(context: CodeFixContext, declSourceFile: SourceFile, classDeclaration: ClassOrInterface, makeStatic: boolean, tokenName: string, typeNode: TypeNode): CodeFixAction {
|
||||
const changes = textChanges.ChangeTracker.with(context, t => addPropertyDeclaration(t, declSourceFile, classDeclaration, tokenName, typeNode, makeStatic));
|
||||
return createCodeFixAction(fixName, changes, [makeStatic ? Diagnostics.Declare_static_property_0 : Diagnostics.Declare_property_0, tokenName], fixId, Diagnostics.Add_all_missing_members);
|
||||
function createAddPropertyDeclarationAction(context: CodeFixContext, declSourceFile: SourceFile, classDeclaration: ClassOrInterface, tokenName: string, typeNode: TypeNode, modifierFlags: ModifierFlags): CodeFixAction {
|
||||
const changes = textChanges.ChangeTracker.with(context, t => addPropertyDeclaration(t, declSourceFile, classDeclaration, tokenName, typeNode, modifierFlags));
|
||||
if (modifierFlags & ModifierFlags.Private) {
|
||||
return createCodeFixActionWithoutFixAll(fixName, changes, [Diagnostics.Declare_private_property_0, tokenName]);
|
||||
}
|
||||
return createCodeFixAction(fixName, changes, [modifierFlags & ModifierFlags.Static ? Diagnostics.Declare_static_property_0 : Diagnostics.Declare_property_0, tokenName], fixId, Diagnostics.Add_all_missing_members);
|
||||
}
|
||||
|
||||
function addPropertyDeclaration(changeTracker: textChanges.ChangeTracker, declSourceFile: SourceFile, classDeclaration: ClassOrInterface, tokenName: string, typeNode: TypeNode, makeStatic: boolean): void {
|
||||
function addPropertyDeclaration(changeTracker: textChanges.ChangeTracker, declSourceFile: SourceFile, classDeclaration: ClassOrInterface, tokenName: string, typeNode: TypeNode, modifierFlags: ModifierFlags): void {
|
||||
const property = createProperty(
|
||||
/*decorators*/ undefined,
|
||||
/*modifiers*/ makeStatic ? [createToken(SyntaxKind.StaticKeyword)] : undefined,
|
||||
/*modifiers*/ modifierFlags ? createNodeArray(createModifiersFromModifierFlags(modifierFlags)) : undefined,
|
||||
tokenName,
|
||||
/*questionToken*/ undefined,
|
||||
typeNode,
|
||||
|
||||
@@ -112,10 +112,6 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
|
||||
return modifiers && createNodeArray(modifiers);
|
||||
}
|
||||
|
||||
function startsWithUnderscore(name: string): boolean {
|
||||
return name.charCodeAt(0) === CharacterCodes._;
|
||||
}
|
||||
|
||||
function getConvertibleFieldAtPosition(context: RefactorContext): Info | undefined {
|
||||
const { file, startPosition, endPosition } = context;
|
||||
|
||||
|
||||
@@ -2760,5 +2760,9 @@ namespace ts {
|
||||
return symbol.name;
|
||||
}
|
||||
|
||||
export function startsWithUnderscore(name: string): boolean {
|
||||
return name.charCodeAt(0) === CharacterCodes._;
|
||||
}
|
||||
|
||||
// #endregion
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user