Skip to content
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
8 changes: 4 additions & 4 deletions .github/workflows/biome.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs

name: CheckFormatting
name: Lint and Check Formatting

on:
push:
Expand All @@ -25,7 +25,7 @@ jobs:
- name: Setup Biome CLI
uses: biomejs/setup-biome@4c91541eaada48f67d7dbd7833600ce162b68f51 # v2.7.1
with:
version: 2.3.10
version: 2.4.7

- name: Run Biome
run: biome format **/*
- name: Lint and Check Formatting
run: npm run lint
23 changes: 17 additions & 6 deletions biome.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
{
"$schema": "https://biomejs.dev/schemas/2.3.10/schema.json",
"assist": { "actions": { "source": { "organizeImports": "on" } } },
"$schema": "https://biomejs.dev/schemas/2.4.7/schema.json",
"files": {
"includes": [
"**",
"!**/node_modules/**/*",
"!test/fixtures/**/*",
"!types/**/*"
"!types/**/*",
"!index.js",
"!package.json",
"!tsconfig.json"
]
},
"javascript": {
"formatter": {
"quoteStyle": "single"
"javascript": { "formatter": { "quoteStyle": "single", "lineEnding": "lf" } },
"assist": { "actions": { "source": { "organizeImports": "on" } } },
"linter": {
"enabled": true,
"rules": {
"suspicious": {
"noEmptyInterface": "off",
"noTsIgnore": "off"
},
"correctness": {
"noUnusedFunctionParameters": "off"
}
}
}
}
2 changes: 0 additions & 2 deletions codemods/define-properties/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import {
insertAfterImports,
insertCommentAboveNode,
removeImport,
replaceDefaultImport,
replaceRequireMemberExpression,
} from '../shared.js';
import { dir } from 'console';

/**
* @typedef {import('../../types.js').Codemod} Codemod
Expand Down
2 changes: 1 addition & 1 deletion codemods/es-aggregate-error/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import jscodeshift from 'jscodeshift';
import { removeImport, transformMathPolyfill } from '../shared.js';
import { removeImport } from '../shared.js';

/**
* @typedef {import('../../types.js').Codemod} Codemod
Expand Down
1 change: 0 additions & 1 deletion codemods/es-errors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export default function (options) {
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
let dirtyFlag = false;

for (const [moduleName, errorName] of Object.entries(moduleToErrorMap)) {
const { identifier } = removeImport(moduleName, root, j);
Expand Down
3 changes: 1 addition & 2 deletions codemods/is-nan/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ export default function (options) {
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
let dirtyFlag = false;

let { identifier } = removeImport('is-nan', root, j);
const { identifier } = removeImport('is-nan', root, j);

root
.find(j.CallExpression, {
Expand Down
24 changes: 18 additions & 6 deletions codemods/is-npm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ export default function (options) {

root
.find(j.Identifier, { name: identifiers.isNpm })
.forEach(() => (isDirty = true))
.forEach(() => {
isDirty = true;
})
.replaceWith(isNpmLogicalExpression);

if (identifiers[NAMESPACE_IMPORT]) {
Expand All @@ -110,13 +112,17 @@ export default function (options) {
type: 'Identifier',
},
})
.forEach(() => (isDirty = true))
.forEach(() => {
isDirty = true;
})
.replaceWith(isNpmLogicalExpression);
}

root
.find(j.Identifier, { name: identifiers.isYarn })
.forEach(() => (isDirty = true))
.forEach(() => {
isDirty = true;
})
.replaceWith(isYarnLogicalExpression);

if (identifiers[NAMESPACE_IMPORT]) {
Expand All @@ -131,13 +137,17 @@ export default function (options) {
type: 'Identifier',
},
})
.forEach(() => (isDirty = true))
.forEach(() => {
isDirty = true;
})
.replaceWith(isYarnLogicalExpression);
}

root
.find(j.Identifier, { name: identifiers.isNpmOrYarn })
.forEach(() => (isDirty = true))
.forEach(() => {
isDirty = true;
})
.replaceWith(isNpmOrYarnLogicalExpression);

if (identifiers[NAMESPACE_IMPORT]) {
Expand All @@ -152,7 +162,9 @@ export default function (options) {
type: 'Identifier',
},
})
.forEach(() => (isDirty = true))
.forEach(() => {
isDirty = true;
})
.replaceWith(isNpmOrYarnLogicalExpression);
}

Expand Down
3 changes: 1 addition & 2 deletions codemods/number.isnan/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ export default function (options) {
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
let dirtyFlag = false;

let { identifier } = removeImport('number.isnan', root, j);
const { identifier } = removeImport('number.isnan', root, j);

root
.find(j.CallExpression, {
Expand Down
3 changes: 1 addition & 2 deletions codemods/object-assign/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ export default function (options) {
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
let dirtyFlag = false;

let { identifier } = removeImport('object-assign', root, j);
const { identifier } = removeImport('object-assign', root, j);

root
.find(j.CallExpression, {
Expand Down
3 changes: 1 addition & 2 deletions codemods/object-is/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ export default function (options) {
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
let dirtyFlag = false;

let { identifier } = removeImport('object-is', root, j);
const { identifier } = removeImport('object-is', root, j);

root
.find(j.CallExpression, {
Expand Down
3 changes: 1 addition & 2 deletions codemods/object.entries/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ export default function (options) {
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
let dirtyFlag = false;

let { identifier } = removeImport('object.entries', root, j);
const { identifier } = removeImport('object.entries', root, j);

root
.find(j.CallExpression, {
Expand Down
3 changes: 1 addition & 2 deletions codemods/object.fromentries/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ export default function (options) {
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
let dirtyFlag = false;

let { identifier } = removeImport('object.fromentries', root, j);
const { identifier } = removeImport('object.fromentries', root, j);

root
.find(j.CallExpression, {
Expand Down
3 changes: 1 addition & 2 deletions codemods/object.values/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ export default function (options) {
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
let dirtyFlag = false;

let { identifier } = removeImport('object.values', root, j);
const { identifier } = removeImport('object.values', root, j);

root
.find(j.CallExpression, {
Expand Down
2 changes: 1 addition & 1 deletion codemods/rimraf/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ Promise.all(
if (imports.length > 0) {
const [firstImport, ...remainingImports] = imports;

let replacedImports = [];
const replacedImports = [];

if (seenAsync) {
replacedImports.push(
Expand Down
4 changes: 1 addition & 3 deletions codemods/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,6 @@ export function getVariableExpressionHasIdentifier(
},
});

const source = root.toSource();

return requireDeclarationWithProperty.length > 0;
}

Expand All @@ -523,7 +521,7 @@ export function getVariableExpressionHasIdentifier(
* @param {import("jscodeshift").JSCodeshift} j - jscodeshift instance
*/
export function replaceRequireMemberExpression(importName, value, root, j) {
const requireDeclaration = root
root
.find(j.MemberExpression, {
object: {
callee: {
Expand Down
2 changes: 1 addition & 1 deletion codemods/typed-array-buffer/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import jscodeshift from 'jscodeshift';
import { removeImport, transformInstanceProperty } from '../shared.js';
import { ALL_TYPED_ARRAY_OBJECTS } from '../CONSTANTS.js';
import { removeImport, transformInstanceProperty } from '../shared.js';

/**
* @typedef {import('../../types.js').Codemod} Codemod
Expand Down
2 changes: 1 addition & 1 deletion codemods/typed-array-byte-length/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import jscodeshift from 'jscodeshift';
import { removeImport, transformInstanceProperty } from '../shared.js';
import { ALL_TYPED_ARRAY_OBJECTS } from '../CONSTANTS.js';
import { removeImport, transformInstanceProperty } from '../shared.js';

/**
* @typedef {import('../../types.js').Codemod} Codemod
Expand Down
2 changes: 1 addition & 1 deletion codemods/typed-array-byte-offset/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import jscodeshift from 'jscodeshift';
import { removeImport, transformInstanceProperty } from '../shared.js';
import { ALL_TYPED_ARRAY_OBJECTS } from '../CONSTANTS.js';
import { removeImport, transformInstanceProperty } from '../shared.js';

/**
* @typedef {import('../../types.js').Codemod} Codemod
Expand Down
2 changes: 1 addition & 1 deletion codemods/typed-array-length/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import jscodeshift from 'jscodeshift';
import { removeImport, transformInstanceProperty } from '../shared.js';
import { ALL_TYPED_ARRAY_OBJECTS } from '../CONSTANTS.js';
import { removeImport, transformInstanceProperty } from '../shared.js';

/**
* @typedef {import('../../types.js').Codemod} Codemod
Expand Down
6 changes: 1 addition & 5 deletions codemods/typedarray.prototype.slice/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import jscodeshift from 'jscodeshift';
import {
removeImport,
transformInstanceMethod,
transformInstanceProperty,
} from '../shared.js';
import { ALL_TYPED_ARRAY_OBJECTS } from '../CONSTANTS.js';
import { removeImport, transformInstanceMethod } from '../shared.js';

/**
* @typedef {import('../../types.js').Codemod} Codemod
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
"scripts": {
"test": "node --test test/codemod.test.js",
"test:watch": "node --test --watch test/codemod.test.js",
"format": "biome format --write **/*",
"lint": "biome check",
"format": "biome check --fix",
"generate": "node scripts/generate-index.js && node scripts/generate-pkg-exports.js",
"lint:types": "tsc",
"which": "node scripts/which.js",
"prepublishOnly": "npm run lint:types && node scripts/generate-index.js && node scripts/generate-pkg-exports.js"
"prepublishOnly": "npm run lint:types && npm run generate"
},
"repository": {
"type": "git",
Expand Down
6 changes: 3 additions & 3 deletions scripts/generate-index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs';
import fs from 'node:fs';

/** @param {string} s */
const camelize = (s) => s.replace(/[-\.]./g, (x) => x[1].toUpperCase());
const camelize = (s) => s.replace(/[-.]./g, (x) => x[1].toUpperCase());

const folders = fs
.readdirSync('./codemods')
Expand Down Expand Up @@ -31,6 +31,6 @@ for (const folder of folders) {
}
obj += `};`;

fs.writeFileSync('./index.js', imports + '\n' + obj);
fs.writeFileSync('./index.js', `${imports}\n${obj}`);

console.log('Generated index.js');
8 changes: 4 additions & 4 deletions scripts/generate-pkg-exports.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env node

import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
Expand Down Expand Up @@ -66,7 +66,7 @@ async function generateExports() {
packageJson.exports = sortedExports;
fs.writeFileSync(
packageJsonPath,
JSON.stringify(packageJson, null, 2) + '\n',
`${JSON.stringify(packageJson, null, 2)}\n`,
);
console.log('\n✓ Updated package.json with exports');
} else {
Expand Down
2 changes: 1 addition & 1 deletion scripts/scaffold-codemod.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fs from 'fs';
import fs from 'node:fs';

/**
* Usage:
Expand Down
2 changes: 1 addition & 1 deletion scripts/which.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
nativeReplacements,
microUtilsReplacements,
nativeReplacements,
} from 'module-replacements';
import { codemods } from '../index.js';

Expand Down
13 changes: 5 additions & 8 deletions test/codemod.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { describe, it } from 'node:test';
import fs from 'node:fs';
import assert from 'node:assert';
import { codemods } from '../index.js';
import { fileURLToPath } from 'node:url';
import fs from 'node:fs';
import { dirname, join } from 'node:path';
import { describe, it } from 'node:test';
import { fileURLToPath } from 'node:url';
import { codemods } from '../index.js';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
Expand All @@ -16,14 +16,11 @@ async function discoverAlternatives() {
/** @type {Array<{path: string, codemodFn: () => import('../types.js').Codemod}>} */
const alternatives = [];

for (const [exportPath, exportConfig] of Object.entries(
packageJson.exports,
)) {
for (const exportPath of Object.keys(packageJson.exports)) {
const match = exportPath.match(
/^\.\/codemods\/([^/]+)\/([^/]+)\/index\.js$/,
);
if (match) {
const [, pkg, alt] = match;
const codemodModule = await import(`../${exportPath}`);
const codemodFn = codemodModule.default;
alternatives.push({ path: exportPath, codemodFn });
Expand Down
Loading