Skip to content
Open
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
2 changes: 1 addition & 1 deletion codemods/array.prototype.find/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default function (options) {
root,
identifierName,
'find',
(args) => args.length === 2,
(args) => args.length >= 2,
);

for (const imp of imports) {
Expand Down
38 changes: 28 additions & 10 deletions codemods/array.prototype.findindex/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import jscodeshift from 'jscodeshift';
import { transformArrayMethod } from '../shared.js';
import { ts } from '@ast-grep/napi';
import {
computePolyfillMethodCallReplacementEdits,
findDefaultImportIdentifier,
} from '../shared-ast-grep.js';

const MODULE_NAME = 'array.prototype.findindex';

/**
* @typedef {import('../../types.js').Codemod} Codemod
Expand All @@ -12,20 +17,33 @@ import { transformArrayMethod } from '../shared.js';
*/
export default function (options) {
return {
name: 'array.prototype.findindex',
name: MODULE_NAME,
to: 'native',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
const ast = ts.parse(file.source);
const root = ast.root();

const dirty = transformArrayMethod(
'array.prototype.findindex',
'findIndex',
const { imports, identifierName } = findDefaultImportIdentifier(
root,
MODULE_NAME,
);

if (!identifierName) {
return file.source;
}

const edits = computePolyfillMethodCallReplacementEdits(
root,
j,
identifierName,
'findIndex',
(args) => args.length >= 2,
);

return dirty ? root.toSource(options) : file.source;
for (const imp of imports) {
edits.push(imp.replace(''));
}

return edits.length > 0 ? root.commitEdits(edits) : file.source;
},
};
}
38 changes: 28 additions & 10 deletions codemods/array.prototype.findlast/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import jscodeshift from 'jscodeshift';
import { transformArrayMethod } from '../shared.js';
import { ts } from '@ast-grep/napi';
import {
computePolyfillMethodCallReplacementEdits,
findDefaultImportIdentifier,
} from '../shared-ast-grep.js';

const MODULE_NAME = 'array.prototype.findlast';

/**
* @typedef {import('../../types.js').Codemod} Codemod
Expand All @@ -12,20 +17,33 @@ import { transformArrayMethod } from '../shared.js';
*/
export default function (options) {
return {
name: 'array.prototype.findlast',
name: MODULE_NAME,
to: 'native',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
const ast = ts.parse(file.source);
const root = ast.root();

const dirty = transformArrayMethod(
'array.prototype.findlast',
'findLast',
const { imports, identifierName } = findDefaultImportIdentifier(
root,
MODULE_NAME,
);

if (!identifierName) {
return file.source;
}

const edits = computePolyfillMethodCallReplacementEdits(
root,
j,
identifierName,
'findLast',
(args) => args.length >= 2,
);

return dirty ? root.toSource(options) : file.source;
for (const imp of imports) {
edits.push(imp.replace(''));
}

return edits.length > 0 ? root.commitEdits(edits) : file.source;
},
};
}
2 changes: 1 addition & 1 deletion codemods/array.prototype.findlastindex/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default function (options) {
root,
identifierName,
'findLastIndex',
(args) => args.length === 2,
(args) => args.length >= 2,
);

for (const imp of imports) {
Expand Down
38 changes: 28 additions & 10 deletions codemods/array.prototype.flatmap/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import jscodeshift from 'jscodeshift';
import { transformArrayMethod } from '../shared.js';
import { ts } from '@ast-grep/napi';
import {
computePolyfillMethodCallReplacementEdits,
findDefaultImportIdentifier,
} from '../shared-ast-grep.js';

const MODULE_NAME = 'array.prototype.flatmap';

/**
* @typedef {import('../../types.js').Codemod} Codemod
Expand All @@ -12,20 +17,33 @@ import { transformArrayMethod } from '../shared.js';
*/
export default function (options) {
return {
name: 'array.prototype.flatmap',
name: MODULE_NAME,
to: 'native',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
const ast = ts.parse(file.source);
const root = ast.root();

const dirty = transformArrayMethod(
'array.prototype.flatmap',
'flatMap',
const { imports, identifierName } = findDefaultImportIdentifier(
root,
MODULE_NAME,
);

if (!identifierName) {
return file.source;
}

const edits = computePolyfillMethodCallReplacementEdits(
root,
j,
identifierName,
'flatMap',
(args) => args.length >= 1,
);

return dirty ? root.toSource(options) : file.source;
for (const imp of imports) {
edits.push(imp.replace(''));
}

return edits.length > 0 ? root.commitEdits(edits) : file.source;
},
};
}
38 changes: 28 additions & 10 deletions codemods/array.prototype.foreach/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import jscodeshift from 'jscodeshift';
import { transformArrayMethod } from '../shared.js';
import { ts } from '@ast-grep/napi';
import {
computePolyfillMethodCallReplacementEdits,
findDefaultImportIdentifier,
} from '../shared-ast-grep.js';

const MODULE_NAME = 'array.prototype.foreach';

/**
* @typedef {import('../../types.js').Codemod} Codemod
Expand All @@ -12,20 +17,33 @@ import { transformArrayMethod } from '../shared.js';
*/
export default function (options) {
return {
name: 'array.prototype.foreach',
name: MODULE_NAME,
to: 'native',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
const ast = ts.parse(file.source);
const root = ast.root();

const dirty = transformArrayMethod(
'array.prototype.foreach',
'forEach',
const { imports, identifierName } = findDefaultImportIdentifier(
root,
MODULE_NAME,
);

if (!identifierName) {
return file.source;
}

const edits = computePolyfillMethodCallReplacementEdits(
root,
j,
identifierName,
'forEach',
(args) => args.length >= 1,
);

return dirty ? root.toSource(options) : file.source;
for (const imp of imports) {
edits.push(imp.replace(''));
}

return edits.length > 0 ? root.commitEdits(edits) : file.source;
},
};
}
38 changes: 28 additions & 10 deletions codemods/array.prototype.indexof/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import jscodeshift from 'jscodeshift';
import { transformArrayMethod } from '../shared.js';
import { ts } from '@ast-grep/napi';
import {
computePolyfillMethodCallReplacementEdits,
findDefaultImportIdentifier,
} from '../shared-ast-grep.js';

const MODULE_NAME = 'array.prototype.indexof';

/**
* @typedef {import('../../types.js').Codemod} Codemod
Expand All @@ -12,20 +17,33 @@ import { transformArrayMethod } from '../shared.js';
*/
export default function (options) {
return {
name: 'array.prototype.indexof',
name: MODULE_NAME,
to: 'native',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
const ast = ts.parse(file.source);
const root = ast.root();

const dirty = transformArrayMethod(
'array.prototype.indexof',
'indexOf',
const { imports, identifierName } = findDefaultImportIdentifier(
root,
MODULE_NAME,
);

if (!identifierName) {
return file.source;
}

const edits = computePolyfillMethodCallReplacementEdits(
root,
j,
identifierName,
'indexOf',
(args) => args.length >= 2,
);

return dirty ? root.toSource(options) : file.source;
for (const imp of imports) {
edits.push(imp.replace(''));
}

return edits.length > 0 ? root.commitEdits(edits) : file.source;
},
};
}
38 changes: 28 additions & 10 deletions codemods/array.prototype.join/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import jscodeshift from 'jscodeshift';
import { transformArrayMethod } from '../shared.js';
import { ts } from '@ast-grep/napi';
import {
computePolyfillMethodCallReplacementEdits,
findDefaultImportIdentifier,
} from '../shared-ast-grep.js';

const MODULE_NAME = 'array.prototype.join';

/**
* @typedef {import('../../types.js').Codemod} Codemod
Expand All @@ -12,20 +17,33 @@ import { transformArrayMethod } from '../shared.js';
*/
export default function (options) {
return {
name: 'array.prototype.join',
name: MODULE_NAME,
to: 'native',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
const ast = ts.parse(file.source);
const root = ast.root();

const dirty = transformArrayMethod(
'array.prototype.join',
'join',
const { imports, identifierName } = findDefaultImportIdentifier(
root,
MODULE_NAME,
);

if (!identifierName) {
return file.source;
}

const edits = computePolyfillMethodCallReplacementEdits(
root,
j,
identifierName,
'join',
(args) => args.length >= 1,
);

return dirty ? root.toSource(options) : file.source;
for (const imp of imports) {
edits.push(imp.replace(''));
}

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