Skip to content

Commit

Permalink
feat: add has-proto, has-symbols, and has-tostringtag
Browse files Browse the repository at this point in the history
  • Loading branch information
ronanru committed Jul 18, 2024
1 parent 7fe4152 commit 613490b
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 0 deletions.
40 changes: 40 additions & 0 deletions codemods/has-proto/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: 'has-proto',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
let dirtyFlag = false;

const { identifier } = removeImport('has-proto', 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;
},
};
}
45 changes: 45 additions & 0 deletions codemods/has-symbols/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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: 'has-symbols',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
let dirtyFlag = false;

const { identifier } = removeImport('has-symbols', root, j);
const { identifier: identifierShams } = removeImport(
'has-symbols/shams',
root,
j,
);
[identifier, identifierShams].forEach((identifier) => {
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;
},
};
}
45 changes: 45 additions & 0 deletions codemods/has-tostringtag/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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: 'has-tostringtag',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
let dirtyFlag = false;

const { identifier } = removeImport('has-tostringtag', root, j);
const { identifier: identifierShams } = removeImport(
'has-tostringtag/shams',
root,
j,
);
[identifier, identifierShams].forEach((identifier) => {
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;
},
};
}
9 changes: 9 additions & 0 deletions codemods/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ import functionsHaveNames from './functions-have-names/index.js';

import gopd from './gopd/index.js';

import hasProto from './has-proto/index.js';

import hasSymbols from './has-symbols/index.js';

import hasTostringtag from './has-tostringtag/index.js';

export const codemods = {
'is-whitespace': isWhitespace,
'is-array-buffer': isArrayBuffer,
Expand All @@ -62,4 +68,7 @@ export const codemods = {
'function.prototype.name': functionPrototypeName,
'functions-have-names': functionsHaveNames,
gopd: gopd,
'has-proto': hasProto,
'has-symbols': hasSymbols,
'has-tostringtag': hasTostringtag,
};
3 changes: 3 additions & 0 deletions test/fixtures/has-proto/case-1/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var assert = require("assert");

assert.equal(typeof true, "boolean");
4 changes: 4 additions & 0 deletions test/fixtures/has-proto/case-1/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var hasProto = require("has-proto");
var assert = require("assert");

assert.equal(typeof hasProto(), "boolean");
3 changes: 3 additions & 0 deletions test/fixtures/has-proto/case-1/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var assert = require("assert");

assert.equal(typeof true, "boolean");
3 changes: 3 additions & 0 deletions test/fixtures/has-symbols/case-1/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
true === true;

true === true;
5 changes: 5 additions & 0 deletions test/fixtures/has-symbols/case-1/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var hasSymbols = require("has-symbols");
hasSymbols() === true;

var hasSymbolsKinda = require("has-symbols/shams");
hasSymbolsKinda() === true;
3 changes: 3 additions & 0 deletions test/fixtures/has-symbols/case-1/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
true === true;

true === true;
3 changes: 3 additions & 0 deletions test/fixtures/has-tostringtag/case-1/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
true === true;

true === true;
5 changes: 5 additions & 0 deletions test/fixtures/has-tostringtag/case-1/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var hasSymbolToStringTag = require("has-tostringtag");
hasSymbolToStringTag() === true;

var hasSymbolToStringTagKinda = require("has-tostringtag/shams");
hasSymbolToStringTagKinda() === true;
3 changes: 3 additions & 0 deletions test/fixtures/has-tostringtag/case-1/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
true === true;

true === true;

0 comments on commit 613490b

Please sign in to comment.