Skip to content

Commit fef2866

Browse files
author
Andy
authored
generateGetAccessorAndSetAccessor: Fix typos and use type predicate (#23310)
1 parent a004571 commit fef2866

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

src/compiler/core.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3119,8 +3119,8 @@ namespace ts {
31193119
return (arg: T) => f(arg) && g(arg);
31203120
}
31213121

3122-
export function or<T>(f: (arg: T) => boolean, g: (arg: T) => boolean, ...others: ((arg: T) => boolean)[]) {
3123-
return (arg: T) => f(arg) || g(arg) || others.some(f => f(arg));
3122+
export function or<T>(f: (arg: T) => boolean, g: (arg: T) => boolean): (arg: T) => boolean {
3123+
return arg => f(arg) || g(arg);
31243124
}
31253125

31263126
export function assertTypeIsNever(_: never): void { } // tslint:disable-line no-empty

src/services/refactors/generateGetAccessorAndSetAccessor.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
44
const actionDescription = Diagnostics.Generate_get_and_set_accessors.message;
55
registerRefactor(actionName, { getEditsForAction, getAvailableActions });
66

7-
type AccepedDeclaration = ParameterDeclaration | PropertyDeclaration | PropertyAssignment;
8-
type AccepedNameType = Identifier | StringLiteral;
9-
type ContainerDeclation = ClassLikeDeclaration | ObjectLiteralExpression;
7+
type AcceptedDeclaration = ParameterDeclaration | PropertyDeclaration | PropertyAssignment;
8+
type AcceptedNameType = Identifier | StringLiteral;
9+
type ContainerDeclaration = ClassLikeDeclaration | ObjectLiteralExpression;
1010

1111
interface DeclarationInfo {
12-
container: ContainerDeclation;
12+
container: ContainerDeclaration;
1313
isStatic: boolean;
1414
type: TypeNode | undefined;
1515
}
1616

1717
interface Info extends DeclarationInfo {
18-
declaration: AccepedDeclaration;
19-
fieldName: AccepedNameType;
20-
accessorName: AccepedNameType;
18+
declaration: AcceptedDeclaration;
19+
fieldName: AcceptedNameType;
20+
accessorName: AcceptedNameType;
2121
}
2222

2323
function getAvailableActions(context: RefactorContext): ApplicableRefactorInfo[] | undefined {
@@ -65,20 +65,24 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
6565
return { renameFilename, renameLocation, edits };
6666
}
6767

68-
function isConvertableName (name: DeclarationName): name is AccepedNameType {
68+
function isConvertableName (name: DeclarationName): name is AcceptedNameType {
6969
return isIdentifier(name) || isStringLiteral(name);
7070
}
7171

72-
function createPropertyName (name: string, originalName: AccepedNameType) {
72+
function isAcceptedDeclaration(node: Node): node is AcceptedDeclaration {
73+
return isParameterPropertyDeclaration(node) || isPropertyDeclaration(node) || isPropertyAssignment(node);
74+
}
75+
76+
function createPropertyName (name: string, originalName: AcceptedNameType) {
7377
return isIdentifier(originalName) ? createIdentifier(name) : createLiteral(name);
7478
}
7579

76-
function createAccessorAccessExpression (fieldName: AccepedNameType, isStatic: boolean, container: ContainerDeclation) {
80+
function createAccessorAccessExpression (fieldName: AcceptedNameType, isStatic: boolean, container: ContainerDeclaration) {
7781
const leftHead = isStatic ? (<ClassLikeDeclaration>container).name : createThis();
7882
return isIdentifier(fieldName) ? createPropertyAccess(leftHead, fieldName) : createElementAccess(leftHead, createLiteral(fieldName));
7983
}
8084

81-
function getAccessorModifiers(isJS: boolean, declaration: AccepedDeclaration, isStatic: boolean, isClassLike: boolean): NodeArray<Modifier> | undefined {
85+
function getAccessorModifiers(isJS: boolean, declaration: AcceptedDeclaration, isStatic: boolean, isClassLike: boolean): NodeArray<Modifier> | undefined {
8286
if (!isClassLike) return undefined;
8387

8488
if (!declaration.modifiers || getModifierFlags(declaration) & ModifierFlags.Private) {
@@ -129,7 +133,7 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
129133
};
130134
}
131135

132-
function getDeclarationInfo(declaration: AccepedDeclaration) {
136+
function getDeclarationInfo(declaration: AcceptedDeclaration) {
133137
if (isPropertyDeclaration(declaration)) {
134138
return getPropertyDeclarationInfo(declaration);
135139
}
@@ -143,7 +147,7 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
143147

144148
function getConvertibleFieldAtPosition(file: SourceFile, startPosition: number): Info | undefined {
145149
const node = getTokenAtPosition(file, startPosition, /*includeJsDocComment*/ false);
146-
const declaration = <AccepedDeclaration>findAncestor(node.parent, or(isParameterPropertyDeclaration, isPropertyDeclaration, isPropertyAssignment));
150+
const declaration = findAncestor(node.parent, isAcceptedDeclaration);
147151
// make sure propertyDeclaration have AccessibilityModifier or Static Modifier
148152
const meaning = ModifierFlags.AccessibilityModifier | ModifierFlags.Static;
149153
if (!declaration || !isConvertableName(declaration.name) || (getModifierFlags(declaration) | meaning) !== meaning) return undefined;
@@ -160,7 +164,7 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
160164
};
161165
}
162166

163-
function generateGetAccessor(fieldName: AccepedNameType, accessorName: AccepedNameType, type: TypeNode, modifiers: ModifiersArray | undefined, isStatic: boolean, container: ContainerDeclation) {
167+
function generateGetAccessor(fieldName: AcceptedNameType, accessorName: AcceptedNameType, type: TypeNode, modifiers: ModifiersArray | undefined, isStatic: boolean, container: ContainerDeclaration) {
164168
return createGetAccessor(
165169
/*decorators*/ undefined,
166170
modifiers,
@@ -175,7 +179,7 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
175179
);
176180
}
177181

178-
function generateSetAccessor(fieldName: AccepedNameType, accessorName: AccepedNameType, type: TypeNode, modifiers: ModifiersArray | undefined, isStatic: boolean, container: ContainerDeclation) {
182+
function generateSetAccessor(fieldName: AcceptedNameType, accessorName: AcceptedNameType, type: TypeNode, modifiers: ModifiersArray | undefined, isStatic: boolean, container: ContainerDeclaration) {
179183
return createSetAccessor(
180184
/*decorators*/ undefined,
181185
modifiers,
@@ -199,7 +203,7 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
199203
);
200204
}
201205

202-
function updatePropertyDeclaration(changeTracker: textChanges.ChangeTracker, file: SourceFile, declaration: PropertyDeclaration, fieldName: AccepedNameType, modifiers: ModifiersArray | undefined) {
206+
function updatePropertyDeclaration(changeTracker: textChanges.ChangeTracker, file: SourceFile, declaration: PropertyDeclaration, fieldName: AcceptedNameType, modifiers: ModifiersArray | undefined) {
203207
const property = updateProperty(
204208
declaration,
205209
declaration.decorators,
@@ -213,7 +217,7 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
213217
changeTracker.replaceNode(file, declaration, property);
214218
}
215219

216-
function updateParameterPropertyDeclaration(changeTracker: textChanges.ChangeTracker, file: SourceFile, declaration: ParameterDeclaration, fieldName: AccepedNameType, modifiers: ModifiersArray | undefined, classLikeContainer: ClassLikeDeclaration) {
220+
function updateParameterPropertyDeclaration(changeTracker: textChanges.ChangeTracker, file: SourceFile, declaration: ParameterDeclaration, fieldName: AcceptedNameType, modifiers: ModifiersArray | undefined, classLikeContainer: ClassLikeDeclaration) {
217221
const property = createProperty(
218222
declaration.decorators,
219223
modifiers,
@@ -227,12 +231,12 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
227231
changeTracker.deleteNodeInList(file, declaration);
228232
}
229233

230-
function updatePropertyAssignmentDeclaration (changeTracker: textChanges.ChangeTracker, file: SourceFile, declaration: PropertyAssignment, fieldName: AccepedNameType) {
234+
function updatePropertyAssignmentDeclaration (changeTracker: textChanges.ChangeTracker, file: SourceFile, declaration: PropertyAssignment, fieldName: AcceptedNameType) {
231235
const assignment = updatePropertyAssignment(declaration, fieldName, declaration.initializer);
232236
changeTracker.replacePropertyAssignment(file, declaration, assignment);
233237
}
234238

235-
function updateFieldDeclaration(changeTracker: textChanges.ChangeTracker, file: SourceFile, declaration: AccepedDeclaration, fieldName: AccepedNameType, modifiers: ModifiersArray | undefined, container: ContainerDeclation) {
239+
function updateFieldDeclaration(changeTracker: textChanges.ChangeTracker, file: SourceFile, declaration: AcceptedDeclaration, fieldName: AcceptedNameType, modifiers: ModifiersArray | undefined, container: ContainerDeclaration) {
236240
if (isPropertyDeclaration(declaration)) {
237241
updatePropertyDeclaration(changeTracker, file, declaration, fieldName, modifiers);
238242
}
@@ -244,7 +248,7 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
244248
}
245249
}
246250

247-
function insertAccessor(changeTracker: textChanges.ChangeTracker, file: SourceFile, accessor: AccessorDeclaration, declaration: AccepedDeclaration, container: ContainerDeclation) {
251+
function insertAccessor(changeTracker: textChanges.ChangeTracker, file: SourceFile, accessor: AccessorDeclaration, declaration: AcceptedDeclaration, container: ContainerDeclaration) {
248252
isParameterPropertyDeclaration(declaration)
249253
? changeTracker.insertNodeAtClassStart(file, <ClassLikeDeclaration>container, accessor)
250254
: changeTracker.insertNodeAfter(file, declaration, accessor);

0 commit comments

Comments
 (0)