|
| 1 | +// For some reason, the `ASTNode` type from `jscodeshift` doesn't match up with the `ASTNode` type expected by |
| 2 | +// `Collection.filter` functions, which are also from `jscodeshift`, so... it should work? But in `jscodeshift` there |
| 3 | +// seems to be a mismatch between the `ASTNode` type from `ast-types` and the `ASTNode` type from `ast-types/lib/types`, |
| 4 | +// wherein the former is exported but filter functions require the latter. Needs more investigation. In the meantime, we |
| 5 | +// can just import directly from `ast-types`. |
| 6 | +import type { ASTNode as ASTNodeType } from 'ast-types'; |
| 7 | +import * as jscsTypes from 'jscodeshift'; |
| 8 | +import { default as jscodeshiftDefault } from 'jscodeshift'; |
| 9 | + |
| 10 | +// In `jscodeshift`, the exports look like this: |
| 11 | +// |
| 12 | +// function core(...) { ... } |
| 13 | +// core.ABC = ... |
| 14 | +// core.XYZ = ... |
| 15 | +// module.exports = core |
| 16 | +// |
| 17 | +// In other words, when required/imported, the module is both a callable function and an object containing all sorts of |
| 18 | +// properties. Meanwhile, its TS export is a namespace continaing the types of all of the properties attached to `core`. |
| 19 | +// In order to use the types, we thus need to use `import *` syntax. But when we do that, Rollup only sees it as a |
| 20 | +// namespace, and will complain if we try to use it as a function. In order to get around this, we take advantage of the |
| 21 | +// fact that Rollup wraps imports in its own version of TS's `esModuleInterop` functions, aliasing the export to a |
| 22 | +// `default` property inside the export. (So, here, we basically end up with `core.default = core`.) When referenced |
| 23 | +// through that alias, `core` is correctly seen as callable by Rollup. Outside of a Rollup context, however, that |
| 24 | +// `default` alias doesn't exist. So, we try both and use whichever one is defined. (See https://github.com/rollup/rollup/issues/1267.) |
| 25 | +const jscodeshiftNamespace = jscsTypes; |
| 26 | +const jscs = jscodeshiftDefault || jscodeshiftNamespace; |
| 27 | + |
| 28 | +const { |
| 29 | + ExportSpecifier, |
| 30 | + Identifier, |
| 31 | + Node, |
| 32 | + VariableDeclaration, |
| 33 | + VariableDeclarator, |
| 34 | +} = jscs; |
| 35 | + |
| 36 | +type ASTFilterFunction = (path: jscsTypes.ASTPath<ASTNodeType>) => boolean; |
| 37 | + |
| 38 | +/** |
| 39 | + * Create a filter for nodes representing Identifiers with the given name |
| 40 | + * |
| 41 | + * @param name The variable name to filter on |
| 42 | + * @returns A filter function with the name baked in |
| 43 | + */ |
| 44 | +function makeIdentifierFilter(name: string): ASTFilterFunction { |
| 45 | + const identifierFilter = function (nodePath: jscsTypes.ASTPath<ASTNodeType>): boolean { |
| 46 | + // Check that what we have is indeed an Identifier, and that the name matches |
| 47 | + // |
| 48 | + // Note: If we were being super precise about this, we'd also check the context in which the identifier is being |
| 49 | + // used, because there are some cases where we actually don't want to be renaming things (if the identifier is being |
| 50 | + // used to name a class property, for example). But the chances that someone is going to have a class property in a |
| 51 | + // nextjs page file with the same name as one of the canonical functions are slim to none, so for simplicity we can |
| 52 | + // stop filtering here. If this ever becomes a problem, more precise filter checks can be found in a comment at the |
| 53 | + // bottom of this file. |
| 54 | + return Identifier.check(nodePath.node) && nodePath.node.name === name; |
| 55 | + }; |
| 56 | + |
| 57 | + return identifierFilter; |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Create a filter for nodes declaring variables with the given name |
| 62 | + * |
| 63 | + * @param name The variable name to filter on |
| 64 | + * @returns A filter function with the name baked in |
| 65 | + */ |
| 66 | +function makeDeclarationFilter(name: string): ASTFilterFunction { |
| 67 | + // Check for a structure of the form |
| 68 | + // |
| 69 | + // node: VariableDeclaration |
| 70 | + // \ |
| 71 | + // declarations: VariableDeclarator[] |
| 72 | + // \ |
| 73 | + // 0 : VariableDeclarator |
| 74 | + // \ |
| 75 | + // id: Identifier |
| 76 | + // \ |
| 77 | + // name: string |
| 78 | + // |
| 79 | + // where `name` matches the given name. |
| 80 | + const declarationFilter = function (path: jscsTypes.ASTPath<ASTNodeType>): boolean { |
| 81 | + return ( |
| 82 | + VariableDeclaration.check(path.node) && |
| 83 | + path.node.declarations.length === 1 && |
| 84 | + VariableDeclarator.check(path.node.declarations[0]) && |
| 85 | + Identifier.check(path.node.declarations[0].id) && |
| 86 | + path.node.declarations[0].id.name === name |
| 87 | + ); |
| 88 | + }; |
| 89 | + |
| 90 | + return declarationFilter; |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * Create a filter for nodes representing exportss with the given name |
| 95 | + * |
| 96 | + * @param name The variable name to filter on |
| 97 | + * @returns A filter function with the name baked in |
| 98 | + */ |
| 99 | +function makeExportFilter(name: string): ASTFilterFunction { |
| 100 | + const exportFilter = function (path: jscsTypes.ASTPath<ASTNodeType>): boolean { |
| 101 | + return ExportSpecifier.check(path.node) && path.node.exported.name === name; |
| 102 | + }; |
| 103 | + |
| 104 | + return exportFilter; |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * Find all nodes which are representing Identifiers with the given name |
| 109 | + * |
| 110 | + * @param ast The code, in AST form |
| 111 | + * @param name The Identifier name to search for |
| 112 | + * @returns A collection of NodePaths pointing to any nodes which were found |
| 113 | + */ |
| 114 | +export function findIdentifiers(ast: jscsTypes.Collection, name: string): jscsTypes.Collection { |
| 115 | + return findNodes(ast, 'Identifier', name); |
| 116 | +} |
| 117 | + |
| 118 | +/** |
| 119 | + * Find all nodes which are declarations of variables with the given name |
| 120 | + * |
| 121 | + * @param ast The code, in AST form |
| 122 | + * @param name The variable name to search for |
| 123 | + * @returns A collection of NodePaths pointing to any nodes which were found |
| 124 | + */ |
| 125 | +export function findDeclarations(ast: jscsTypes.Collection, name: string): jscsTypes.Collection { |
| 126 | + return findNodes(ast, 'VariableDeclaration', name); |
| 127 | +} |
| 128 | + |
| 129 | +/** |
| 130 | + * Find all nodes which are exports of variables with the given name |
| 131 | + * |
| 132 | + * @param ast The code, in AST form |
| 133 | + * @param name The variable name to search for |
| 134 | + * @returns A collection of NodePaths pointing to any nodes which were found |
| 135 | + */ |
| 136 | +export function findExports(ast: jscsTypes.Collection, name: string): jscsTypes.Collection { |
| 137 | + return findNodes(ast, 'ExportSpecifier', name); |
| 138 | +} |
| 139 | + |
| 140 | +/** |
| 141 | + * Remove comments from all nodes in the given AST. |
| 142 | + * |
| 143 | + * Note: Comments are not nodes in and of themselves, but are instead attached to the nodes above and below them. |
| 144 | + * |
| 145 | + * @param ast The code, in AST form |
| 146 | + */ |
| 147 | +export function removeComments(ast: jscsTypes.Collection): void { |
| 148 | + const nodesWithComments = ast.find(Node).filter( |
| 149 | + path => |
| 150 | + !!( |
| 151 | + path.node.comments |
| 152 | + ), |
| 153 | + ); |
| 154 | + nodesWithComments.forEach(path => (path.node.comments = null)); |
| 155 | +} |
| 156 | + |
| 157 | +/** |
| 158 | + * Find all nodes of the given type with the given name |
| 159 | + * |
| 160 | + * @param ast The code, in AST form |
| 161 | + * @param nodeType The type of node for which to search |
| 162 | + * @param name The identifier which should be associated with the found nodes |
| 163 | + * @returns A collection of NodePaths pointing to any nodes which were found |
| 164 | + */ |
| 165 | +function findNodes(ast: jscsTypes.Collection, nodeType: string, name: string): jscsTypes.Collection { |
| 166 | + const filterFactories: { [key: string]: (name: string) => ASTFilterFunction } = { |
| 167 | + VariableDeclaration: makeDeclarationFilter, |
| 168 | + Identifier: makeIdentifierFilter, |
| 169 | + ExportSpecifier: makeExportFilter, |
| 170 | + }; |
| 171 | + const filter = filterFactories[nodeType](name); |
| 172 | + return ast.find(Node).filter(filter); |
| 173 | +} |
0 commit comments