From d883dd5ca49fa4904c1fd64c178f662537ec0f9a Mon Sep 17 00:00:00 2001 From: Alexander Rose Date: Fri, 21 Feb 2020 10:27:58 +0100 Subject: [PATCH 01/13] Added support for refactorings that share an AST traversal run. --- src/action-providers.ts | 81 +++++++++++++++++-- src/refactorings/simplify-ternary/index.ts | 13 ++- .../simplify-ternary/simplify-ternary.ts | 10 +-- src/types.ts | 38 +++++++-- 4 files changed, 118 insertions(+), 24 deletions(-) diff --git a/src/action-providers.ts b/src/action-providers.ts index f73b4090b..f86580912 100644 --- a/src/action-providers.ts +++ b/src/action-providers.ts @@ -1,9 +1,9 @@ import * as vscode from "vscode"; import { createSelectionFromVSCode } from "./editor/adapters/vscode-editor"; -import { Selection } from "./editor/selection"; -import { RefactoringWithActionProvider } from "./types"; +import { RefactoringWithActionProvider, isLegacyActionProvider } from "./types"; import * as t from "./ast"; +import { Selection } from "./editor/selection"; export { RefactoringActionProvider }; @@ -21,18 +21,87 @@ class RefactoringActionProvider implements vscode.CodeActionProvider { const ast = t.parse(document.getText()); const selection = createSelectionFromVSCode(range); - return this.refactorings - .filter(refactoring => this.canPerform(refactoring, ast, selection)) - .map(refactoring => this.buildCodeActionFor(refactoring)); + const applicableRefactorings: RefactoringWithActionProvider[] = []; + + const onCanPeform = ( + path: t.NodePath, + refactoring: RefactoringWithActionProvider + ) => { + if (isLegacyActionProvider(refactoring.actionProvider)) { + return; + } + + if (refactoring.actionProvider.updateMessage) { + refactoring.actionProvider.updateMessage(path); + } + + applicableRefactorings.push(refactoring); + }; + + t.traverseAST(ast, { + enter: (path: t.NodePath) => { + this.refactorings.forEach(refactoring => + this.canPerform(refactoring, path, selection, onCanPeform) + ); + } + }); + + const applicableLegacyRefactorings = this.refactorings.filter(refactoring => + this.canPerformLegacy(refactoring, ast, selection) + ); + + return [...applicableRefactorings, ...applicableLegacyRefactorings].map( + refactoring => this.buildCodeActionFor(refactoring) + ); } private canPerform( + refactoring: RefactoringWithActionProvider, + path: t.NodePath, + selection: Selection, + onCanPerform: ( + matchedPath: t.NodePath, + refactoring: RefactoringWithActionProvider + ) => void + ) { + if (isLegacyActionProvider(refactoring.actionProvider)) { + return; + } + + const visitor: t.Visitor = refactoring.actionProvider.createVisitor( + selection, + path => onCanPerform(path, refactoring), + refactoring + ); + + this.visit(visitor, path); + } + + private visit(visitor: any, path: t.NodePath) { + const node: t.Node = path.node; + + try { + if (typeof visitor[node.type] === "function") { + visitor[node.type](path); + } else if (typeof visitor[node.type] === "object") { + visitor[node.type].enter(path); + } + } catch (_) { + // Silently fail, we don't care why it failed (e.g. code can't be parsed). + } + } + + private canPerformLegacy( refactoring: RefactoringWithActionProvider, ast: t.AST, selection: Selection ) { try { - return refactoring.actionProvider.canPerform(ast, selection); + return ( + isLegacyActionProvider(refactoring.actionProvider) && + typeof refactoring.actionProvider.canPerform === "function" && + refactoring.actionProvider.canPerform(ast, selection) + ); } catch (_) { // Silently fail, we don't care why it failed (e.g. code can't be parsed). return false; diff --git a/src/refactorings/simplify-ternary/index.ts b/src/refactorings/simplify-ternary/index.ts index ada785bd3..e163e8435 100644 --- a/src/refactorings/simplify-ternary/index.ts +++ b/src/refactorings/simplify-ternary/index.ts @@ -1,4 +1,8 @@ -import { simplifyTernary, hasTernaryToSimplify } from "./simplify-ternary"; +import { + simplifyTernary, + createTernaryToSimplifyVisitor +} from "./simplify-ternary"; +import * as t from "../../ast"; import { RefactoringWithActionProvider } from "../../types"; @@ -10,7 +14,12 @@ const config: RefactoringWithActionProvider = { }, actionProvider: { message: "Simplify ternary", - canPerform: hasTernaryToSimplify + createVisitor: createTernaryToSimplifyVisitor, + updateMessage(path: t.NodePath): void { + this.message = `Simplify ternary (for demo purposes only: ${ + path.node.type + })`; + } } }; diff --git a/src/refactorings/simplify-ternary/simplify-ternary.ts b/src/refactorings/simplify-ternary/simplify-ternary.ts index 458a3256e..0838d8d1a 100644 --- a/src/refactorings/simplify-ternary/simplify-ternary.ts +++ b/src/refactorings/simplify-ternary/simplify-ternary.ts @@ -2,7 +2,7 @@ import { Editor, Code, ErrorReason } from "../../editor/editor"; import { Selection } from "../../editor/selection"; import * as t from "../../ast"; -export { simplifyTernary, hasTernaryToSimplify }; +export { simplifyTernary, createVisitor as createTernaryToSimplifyVisitor }; async function simplifyTernary( code: Code, @@ -19,14 +19,6 @@ async function simplifyTernary( await editor.write(updatedCode.code); } -function hasTernaryToSimplify(ast: t.AST, selection: Selection): boolean { - let result = false; - - t.traverseAST(ast, createVisitor(selection, () => (result = true))); - - return result; -} - function updateCode(ast: t.AST, selection: Selection): t.Transformed { return t.transformAST( ast, diff --git a/src/types.ts b/src/types.ts index 3c03b8923..484255072 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,8 +1,13 @@ import { Code, Editor } from "./editor/editor"; import { Selection } from "./editor/selection"; -import { AST } from "./ast"; +import { AST, Visitor, NodePath } from "./ast"; -export { Refactoring, RefactoringWithActionProvider, Operation }; +export { + Refactoring, + RefactoringWithActionProvider, + Operation, + isLegacyActionProvider +}; interface Refactoring { command: { @@ -11,17 +16,30 @@ interface Refactoring { }; } +interface ActionProvider { + message: string; + isPreferred?: boolean; + createVisitor: ( + selection: Selection, + onMatch: (path: NodePath) => void, + refactoring: RefactoringWithActionProvider + ) => Visitor; + updateMessage?: (path: NodePath) => void; +} + +interface LegacyActionProvider { + message: string; + isPreferred?: boolean; + canPerform: (ast: AST, selection: Selection) => boolean; +} + interface RefactoringWithActionProvider extends Refactoring { command: { key: string; title: string; operation: Operation; }; - actionProvider: { - message: string; - canPerform: (ast: AST, selection: Selection) => boolean; - isPreferred?: boolean; - }; + actionProvider: ActionProvider | LegacyActionProvider; } type Operation = ( @@ -29,3 +47,9 @@ type Operation = ( selection: Selection, write: Editor ) => Promise; + +function isLegacyActionProvider( + actionProvider: ActionProvider | LegacyActionProvider +): actionProvider is LegacyActionProvider { + return (actionProvider as LegacyActionProvider).canPerform !== undefined; +} From 010e6f21b0fea622036668b8d7921fea275b68c4 Mon Sep 17 00:00:00 2001 From: Alexander Rose Date: Mon, 2 Mar 2020 21:39:12 +0100 Subject: [PATCH 02/13] Remove unnecessary Co-Authored-By: Nicolas Carlo --- src/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types.ts b/src/types.ts index 484255072..071b3ab1e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -21,7 +21,7 @@ interface ActionProvider { isPreferred?: boolean; createVisitor: ( selection: Selection, - onMatch: (path: NodePath) => void, + onMatch: (path: NodePath) => void, refactoring: RefactoringWithActionProvider ) => Visitor; updateMessage?: (path: NodePath) => void; From 5d02f6edec17f78ffabeeec7e7f9a3390a1bc85f Mon Sep 17 00:00:00 2001 From: Alexander Rose Date: Mon, 2 Mar 2020 21:40:35 +0100 Subject: [PATCH 03/13] Remove unnecessary Co-Authored-By: Nicolas Carlo --- src/action-providers.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/action-providers.ts b/src/action-providers.ts index f86580912..5b0783263 100644 --- a/src/action-providers.ts +++ b/src/action-providers.ts @@ -24,7 +24,7 @@ class RefactoringActionProvider implements vscode.CodeActionProvider { const applicableRefactorings: RefactoringWithActionProvider[] = []; const onCanPeform = ( - path: t.NodePath, + path: t.NodePath, refactoring: RefactoringWithActionProvider ) => { if (isLegacyActionProvider(refactoring.actionProvider)) { From ac11c9f8bb4ec5db5b7b59d707d6cb77280886f2 Mon Sep 17 00:00:00 2001 From: Alexander Rose Date: Fri, 6 Mar 2020 17:35:12 +0100 Subject: [PATCH 04/13] Make updateMessage return a string instead of mutating this.message. --- src/action-providers.ts | 4 +++- src/refactorings/simplify-ternary/index.ts | 6 ++---- src/types.ts | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/action-providers.ts b/src/action-providers.ts index 5b0783263..4bb44a8eb 100644 --- a/src/action-providers.ts +++ b/src/action-providers.ts @@ -32,7 +32,9 @@ class RefactoringActionProvider implements vscode.CodeActionProvider { } if (refactoring.actionProvider.updateMessage) { - refactoring.actionProvider.updateMessage(path); + refactoring.actionProvider.message = refactoring.actionProvider.updateMessage( + path + ); } applicableRefactorings.push(refactoring); diff --git a/src/refactorings/simplify-ternary/index.ts b/src/refactorings/simplify-ternary/index.ts index e163e8435..44a5f6b8f 100644 --- a/src/refactorings/simplify-ternary/index.ts +++ b/src/refactorings/simplify-ternary/index.ts @@ -15,10 +15,8 @@ const config: RefactoringWithActionProvider = { actionProvider: { message: "Simplify ternary", createVisitor: createTernaryToSimplifyVisitor, - updateMessage(path: t.NodePath): void { - this.message = `Simplify ternary (for demo purposes only: ${ - path.node.type - })`; + updateMessage(path: t.NodePath): string { + return `Simplify ternary (for demo purposes only: ${path.node.type})`; } } }; diff --git a/src/types.ts b/src/types.ts index 071b3ab1e..3667820f5 100644 --- a/src/types.ts +++ b/src/types.ts @@ -24,7 +24,7 @@ interface ActionProvider { onMatch: (path: NodePath) => void, refactoring: RefactoringWithActionProvider ) => Visitor; - updateMessage?: (path: NodePath) => void; + updateMessage?: (path: NodePath) => string; } interface LegacyActionProvider { From 6f556ee4d4396b7478eaa8475e57e3083ac0ffbf Mon Sep 17 00:00:00 2001 From: Alexander Rose Date: Fri, 6 Mar 2020 18:09:00 +0100 Subject: [PATCH 05/13] Split legacy/non-legacy responsibilities in action-providers.ts. --- src/action-providers.ts | 63 ++++++++++++++++++++++++++--------------- src/types.ts | 23 +++++++++++++-- 2 files changed, 60 insertions(+), 26 deletions(-) diff --git a/src/action-providers.ts b/src/action-providers.ts index 4bb44a8eb..f8764c53b 100644 --- a/src/action-providers.ts +++ b/src/action-providers.ts @@ -1,7 +1,13 @@ import * as vscode from "vscode"; import { createSelectionFromVSCode } from "./editor/adapters/vscode-editor"; -import { RefactoringWithActionProvider, isLegacyActionProvider } from "./types"; +import { + RefactoringWithActionProvider, + ActionProvider, + LegacyActionProvider, + isRefactoringWithActionProvider, + isRefactoringWithLegacyActionProvider +} from "./types"; import * as t from "./ast"; import { Selection } from "./editor/selection"; @@ -21,16 +27,38 @@ class RefactoringActionProvider implements vscode.CodeActionProvider { const ast = t.parse(document.getText()); const selection = createSelectionFromVSCode(range); - const applicableRefactorings: RefactoringWithActionProvider[] = []; + return [ + ...this.findApplicableRefactorings(ast, selection), + ...this.findApplicableLegacyRefactorings(ast, selection) + ].map(refactoring => this.buildCodeActionFor(refactoring)); + } + + private findApplicableLegacyRefactorings(ast: t.File, selection: Selection) { + const legacyRefactorings = this.refactorings.filter( + isRefactoringWithLegacyActionProvider + ); + const applicableLegacyRefactorings = legacyRefactorings.filter( + refactoring => this.canPerformLegacy(refactoring, ast, selection) + ); + return applicableLegacyRefactorings; + } + + private findApplicableRefactorings( + ast: t.File, + selection: Selection + ): RefactoringWithActionProvider[] { + const refactorings = this.refactorings.filter( + isRefactoringWithActionProvider + ); + + const applicableRefactorings: RefactoringWithActionProvider< + ActionProvider + >[] = []; const onCanPeform = ( path: t.NodePath, - refactoring: RefactoringWithActionProvider + refactoring: RefactoringWithActionProvider ) => { - if (isLegacyActionProvider(refactoring.actionProvider)) { - return; - } - if (refactoring.actionProvider.updateMessage) { refactoring.actionProvider.message = refactoring.actionProvider.updateMessage( path @@ -42,34 +70,24 @@ class RefactoringActionProvider implements vscode.CodeActionProvider { t.traverseAST(ast, { enter: (path: t.NodePath) => { - this.refactorings.forEach(refactoring => + refactorings.forEach(refactoring => this.canPerform(refactoring, path, selection, onCanPeform) ); } }); - const applicableLegacyRefactorings = this.refactorings.filter(refactoring => - this.canPerformLegacy(refactoring, ast, selection) - ); - - return [...applicableRefactorings, ...applicableLegacyRefactorings].map( - refactoring => this.buildCodeActionFor(refactoring) - ); + return applicableRefactorings; } private canPerform( - refactoring: RefactoringWithActionProvider, + refactoring: RefactoringWithActionProvider, path: t.NodePath, selection: Selection, onCanPerform: ( matchedPath: t.NodePath, - refactoring: RefactoringWithActionProvider + refactoring: RefactoringWithActionProvider ) => void ) { - if (isLegacyActionProvider(refactoring.actionProvider)) { - return; - } - const visitor: t.Visitor = refactoring.actionProvider.createVisitor( selection, path => onCanPerform(path, refactoring), @@ -94,13 +112,12 @@ class RefactoringActionProvider implements vscode.CodeActionProvider { } private canPerformLegacy( - refactoring: RefactoringWithActionProvider, + refactoring: RefactoringWithActionProvider, ast: t.AST, selection: Selection ) { try { return ( - isLegacyActionProvider(refactoring.actionProvider) && typeof refactoring.actionProvider.canPerform === "function" && refactoring.actionProvider.canPerform(ast, selection) ); diff --git a/src/types.ts b/src/types.ts index 3667820f5..99149d657 100644 --- a/src/types.ts +++ b/src/types.ts @@ -6,7 +6,10 @@ export { Refactoring, RefactoringWithActionProvider, Operation, - isLegacyActionProvider + ActionProvider, + LegacyActionProvider, + isRefactoringWithActionProvider, + isRefactoringWithLegacyActionProvider }; interface Refactoring { @@ -33,13 +36,15 @@ interface LegacyActionProvider { canPerform: (ast: AST, selection: Selection) => boolean; } -interface RefactoringWithActionProvider extends Refactoring { +interface RefactoringWithActionProvider< + ActionProviderType = ActionProvider | LegacyActionProvider +> extends Refactoring { command: { key: string; title: string; operation: Operation; }; - actionProvider: ActionProvider | LegacyActionProvider; + actionProvider: ActionProviderType; } type Operation = ( @@ -53,3 +58,15 @@ function isLegacyActionProvider( ): actionProvider is LegacyActionProvider { return (actionProvider as LegacyActionProvider).canPerform !== undefined; } + +function isRefactoringWithLegacyActionProvider( + refactoring: RefactoringWithActionProvider +): refactoring is RefactoringWithActionProvider { + return isLegacyActionProvider(refactoring.actionProvider); +} + +function isRefactoringWithActionProvider( + refactoring: RefactoringWithActionProvider +): refactoring is RefactoringWithActionProvider { + return !isLegacyActionProvider(refactoring.actionProvider); +} From 7c25dbd7c9061e2d48dd94b12a6a6ee88e945c9c Mon Sep 17 00:00:00 2001 From: Alexander Rose Date: Fri, 6 Mar 2020 18:09:44 +0100 Subject: [PATCH 06/13] Inlined onCanPerform due to its strange name. --- src/action-providers.ts | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/action-providers.ts b/src/action-providers.ts index f8764c53b..078f859f7 100644 --- a/src/action-providers.ts +++ b/src/action-providers.ts @@ -55,23 +55,26 @@ class RefactoringActionProvider implements vscode.CodeActionProvider { ActionProvider >[] = []; - const onCanPeform = ( - path: t.NodePath, - refactoring: RefactoringWithActionProvider - ) => { - if (refactoring.actionProvider.updateMessage) { - refactoring.actionProvider.message = refactoring.actionProvider.updateMessage( - path - ); - } - - applicableRefactorings.push(refactoring); - }; - t.traverseAST(ast, { enter: (path: t.NodePath) => { refactorings.forEach(refactoring => - this.canPerform(refactoring, path, selection, onCanPeform) + this.canPerform( + refactoring, + path, + selection, + ( + path: t.NodePath, + refactoring: RefactoringWithActionProvider + ) => { + if (refactoring.actionProvider.updateMessage) { + refactoring.actionProvider.message = refactoring.actionProvider.updateMessage( + path + ); + } + + applicableRefactorings.push(refactoring); + } + ) ); } }); From 1dcacc2b9a3803d6b0460348ebc3a47f19880a49 Mon Sep 17 00:00:00 2001 From: Alexander Rose Date: Fri, 6 Mar 2020 18:34:34 +0100 Subject: [PATCH 07/13] Renamed canPerform to visitAndCheckApplicability, and onCanPerform to whenApplicable. --- src/action-providers.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/action-providers.ts b/src/action-providers.ts index 078f859f7..8dc29f339 100644 --- a/src/action-providers.ts +++ b/src/action-providers.ts @@ -58,7 +58,7 @@ class RefactoringActionProvider implements vscode.CodeActionProvider { t.traverseAST(ast, { enter: (path: t.NodePath) => { refactorings.forEach(refactoring => - this.canPerform( + this.visitAndCheckApplicability( refactoring, path, selection, @@ -82,18 +82,18 @@ class RefactoringActionProvider implements vscode.CodeActionProvider { return applicableRefactorings; } - private canPerform( + private visitAndCheckApplicability( refactoring: RefactoringWithActionProvider, path: t.NodePath, selection: Selection, - onCanPerform: ( + whenApplicable: ( matchedPath: t.NodePath, refactoring: RefactoringWithActionProvider ) => void ) { const visitor: t.Visitor = refactoring.actionProvider.createVisitor( selection, - path => onCanPerform(path, refactoring), + path => whenApplicable(path, refactoring), refactoring ); From 18c723bfcf82c7c4d0b4cc6dec3c0edc11aa4386 Mon Sep 17 00:00:00 2001 From: Nicolas Carlo Date: Wed, 18 Mar 2020 08:00:55 -0400 Subject: [PATCH 08/13] Remove updateMessage() test Confirmed it works! --- src/refactorings/simplify-ternary/index.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/refactorings/simplify-ternary/index.ts b/src/refactorings/simplify-ternary/index.ts index 44a5f6b8f..f5b34f20f 100644 --- a/src/refactorings/simplify-ternary/index.ts +++ b/src/refactorings/simplify-ternary/index.ts @@ -2,7 +2,6 @@ import { simplifyTernary, createTernaryToSimplifyVisitor } from "./simplify-ternary"; -import * as t from "../../ast"; import { RefactoringWithActionProvider } from "../../types"; @@ -14,10 +13,7 @@ const config: RefactoringWithActionProvider = { }, actionProvider: { message: "Simplify ternary", - createVisitor: createTernaryToSimplifyVisitor, - updateMessage(path: t.NodePath): string { - return `Simplify ternary (for demo purposes only: ${path.node.type})`; - } + createVisitor: createTernaryToSimplifyVisitor } }; From 1848d283bb5bac12e5d7ef312e4f0519739da9b5 Mon Sep 17 00:00:00 2001 From: Nicolas Carlo Date: Wed, 18 Mar 2020 08:02:14 -0400 Subject: [PATCH 09/13] Remove unnecessary `any` --- src/action-providers.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/action-providers.ts b/src/action-providers.ts index 8dc29f339..f5eb18ef2 100644 --- a/src/action-providers.ts +++ b/src/action-providers.ts @@ -56,7 +56,7 @@ class RefactoringActionProvider implements vscode.CodeActionProvider { >[] = []; t.traverseAST(ast, { - enter: (path: t.NodePath) => { + enter: (path: t.NodePath) => { refactorings.forEach(refactoring => this.visitAndCheckApplicability( refactoring, @@ -84,10 +84,10 @@ class RefactoringActionProvider implements vscode.CodeActionProvider { private visitAndCheckApplicability( refactoring: RefactoringWithActionProvider, - path: t.NodePath, + path: t.NodePath, selection: Selection, whenApplicable: ( - matchedPath: t.NodePath, + matchedPath: t.NodePath, refactoring: RefactoringWithActionProvider ) => void ) { @@ -100,7 +100,7 @@ class RefactoringActionProvider implements vscode.CodeActionProvider { this.visit(visitor, path); } - private visit(visitor: any, path: t.NodePath) { + private visit(visitor: any, path: t.NodePath) { const node: t.Node = path.node; try { From a2f92147107627e9a39b59e4793eee5c00b5693e Mon Sep 17 00:00:00 2001 From: Nicolas Carlo Date: Wed, 18 Mar 2020 08:33:54 -0400 Subject: [PATCH 10/13] Remove any type and ignore type issues It's a union type issue. Path is of type `NodePath` by default, whereas a visitor expects `NodePath` sometimes. At runtime it should be OK. If it's not, at least it won't crash (it will fail silently). --- src/action-providers.ts | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/action-providers.ts b/src/action-providers.ts index f5eb18ef2..c3c753645 100644 --- a/src/action-providers.ts +++ b/src/action-providers.ts @@ -91,7 +91,7 @@ class RefactoringActionProvider implements vscode.CodeActionProvider { refactoring: RefactoringWithActionProvider ) => void ) { - const visitor: t.Visitor = refactoring.actionProvider.createVisitor( + const visitor = refactoring.actionProvider.createVisitor( selection, path => whenApplicable(path, refactoring), refactoring @@ -100,14 +100,20 @@ class RefactoringActionProvider implements vscode.CodeActionProvider { this.visit(visitor, path); } - private visit(visitor: any, path: t.NodePath) { - const node: t.Node = path.node; + private visit(visitor: t.Visitor, path: t.NodePath) { + const node = path.node; try { - if (typeof visitor[node.type] === "function") { - visitor[node.type](path); - } else if (typeof visitor[node.type] === "object") { - visitor[node.type].enter(path); + const visitorNode = visitor[node.type]; + if (typeof visitorNode === "function") { + // @ts-ignore visitor can expect `NodePath` but `path` is typed as `NodePath`. It should be OK at runtime. + visitorNode.bind(visitor)(path, path.state); + } else if ( + typeof visitorNode === "object" && + typeof visitorNode.enter === "function" + ) { + // @ts-ignore visitor can expect `NodePath` but `path` is typed as `NodePath`. It should be OK at runtime. + visitorNode.enter(path, path.state); } } catch (_) { // Silently fail, we don't care why it failed (e.g. code can't be parsed). From 51cca8c0fb886244f31f7d04df83af2c8718b573 Mon Sep 17 00:00:00 2001 From: Nicolas Carlo Date: Wed, 18 Mar 2020 08:39:00 -0400 Subject: [PATCH 11/13] Inline variables --- src/action-providers.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/action-providers.ts b/src/action-providers.ts index c3c753645..2a1bf62f2 100644 --- a/src/action-providers.ts +++ b/src/action-providers.ts @@ -34,13 +34,11 @@ class RefactoringActionProvider implements vscode.CodeActionProvider { } private findApplicableLegacyRefactorings(ast: t.File, selection: Selection) { - const legacyRefactorings = this.refactorings.filter( - isRefactoringWithLegacyActionProvider - ); - const applicableLegacyRefactorings = legacyRefactorings.filter( - refactoring => this.canPerformLegacy(refactoring, ast, selection) - ); - return applicableLegacyRefactorings; + return this.refactorings + .filter(isRefactoringWithLegacyActionProvider) + .filter(refactoring => + this.canPerformLegacy(refactoring, ast, selection) + ); } private findApplicableRefactorings( From 7b2d1dffc155d3a31e6ed2bbd9c7678a6ed6c042 Mon Sep 17 00:00:00 2001 From: Nicolas Carlo Date: Wed, 18 Mar 2020 08:39:13 -0400 Subject: [PATCH 12/13] Drop unnecessary types --- src/action-providers.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/action-providers.ts b/src/action-providers.ts index 2a1bf62f2..929040a3c 100644 --- a/src/action-providers.ts +++ b/src/action-providers.ts @@ -54,16 +54,13 @@ class RefactoringActionProvider implements vscode.CodeActionProvider { >[] = []; t.traverseAST(ast, { - enter: (path: t.NodePath) => { + enter: path => { refactorings.forEach(refactoring => this.visitAndCheckApplicability( refactoring, path, selection, - ( - path: t.NodePath, - refactoring: RefactoringWithActionProvider - ) => { + (path, refactoring) => { if (refactoring.actionProvider.updateMessage) { refactoring.actionProvider.message = refactoring.actionProvider.updateMessage( path From c1c000ed56b92ea9655231e156828e96f9b0cebc Mon Sep 17 00:00:00 2001 From: Nicolas Carlo Date: Wed, 18 Mar 2020 08:39:25 -0400 Subject: [PATCH 13/13] Drop redundant check --- src/action-providers.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/action-providers.ts b/src/action-providers.ts index 929040a3c..84ad9826a 100644 --- a/src/action-providers.ts +++ b/src/action-providers.ts @@ -121,10 +121,7 @@ class RefactoringActionProvider implements vscode.CodeActionProvider { selection: Selection ) { try { - return ( - typeof refactoring.actionProvider.canPerform === "function" && - refactoring.actionProvider.canPerform(ast, selection) - ); + return refactoring.actionProvider.canPerform(ast, selection); } catch (_) { // Silently fail, we don't care why it failed (e.g. code can't be parsed). return false;