-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
231 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/** | ||
* @typedef {import('../../types.js').Codemod} Codemod | ||
* @typedef {import('../../types.js').CodemodOptions} CodemodOptions | ||
*/ | ||
|
||
/** | ||
* @param {CodemodOptions} [options] | ||
* @returns {Codemod} | ||
*/ | ||
export default function (options) { | ||
return { | ||
name: 'is-boolean-object', | ||
transform: ({ file, jscodeshift }) => { | ||
const j = jscodeshift; | ||
const root = j(file.source); | ||
let dirtyFlag = false; | ||
|
||
// Find all require statements for 'is-boolean-object' | ||
root | ||
.find(j.VariableDeclaration) | ||
.filter((path) => | ||
path.value.declarations.some( | ||
(decl) => | ||
j.VariableDeclarator.check(decl) && | ||
decl.init && | ||
j.CallExpression.check(decl.init) && | ||
j.Identifier.check(decl.init.callee) && | ||
decl.init.callee.name === 'require' && | ||
decl.init.arguments.length > 0 && | ||
j.Literal.check(decl.init.arguments[0]) && | ||
decl.init.arguments[0].value === 'is-boolean-object', | ||
), | ||
) | ||
.forEach((path) => { | ||
// Remove the require statement for 'is-boolean-object' | ||
path.prune(); | ||
dirtyFlag = true; | ||
}); | ||
|
||
// Remove ESM import statement for 'is-boolean-object' | ||
root | ||
.find(j.ImportDeclaration, { | ||
source: { value: 'is-boolean-object' }, | ||
}) | ||
.forEach((path) => { | ||
j(path).remove(); | ||
dirtyFlag = true; | ||
}); | ||
|
||
// Replace all calls to isBoolean with Object.prototype.toString.call | ||
root | ||
.find(j.CallExpression, { | ||
callee: { | ||
type: 'Identifier', | ||
name: 'isBoolean', | ||
}, | ||
}) | ||
.replaceWith((path) => { | ||
const arg = path.node.arguments[0]; | ||
return j.callExpression( | ||
j.memberExpression( | ||
j.memberExpression( | ||
j.memberExpression( | ||
j.identifier('Object'), | ||
j.identifier('prototype'), | ||
), | ||
j.identifier('toString'), | ||
), | ||
j.identifier('call'), | ||
), | ||
[arg], | ||
); | ||
}) | ||
.forEach((path) => { | ||
const parent = path.parent.node; | ||
if (j.BinaryExpression.check(parent)) { | ||
parent.operator = '==='; | ||
parent.right = j.literal('[object Boolean]'); | ||
} else if ( | ||
j.CallExpression.check(parent) && | ||
parent.arguments.length === 1 | ||
) { | ||
const newExpression = j.binaryExpression( | ||
'===', | ||
path.node, | ||
j.literal('[object Boolean]'), | ||
); | ||
parent.arguments[0] = newExpression; | ||
} | ||
}); | ||
|
||
return dirtyFlag ? root.toSource({ quote: 'single' }) : file.source; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
var assert = require('assert'); | ||
|
||
assert.notOk(Object.prototype.toString.call(undefined) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call(null) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call('foo') === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call(function () {}) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call([]) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call({}) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call(/a/g) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call(new RegExp('a', 'g')) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call(new Date()) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call(42) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call(NaN) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call(Infinity) === '[object Boolean]'); | ||
|
||
assert.ok(Object.prototype.toString.call(new Boolean(42)) === '[object Boolean]'); | ||
assert.ok(Object.prototype.toString.call(false) === '[object Boolean]'); | ||
assert.ok(Object.prototype.toString.call(Object(false)) === '[object Boolean]'); | ||
assert.ok(Object.prototype.toString.call(true) === '[object Boolean]'); | ||
assert.ok(Object.prototype.toString.call(Object(true)) === '[object Boolean]'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
var assert = require('assert'); | ||
var isBoolean = require('is-boolean-object'); | ||
|
||
assert.notOk(isBoolean(undefined)); | ||
assert.notOk(isBoolean(null)); | ||
assert.notOk(isBoolean('foo')); | ||
assert.notOk(isBoolean(function () {})); | ||
assert.notOk(isBoolean([])); | ||
assert.notOk(isBoolean({})); | ||
assert.notOk(isBoolean(/a/g)); | ||
assert.notOk(isBoolean(new RegExp('a', 'g'))); | ||
assert.notOk(isBoolean(new Date())); | ||
assert.notOk(isBoolean(42)); | ||
assert.notOk(isBoolean(NaN)); | ||
assert.notOk(isBoolean(Infinity)); | ||
|
||
assert.ok(isBoolean(new Boolean(42))); | ||
assert.ok(isBoolean(false)); | ||
assert.ok(isBoolean(Object(false))); | ||
assert.ok(isBoolean(true)); | ||
assert.ok(isBoolean(Object(true))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
var assert = require('assert'); | ||
|
||
assert.notOk(Object.prototype.toString.call(undefined) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call(null) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call('foo') === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call(function () {}) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call([]) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call({}) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call(/a/g) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call(new RegExp('a', 'g')) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call(new Date()) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call(42) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call(NaN) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call(Infinity) === '[object Boolean]'); | ||
|
||
assert.ok(Object.prototype.toString.call(new Boolean(42)) === '[object Boolean]'); | ||
assert.ok(Object.prototype.toString.call(false) === '[object Boolean]'); | ||
assert.ok(Object.prototype.toString.call(Object(false)) === '[object Boolean]'); | ||
assert.ok(Object.prototype.toString.call(true) === '[object Boolean]'); | ||
assert.ok(Object.prototype.toString.call(Object(true)) === '[object Boolean]'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import assert from 'assert'; | ||
|
||
assert.notOk(Object.prototype.toString.call(undefined) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call(null) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call('foo') === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call(function () {}) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call([]) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call({}) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call(/a/g) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call(new RegExp('a', 'g')) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call(new Date()) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call(42) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call(NaN) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call(Infinity) === '[object Boolean]'); | ||
|
||
assert.ok(Object.prototype.toString.call(new Boolean(42)) === '[object Boolean]'); | ||
assert.ok(Object.prototype.toString.call(false) === '[object Boolean]'); | ||
assert.ok(Object.prototype.toString.call(Object(false)) === '[object Boolean]'); | ||
assert.ok(Object.prototype.toString.call(true) === '[object Boolean]'); | ||
assert.ok(Object.prototype.toString.call(Object(true)) === '[object Boolean]'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import assert from 'assert'; | ||
import isBoolean from 'is-boolean-object'; | ||
|
||
assert.notOk(isBoolean(undefined)); | ||
assert.notOk(isBoolean(null)); | ||
assert.notOk(isBoolean('foo')); | ||
assert.notOk(isBoolean(function () {})); | ||
assert.notOk(isBoolean([])); | ||
assert.notOk(isBoolean({})); | ||
assert.notOk(isBoolean(/a/g)); | ||
assert.notOk(isBoolean(new RegExp('a', 'g'))); | ||
assert.notOk(isBoolean(new Date())); | ||
assert.notOk(isBoolean(42)); | ||
assert.notOk(isBoolean(NaN)); | ||
assert.notOk(isBoolean(Infinity)); | ||
|
||
assert.ok(isBoolean(new Boolean(42))); | ||
assert.ok(isBoolean(false)); | ||
assert.ok(isBoolean(Object(false))); | ||
assert.ok(isBoolean(true)); | ||
assert.ok(isBoolean(Object(true))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import assert from 'assert'; | ||
|
||
assert.notOk(Object.prototype.toString.call(undefined) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call(null) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call('foo') === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call(function () {}) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call([]) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call({}) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call(/a/g) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call(new RegExp('a', 'g')) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call(new Date()) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call(42) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call(NaN) === '[object Boolean]'); | ||
assert.notOk(Object.prototype.toString.call(Infinity) === '[object Boolean]'); | ||
|
||
assert.ok(Object.prototype.toString.call(new Boolean(42)) === '[object Boolean]'); | ||
assert.ok(Object.prototype.toString.call(false) === '[object Boolean]'); | ||
assert.ok(Object.prototype.toString.call(Object(false)) === '[object Boolean]'); | ||
assert.ok(Object.prototype.toString.call(true) === '[object Boolean]'); | ||
assert.ok(Object.prototype.toString.call(Object(true)) === '[object Boolean]'); |