Skip to content

generateGetAccessorAndSetAccessor: Fix typos and use type predicate #23310

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

Merged
1 commit merged into from
Apr 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3119,8 +3119,8 @@ namespace ts {
return (arg: T) => f(arg) && g(arg);
}

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

export function assertTypeIsNever(_: never): void { } // tslint:disable-line no-empty
Expand Down
44 changes: 24 additions & 20 deletions src/services/refactors/generateGetAccessorAndSetAccessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
const actionDescription = Diagnostics.Generate_get_and_set_accessors.message;
registerRefactor(actionName, { getEditsForAction, getAvailableActions });

type AccepedDeclaration = ParameterDeclaration | PropertyDeclaration | PropertyAssignment;
type AccepedNameType = Identifier | StringLiteral;
type ContainerDeclation = ClassLikeDeclaration | ObjectLiteralExpression;
type AcceptedDeclaration = ParameterDeclaration | PropertyDeclaration | PropertyAssignment;
type AcceptedNameType = Identifier | StringLiteral;
type ContainerDeclaration = ClassLikeDeclaration | ObjectLiteralExpression;

interface DeclarationInfo {
container: ContainerDeclation;
container: ContainerDeclaration;
isStatic: boolean;
type: TypeNode | undefined;
}

interface Info extends DeclarationInfo {
declaration: AccepedDeclaration;
fieldName: AccepedNameType;
accessorName: AccepedNameType;
declaration: AcceptedDeclaration;
fieldName: AcceptedNameType;
accessorName: AcceptedNameType;
}

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

function isConvertableName (name: DeclarationName): name is AccepedNameType {
function isConvertableName (name: DeclarationName): name is AcceptedNameType {
return isIdentifier(name) || isStringLiteral(name);
}

function createPropertyName (name: string, originalName: AccepedNameType) {
function isAcceptedDeclaration(node: Node): node is AcceptedDeclaration {
return isParameterPropertyDeclaration(node) || isPropertyDeclaration(node) || isPropertyAssignment(node);
}

function createPropertyName (name: string, originalName: AcceptedNameType) {
return isIdentifier(originalName) ? createIdentifier(name) : createLiteral(name);
}

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

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

if (!declaration.modifiers || getModifierFlags(declaration) & ModifierFlags.Private) {
Expand Down Expand Up @@ -129,7 +133,7 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
};
}

function getDeclarationInfo(declaration: AccepedDeclaration) {
function getDeclarationInfo(declaration: AcceptedDeclaration) {
if (isPropertyDeclaration(declaration)) {
return getPropertyDeclarationInfo(declaration);
}
Expand All @@ -143,7 +147,7 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {

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

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

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

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

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

function updatePropertyAssignmentDeclaration (changeTracker: textChanges.ChangeTracker, file: SourceFile, declaration: PropertyAssignment, fieldName: AccepedNameType) {
function updatePropertyAssignmentDeclaration (changeTracker: textChanges.ChangeTracker, file: SourceFile, declaration: PropertyAssignment, fieldName: AcceptedNameType) {
const assignment = updatePropertyAssignment(declaration, fieldName, declaration.initializer);
changeTracker.replacePropertyAssignment(file, declaration, assignment);
}

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

function insertAccessor(changeTracker: textChanges.ChangeTracker, file: SourceFile, accessor: AccessorDeclaration, declaration: AccepedDeclaration, container: ContainerDeclation) {
function insertAccessor(changeTracker: textChanges.ChangeTracker, file: SourceFile, accessor: AccessorDeclaration, declaration: AcceptedDeclaration, container: ContainerDeclaration) {
isParameterPropertyDeclaration(declaration)
? changeTracker.insertNodeAtClassStart(file, <ClassLikeDeclaration>container, accessor)
: changeTracker.insertNodeAfter(file, declaration, accessor);
Expand Down