Skip to content

Commit

Permalink
Merge pull request #17 from ronanru/main
Browse files Browse the repository at this point in the history
feat: add functions-have-names
  • Loading branch information
thepassle authored Jul 18, 2024
2 parents 3eb4689 + 6502d65 commit 02d874c
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 0 deletions.
40 changes: 40 additions & 0 deletions codemods/functions-have-names/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import jscodeshift from 'jscodeshift';
import { removeImport } from '../shared.js';

/**
* @typedef {import('../../types.js').Codemod} Codemod
* @typedef {import('../../types.js').CodemodOptions} CodemodOptions
*/

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

const { identifier } = removeImport('functions-have-names', root, j);

root
.find(j.CallExpression, {
callee: {
type: 'Identifier',
name: identifier,
},
})
.forEach((path) => {
const newExpression = j.booleanLiteral(true);

j(path).replaceWith(newExpression);
dirtyFlag = true;
});

return dirtyFlag ? root.toSource(options) : file.source;
},
};
}
3 changes: 3 additions & 0 deletions codemods/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import arrayPrototypeEvery from './array.prototype.every/index.js';

import functionPrototypeName from './function.prototype.name/index.js';

import functionsHaveNames from './functions-have-names/index.js';

export const codemods = {
'is-whitespace': isWhitespace,
'is-array-buffer': isArrayBuffer,
Expand All @@ -56,4 +58,5 @@ export const codemods = {
'array.prototype.reduceright': arrayPrototypeReduceright,
'array.prototype.every': arrayPrototypeEvery,
'function.prototype.name': functionPrototypeName,
'functions-have-names': functionsHaveNames,
};
3 changes: 3 additions & 0 deletions test/fixtures/functions-have-names/case-1/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var assert = require("assert");

assert.equal(true, true);
4 changes: 4 additions & 0 deletions test/fixtures/functions-have-names/case-1/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var functionsHaveNames = require("functions-have-names");
var assert = require("assert");

assert.equal(functionsHaveNames(), true);
3 changes: 3 additions & 0 deletions test/fixtures/functions-have-names/case-1/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var assert = require("assert");

assert.equal(true, true);

0 comments on commit 02d874c

Please sign in to comment.