Skip to content
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

Use Node.remove() to remove nodes #783

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
19 changes: 19 additions & 0 deletions src/transforms/v2-to-v3/modules/addTopLevelComments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { isDeepStrictEqual } from "util";
import { CommentKind } from "ast-types/gen/kinds";
import { Collection, JSCodeshift } from "jscodeshift";

export const addTopLevelComments = (
j: JSCodeshift,
source: Collection<unknown>,
comments?: CommentKind[] | null
) => {
if (!comments || comments.length === 0) {
return;
}

const firstNode = source.find(j.Program).get("body", 0);
const newComments = firstNode.node.comments;
if (!isDeepStrictEqual(comments, newComments)) {
firstNode.insertBefore(j.emptyStatement.from({ comments }));
}
};

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Collection, JSCodeshift, Literal } from "jscodeshift";
import { STRING_LITERAL_TYPE_LIST } from "../config";

export interface GetRequireObjectPatternPropertyOptions {
identifierName: string;
sourceValue: string;
}

export const getRequireObjectPatternProperty = (
j: JSCodeshift,
source: Collection<unknown>,
{ identifierName, sourceValue }: GetRequireObjectPatternPropertyOptions
) => {
const filter = {
key: {
type: "Identifier",
name: identifierName,
},
value: {
type: "Identifier",
},
} as const;

const propertyCollection = source.find(j.Property, filter);
const objectPropertyCollection = source.find(j.ObjectProperty, filter);
const collection = propertyCollection.size() ? propertyCollection : objectPropertyCollection;

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore This expression is not callable.
return collection.filter((path) => {
const pathValueName = path.value.name;
if (pathValueName && pathValueName !== sourceValue) return false;

const varDeclarator = j(path).closest(j.VariableDeclarator);
const init = varDeclarator.nodes()[0].init;
if (!init || init.type !== "CallExpression") return false;

const initCallee = init.callee;
if (initCallee.type !== "Identifier" || initCallee.name !== "require") return false;

const initArguments = init.arguments;
if (initArguments.length !== 1) return false;
const arg = initArguments[0];
if (!STRING_LITERAL_TYPE_LIST.includes(arg.type)) return false;
return (arg as Literal).value === sourceValue;
});
};
44 changes: 5 additions & 39 deletions src/transforms/v2-to-v3/modules/removeRequireIdentifier.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Collection, Identifier, JSCodeshift, Literal, VariableDeclarator } from "jscodeshift";
import { Collection, JSCodeshift } from "jscodeshift";

import { STRING_LITERAL_TYPE_LIST } from "../config";
import { addTopLevelComments } from "./addTopLevelComments";
import { getRequireDeclaratorsWithIdentifier } from "./getRequireDeclaratorsWithIdentifier";
import { removeDeclaration } from "./removeDeclaration";

export interface RemoveRequireIdentifierOptions {
localName: string;
Expand All @@ -19,40 +18,7 @@ export const removeRequireIdentifier = (
sourceValue,
});

requireDeclarators.forEach((varDeclarator) => {
const varDeclaration = varDeclarator.parentPath.parentPath;

// Removes variable declarator from the declarations.
varDeclaration.value.declarations = varDeclaration.value.declarations.filter(
(declaration: VariableDeclarator | Identifier) => {
if (declaration.type === "Identifier") return true;

const id = declaration.id;
if (id.type !== "Identifier") return true;
if (id.name !== localName) return true;

const init = declaration.init;
if (!init) return true;
if (init.type !== "CallExpression") return true;

const callee = init.callee;
if (!callee) return true;
if (callee.type !== "Identifier") return true;
if (callee.name !== "require") return true;

const args = init.arguments;
if (!args) return true;
if (args.length !== 1) return true;
if (!STRING_LITERAL_TYPE_LIST.includes(args[0].type)) return true;
if ((args[0] as Literal).value !== sourceValue) return true;

return false;
}
);

// Remove VariableDeclaration if there are no declarations.
if (varDeclaration.value.declarations?.length === 0) {
removeDeclaration(j, source, varDeclaration);
}
});
const comments = source.find(j.Program).get("body", 0).node.comments;
requireDeclarators.remove();
addTopLevelComments(j, source, comments);
};
42 changes: 7 additions & 35 deletions src/transforms/v2-to-v3/modules/removeRequireObjectPattern.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import {
Collection,
Identifier,
JSCodeshift,
ObjectPattern,
ObjectProperty,
Property,
VariableDeclarator,
} from "jscodeshift";
import { Collection, JSCodeshift } from "jscodeshift";

import { OBJECT_PROPERTY_TYPE_LIST } from "../config";
import { getRequireDeclaratorsWithObjectPattern } from "./getRequireDeclaratorsWithObjectPattern";
import { removeDeclaration } from "./removeDeclaration";
import { addTopLevelComments } from "./addTopLevelComments";
import { getRequireObjectPatternProperty } from "./getRequireObjectPatternProperty";

export interface RemoveRequireObjectPropertyOptions {
localName: string;
Expand All @@ -22,31 +13,12 @@ export const removeRequireObjectPattern = (
source: Collection<unknown>,
{ localName, sourceValue }: RemoveRequireObjectPropertyOptions
) => {
const requireDeclarators = getRequireDeclaratorsWithObjectPattern(j, source, {
const requireObjectPatternProperty = getRequireObjectPatternProperty(j, source, {
identifierName: localName,
sourceValue,
});

requireDeclarators.forEach((varDeclarator) => {
// Remove ObjectProperty from Variable Declarator.
const varDeclaratorId = varDeclarator.value.id as ObjectPattern;
varDeclaratorId.properties = varDeclaratorId.properties.filter((property) => {
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) return true;
const propertyValue = (property as Property | ObjectProperty).value;
return propertyValue.type !== "Identifier" || propertyValue.name !== localName;
});

// Remove VariableDeclarator if there are no properties.
if (varDeclaratorId.properties.length === 0) {
const varDeclaration = varDeclarator.parentPath.parentPath;
varDeclaration.value.declarations = varDeclaration.value.declarations.filter(
(declaration: VariableDeclarator | Identifier) => declaration !== varDeclarator.value
);

// Remove VariableDeclaration if there are no declarations.
if (varDeclaration.value.declarations?.length === 0) {
removeDeclaration(j, source, varDeclaration);
}
}
});
const comments = source.find(j.Program).get("body", 0).node.comments;
requireObjectPatternProperty.remove();
addTopLevelComments(j, source, comments);
};
Loading