Skip to content

Commit

Permalink
feat: add is-array-buffer codemod
Browse files Browse the repository at this point in the history
  • Loading branch information
thepassle committed Jul 17, 2024
1 parent 598ea99 commit 59b061e
Show file tree
Hide file tree
Showing 15 changed files with 262 additions and 6 deletions.
2 changes: 1 addition & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"enabled": true
},
"files": {
"ignore": ["node_modules/*"]
"ignore": ["node_modules/*", "test/fixtures/*"]
},
"javascript": {
"formatter": {
Expand Down
3 changes: 3 additions & 0 deletions codemods/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import isWhitespace from './is-whitespace/index.js';

import isArrayBuffer from './is-array-buffer/index.js';

export const codemods = {
'is-whitespace': isWhitespace,
'is-array-buffer': isArrayBuffer,
};
82 changes: 82 additions & 0 deletions codemods/is-array-buffer/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* @typedef {import('../../types.js').Codemod} Codemod
* @typedef {import('../../types.js').CodemodOptions} CodemodOptions
*/

/**
* @param {CodemodOptions} [options]
* @returns {Codemod}
*/
export default function (options) {
return {
name: 'is-array-buffer',
transform: ({ file, jscodeshift }) => {
const j = jscodeshift;
const root = j(file.source);
let dirtyFlag = false;

// Remove the require statement for 'is-array-buffer'
root.find(j.VariableDeclaration).forEach((path) => {
const declarations = path.value.declarations;
if (
declarations.length === 1 &&
j.VariableDeclarator.check(declarations[0])
) {
const declarator = declarations[0];
if (
j.CallExpression.check(declarator.init) &&
j.Identifier.check(declarator.id)
) {
const callExpr = declarator.init;
if (
j.Identifier.check(callExpr.callee) &&
callExpr.callee.name === 'require'
) {
const args = callExpr.arguments;
if (
args.length === 1 &&
j.Literal.check(args[0]) &&
args[0].value === 'is-array-buffer'
) {
j(path).remove();
dirtyFlag = true;
}
}
}
}
});

// Remove ESM import statement for 'is-array-buffer'
root.find(j.ImportDeclaration, {
source: { value: 'is-array-buffer' }
}).forEach(path => {
j(path).remove();
dirtyFlag = true;
});

// Replace isArrayBuffer calls with (foo instanceof ArrayBuffer)
root
.find(j.CallExpression, {
callee: {
type: 'Identifier',
name: 'isArrayBuffer',
},
})
.forEach((path) => {
const args = path.value.arguments;
if (args.length === 1) {
const arg = args[0];
const newExpression = j.binaryExpression(
'instanceof',
arg,

Check failure on line 71 in codemods/is-array-buffer/index.js

View workflow job for this annotation

GitHub Actions / typecheck (20.x)

Argument of type 'SpreadElement | ExpressionKind' is not assignable to parameter of type 'ExpressionKind'.
j.identifier('ArrayBuffer'),
);
j(path).replaceWith(newExpression);
dirtyFlag = true;
}
});

return dirtyFlag ? root.toSource(options) : file.source;
},
};
}
2 changes: 0 additions & 2 deletions codemods/is-whitespace/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { DEFAULT_GLOB } from '../../CONSTANTS.js';

/**
* @typedef {import('../../types.js').Codemod} Codemod
* @typedef {import('../../types.js').CodemodOptions} CodemodOptions
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
},
"scripts": {
"test": "node --test test/codemod.test.js",
"test:watch": "node --test --watch test/codemod.test.js",
"format": "npx @biomejs/biome format --write **/*",
"lint:types": "tsc"
},
Expand Down
3 changes: 0 additions & 3 deletions scripts/scaffold-codemod.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ fs.mkdirSync(`./codemods/${name}`);
fs.writeFileSync(
`./codemods/${name}/index.js`,
`
import { DEFAULT_GLOB } from '../../CONSTANTS.js';
/**
* @typedef {import('../../types.js').Codemod} Codemod
* @typedef {import('../../types.js').CodemodOptions} CodemodOptions
Expand All @@ -48,7 +46,6 @@ import { DEFAULT_GLOB } from '../../CONSTANTS.js';
export default function(options) {
return {
name: '${name}',
glob: options?.glob ?? DEFAULT_GLOB,
transform: ({ file, jscodeshift }) => {
return '';
},
Expand Down
26 changes: 26 additions & 0 deletions test/fixtures/is-array-buffer/case-1/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var assert = require('assert');

assert(!(function () {} instanceof ArrayBuffer));
assert(!(null instanceof ArrayBuffer));
assert(
!(function* () {
yield 42;
return Infinity;
} instanceof ArrayBuffer),
);
assert(!(Symbol('foo') instanceof ArrayBuffer));
assert(!(1n instanceof ArrayBuffer));
assert(!(Object(1n) instanceof ArrayBuffer));

assert(!(new Set() instanceof ArrayBuffer));
assert(!(new WeakSet() instanceof ArrayBuffer));
assert(!(new Map() instanceof ArrayBuffer));
assert(!(new WeakMap() instanceof ArrayBuffer));
assert(!(new WeakRef({}) instanceof ArrayBuffer));
assert(!(new FinalizationRegistry(() => {}) instanceof ArrayBuffer));
assert(!(new SharedArrayBuffer() instanceof ArrayBuffer));

assert(new ArrayBuffer() instanceof ArrayBuffer);

class MyArrayBuffer extends ArrayBuffer {}
assert(new MyArrayBuffer() instanceof ArrayBuffer);
27 changes: 27 additions & 0 deletions test/fixtures/is-array-buffer/case-1/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var assert = require('assert');
var isArrayBuffer = require('is-array-buffer');

assert(!isArrayBuffer(function () {}));
assert(!isArrayBuffer(null));
assert(
!isArrayBuffer(function* () {
yield 42;
return Infinity;
}),
);
assert(!isArrayBuffer(Symbol('foo')));
assert(!isArrayBuffer(1n));
assert(!isArrayBuffer(Object(1n)));

assert(!isArrayBuffer(new Set()));
assert(!isArrayBuffer(new WeakSet()));
assert(!isArrayBuffer(new Map()));
assert(!isArrayBuffer(new WeakMap()));
assert(!isArrayBuffer(new WeakRef({})));
assert(!isArrayBuffer(new FinalizationRegistry(() => {})));
assert(!isArrayBuffer(new SharedArrayBuffer()));

assert(isArrayBuffer(new ArrayBuffer()));

class MyArrayBuffer extends ArrayBuffer {}
assert(isArrayBuffer(new MyArrayBuffer()));
26 changes: 26 additions & 0 deletions test/fixtures/is-array-buffer/case-1/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var assert = require('assert');

assert(!(function () {} instanceof ArrayBuffer));
assert(!(null instanceof ArrayBuffer));
assert(
!(function* () {
yield 42;
return Infinity;
} instanceof ArrayBuffer),
);
assert(!(Symbol('foo') instanceof ArrayBuffer));
assert(!(1n instanceof ArrayBuffer));
assert(!(Object(1n) instanceof ArrayBuffer));

assert(!(new Set() instanceof ArrayBuffer));
assert(!(new WeakSet() instanceof ArrayBuffer));
assert(!(new Map() instanceof ArrayBuffer));
assert(!(new WeakMap() instanceof ArrayBuffer));
assert(!(new WeakRef({}) instanceof ArrayBuffer));
assert(!(new FinalizationRegistry(() => {}) instanceof ArrayBuffer));
assert(!(new SharedArrayBuffer() instanceof ArrayBuffer));

assert(new ArrayBuffer() instanceof ArrayBuffer);

class MyArrayBuffer extends ArrayBuffer {}
assert(new MyArrayBuffer() instanceof ArrayBuffer);
26 changes: 26 additions & 0 deletions test/fixtures/is-array-buffer/case-2/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var assert = require('assert');

assert(!(function () {} instanceof ArrayBuffer));
assert(!(null instanceof ArrayBuffer));
assert(
!(function* () {
yield 42;
return Infinity;
} instanceof ArrayBuffer),
);
assert(!(Symbol('foo') instanceof ArrayBuffer));
assert(!(1n instanceof ArrayBuffer));
assert(!(Object(1n) instanceof ArrayBuffer));

assert(!(new Set() instanceof ArrayBuffer));
assert(!(new WeakSet() instanceof ArrayBuffer));
assert(!(new Map() instanceof ArrayBuffer));
assert(!(new WeakMap() instanceof ArrayBuffer));
assert(!(new WeakRef({}) instanceof ArrayBuffer));
assert(!(new FinalizationRegistry(() => {}) instanceof ArrayBuffer));
assert(!(new SharedArrayBuffer() instanceof ArrayBuffer));

assert(new ArrayBuffer() instanceof ArrayBuffer);

class MyArrayBuffer extends ArrayBuffer {}
assert(new MyArrayBuffer() instanceof ArrayBuffer);
27 changes: 27 additions & 0 deletions test/fixtures/is-array-buffer/case-2/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var assert = require('assert');
import isArrayBuffer from 'is-array-buffer';

assert(!isArrayBuffer(function () {}));
assert(!isArrayBuffer(null));
assert(
!isArrayBuffer(function* () {
yield 42;
return Infinity;
}),
);
assert(!isArrayBuffer(Symbol('foo')));
assert(!isArrayBuffer(1n));
assert(!isArrayBuffer(Object(1n)));

assert(!isArrayBuffer(new Set()));
assert(!isArrayBuffer(new WeakSet()));
assert(!isArrayBuffer(new Map()));
assert(!isArrayBuffer(new WeakMap()));
assert(!isArrayBuffer(new WeakRef({})));
assert(!isArrayBuffer(new FinalizationRegistry(() => {})));
assert(!isArrayBuffer(new SharedArrayBuffer()));

assert(isArrayBuffer(new ArrayBuffer()));

class MyArrayBuffer extends ArrayBuffer {}
assert(isArrayBuffer(new MyArrayBuffer()));
26 changes: 26 additions & 0 deletions test/fixtures/is-array-buffer/case-2/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var assert = require('assert');

assert(!(function () {} instanceof ArrayBuffer));
assert(!(null instanceof ArrayBuffer));
assert(
!(function* () {
yield 42;
return Infinity;
} instanceof ArrayBuffer),
);
assert(!(Symbol('foo') instanceof ArrayBuffer));
assert(!(1n instanceof ArrayBuffer));
assert(!(Object(1n) instanceof ArrayBuffer));

assert(!(new Set() instanceof ArrayBuffer));
assert(!(new WeakSet() instanceof ArrayBuffer));
assert(!(new Map() instanceof ArrayBuffer));
assert(!(new WeakMap() instanceof ArrayBuffer));
assert(!(new WeakRef({}) instanceof ArrayBuffer));
assert(!(new FinalizationRegistry(() => {}) instanceof ArrayBuffer));
assert(!(new SharedArrayBuffer() instanceof ArrayBuffer));

assert(new ArrayBuffer() instanceof ArrayBuffer);

class MyArrayBuffer extends ArrayBuffer {}
assert(new MyArrayBuffer() instanceof ArrayBuffer);
5 changes: 5 additions & 0 deletions test/fixtures/is-whitespace/case-2/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let str = ' ';
let str2 = 'Hello';

console.log(str1.trim() === ''); // true
console.log(str2.trim() === ''); // false
7 changes: 7 additions & 0 deletions test/fixtures/is-whitespace/case-2/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var isWhitespace = require('is-whitespace');

let str = ' ';
let str2 = 'Hello';

console.log(isWhitespace(str1)); // true
console.log(isWhitespace(str2)); // false
5 changes: 5 additions & 0 deletions test/fixtures/is-whitespace/case-2/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let str = ' ';
let str2 = 'Hello';

console.log(str1.trim() === ''); // true
console.log(str2.trim() === ''); // false

0 comments on commit 59b061e

Please sign in to comment.