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
46 changes: 14 additions & 32 deletions codemods/array-buffer-byte-length/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { ts } from '@ast-grep/napi';
import { removeImport } from '../shared-ast-grep.js';
import {
computeCallReplacementEdits,
removeImport,
} from '../shared-ast-grep.js';

const MODULE_NAME = 'array-buffer-byte-length';

Expand All @@ -22,37 +25,16 @@ export default function (options) {

const { edits, localNames } = removeImport(root, MODULE_NAME);

if (localNames.length === 0) {
return file.source;
}

const identifierName = localNames[0];

const callExpressions = root.findAll({
rule: {
pattern: `${identifierName}($$$ARG)`,
},
});

for (const call of callExpressions) {
const argsMatch = call.getMultipleMatches('ARG');
if (!argsMatch) continue;

const args = argsMatch.filter((m) => m.kind() !== ',');

if (args.length !== 1) continue;

const argNode = args[0];
const argText = argNode.text();

const isIdentifier = argNode.kind() === 'identifier';
const isNewArrayBuffer =
argNode.kind() === 'new_expression' &&
argText.startsWith('new ArrayBuffer');

if (isIdentifier || isNewArrayBuffer) {
edits.push(call.replace(`${argText}.byteLength`));
}
for (const identifierName of localNames) {
const callEdits = computeCallReplacementEdits(
root,
identifierName,
(args) => {
if (args.length !== 1) return null;
return `${args[0]}.byteLength`;
},
);
edits.push(...callEdits);
}

return edits.length > 0 ? root.commitEdits(edits) : file.source;
Expand Down
32 changes: 11 additions & 21 deletions codemods/es-define-property/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { ts } from '@ast-grep/napi';
import { removeImport } from '../shared-ast-grep.js';
import {
computeCallReplacementEdits,
removeImport,
} from '../shared-ast-grep.js';

const MODULE_NAME = 'es-define-property';

Expand All @@ -21,27 +24,14 @@ export default function (options) {
const root = ast.root();

const { edits, localNames } = removeImport(root, MODULE_NAME);
const identifierName = localNames[0];

if (!identifierName) {
return edits.length > 0 ? root.commitEdits(edits) : file.source;
}

const calls = root.findAll({
rule: {
pattern: `${identifierName}($$$ARGS)`,
},
});

for (const call of calls) {
const argsMatch = call.getMultipleMatches('ARGS');
const argsText = argsMatch
? argsMatch
.filter((m) => m.kind() !== ',')
.map((m) => m.text())
.join(', ')
: '';
edits.push(call.replace(`Object.defineProperty(${argsText})`));
for (const identifierName of localNames) {
const callEdits = computeCallReplacementEdits(
root,
identifierName,
(args) => `Object.defineProperty(${args.join(', ')})`,
);
edits.push(...callEdits);
}

return edits.length > 0 ? root.commitEdits(edits) : file.source;
Expand Down
31 changes: 9 additions & 22 deletions codemods/is-array-buffer/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { ts } from '@ast-grep/napi';
import { removeImport } from '../shared-ast-grep.js';
import {
computeCallReplacementEdits,
removeImport,
} from '../shared-ast-grep.js';

const MODULE_NAME = 'is-array-buffer';

Expand All @@ -22,28 +25,12 @@ export default function (options) {

const { edits, localNames } = removeImport(root, MODULE_NAME);

if (localNames.length === 0) {
return file.source;
}

for (const localName of localNames) {
const calls = root.findAll({
rule: {
pattern: `${localName}($$$ARGS)`,
},
for (const name of localNames) {
const callEdits = computeCallReplacementEdits(root, name, (args) => {
if (args.length !== 1) return null;
return `(${args[0]} instanceof ArrayBuffer)`;
});

for (const call of calls) {
const argsMatch = call.getMultipleMatches('ARGS');
if (!argsMatch) continue;

const args = argsMatch.filter((m) => m.kind() !== ',');
if (args.length !== 1) continue;

const argText = args[0].text();
const replacement = `(${argText} instanceof ArrayBuffer)`;
edits.push(call.replace(replacement));
}
edits.push(...callEdits);
}

return edits.length > 0 ? root.commitEdits(edits) : file.source;
Expand Down
31 changes: 9 additions & 22 deletions codemods/is-boolean-object/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { ts } from '@ast-grep/napi';
import { removeImport } from '../shared-ast-grep.js';
import {
computeCallReplacementEdits,
removeImport,
} from '../shared-ast-grep.js';

const MODULE_NAME = 'is-boolean-object';

Expand All @@ -22,28 +25,12 @@ export default function (options) {

const { edits, localNames } = removeImport(root, MODULE_NAME);

if (localNames.length === 0) {
return file.source;
}

for (const localName of localNames) {
const calls = root.findAll({
rule: {
pattern: `${localName}($$$ARGS)`,
},
for (const name of localNames) {
const callEdits = computeCallReplacementEdits(root, name, (args) => {
if (args.length !== 1) return null;
return `Object.prototype.toString.call(${args[0]}) === '[object Boolean]'`;
});

for (const call of calls) {
const argsMatch = call.getMultipleMatches('ARGS');
if (!argsMatch) continue;

const args = argsMatch.filter((m) => m.kind() !== ',');
if (args.length !== 1) continue;

const argText = args[0].text();
const replacement = `Object.prototype.toString.call(${argText}) === '[object Boolean]'`;
edits.push(call.replace(replacement));
}
edits.push(...callEdits);
}

return edits.length > 0 ? root.commitEdits(edits) : file.source;
Expand Down
31 changes: 9 additions & 22 deletions codemods/is-date-object/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { ts } from '@ast-grep/napi';
import { removeImport } from '../shared-ast-grep.js';
import {
computeCallReplacementEdits,
removeImport,
} from '../shared-ast-grep.js';

const MODULE_NAME = 'is-date-object';

Expand All @@ -22,28 +25,12 @@ export default function (options) {

const { edits, localNames } = removeImport(root, MODULE_NAME);

if (localNames.length === 0) {
return file.source;
}

for (const localName of localNames) {
const calls = root.findAll({
rule: {
pattern: `${localName}($$$ARGS)`,
},
for (const name of localNames) {
const callEdits = computeCallReplacementEdits(root, name, (args) => {
if (args.length !== 1) return null;
return `Object.prototype.toString.call(${args[0]}) === '[object Date]'`;
});

for (const call of calls) {
const argsMatch = call.getMultipleMatches('ARGS');
if (!argsMatch) continue;

const args = argsMatch.filter((m) => m.kind() !== ',');
if (args.length !== 1) continue;

const argText = args[0].text();
const replacement = `Object.prototype.toString.call(${argText}) === '[object Date]'`;
edits.push(call.replace(replacement));
}
edits.push(...callEdits);
}

return edits.length > 0 ? root.commitEdits(edits) : file.source;
Expand Down
31 changes: 9 additions & 22 deletions codemods/is-even/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { ts } from '@ast-grep/napi';
import { removeImport } from '../shared-ast-grep.js';
import {
computeCallReplacementEdits,
removeImport,
} from '../shared-ast-grep.js';

const MODULE_NAME = 'is-even';

Expand All @@ -22,28 +25,12 @@ export default function (options) {

const { edits, localNames } = removeImport(root, MODULE_NAME);

if (localNames.length === 0) {
return file.source;
}

for (const localName of localNames) {
const calls = root.findAll({
rule: {
pattern: `${localName}($$$ARGS)`,
},
for (const name of localNames) {
const callEdits = computeCallReplacementEdits(root, name, (args) => {
if (args.length !== 1) return null;
return `(${args[0]} % 2 === 0)`;
});

for (const call of calls) {
const argsMatch = call.getMultipleMatches('ARGS');
if (!argsMatch) continue;

const args = argsMatch.filter((m) => m.kind() !== ',');
if (args.length !== 1) continue;

const argText = args[0].text();
const replacement = `(${argText} % 2 === 0)`;
edits.push(call.replace(replacement));
}
edits.push(...callEdits);
}

return edits.length > 0 ? root.commitEdits(edits) : file.source;
Expand Down
26 changes: 12 additions & 14 deletions codemods/is-negative-zero/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { ts } from '@ast-grep/napi';
import { removeImport } from '../shared-ast-grep.js';
import {
computeCallReplacementEdits,
removeImport,
} from '../shared-ast-grep.js';

const MODULE_NAME = 'is-negative-zero';

Expand Down Expand Up @@ -42,20 +45,15 @@ export default function (options) {
}

// Find remaining localName(...) calls and replace with Object.is(arg, -0)
const calls = root.findAll({
rule: {
pattern: `${localName}($$$ARGS)`,
const callEdits = computeCallReplacementEdits(
root,
localName,
(args) => {
if (args.length !== 1) return null;
return `Object.is(${args[0]}, -0)`;
},
});

for (const call of calls) {
const argsMatch = call.getMultipleMatches('ARGS');
if (!argsMatch) continue;
const args = argsMatch.filter((m) => m.kind() !== ',');
if (args.length !== 1) continue;
const argText = args[0].text();
edits.push(call.replace(`Object.is(${argText}, -0)`));
}
);
edits.push(...callEdits);
}

return edits.length > 0 ? root.commitEdits(edits) : file.source;
Expand Down
31 changes: 9 additions & 22 deletions codemods/is-number/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { ts } from '@ast-grep/napi';
import { removeImport } from '../shared-ast-grep.js';
import {
computeCallReplacementEdits,
removeImport,
} from '../shared-ast-grep.js';

const MODULE_NAME = 'is-number';

Expand All @@ -22,28 +25,12 @@ export default function (options) {

const { edits, localNames } = removeImport(root, MODULE_NAME);

if (localNames.length === 0) {
return file.source;
}

for (const localName of localNames) {
const calls = root.findAll({
rule: {
pattern: `${localName}($$$ARGS)`,
},
for (const name of localNames) {
const callEdits = computeCallReplacementEdits(root, name, (args) => {
if (args.length !== 1) return null;
return `(typeof ${args[0]} === 'number' || typeof ${args[0]} === 'string' && Number.isFinite(+${args[0]}))`;
});

for (const call of calls) {
const argsMatch = call.getMultipleMatches('ARGS');
if (!argsMatch) continue;

const args = argsMatch.filter((m) => m.kind() !== ',');
if (args.length !== 1) continue;

const argText = args[0].text();
const replacement = `(typeof ${argText} === 'number' || typeof ${argText} === 'string' && Number.isFinite(+${argText}))`;
edits.push(call.replace(replacement));
}
edits.push(...callEdits);
}

return edits.length > 0 ? root.commitEdits(edits) : file.source;
Expand Down
Loading