-
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
15 changed files
with
262 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
import isWhitespace from './is-whitespace/index.js'; | ||
|
||
import isArrayBuffer from './is-array-buffer/index.js'; | ||
|
||
export const codemods = { | ||
'is-whitespace': isWhitespace, | ||
'is-array-buffer': isArrayBuffer, | ||
}; |
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,82 @@ | ||
/** | ||
* @typedef {import('../../types.js').Codemod} Codemod | ||
* @typedef {import('../../types.js').CodemodOptions} CodemodOptions | ||
*/ | ||
|
||
/** | ||
* @param {CodemodOptions} [options] | ||
* @returns {Codemod} | ||
*/ | ||
export default function (options) { | ||
return { | ||
name: 'is-array-buffer', | ||
transform: ({ file, jscodeshift }) => { | ||
const j = jscodeshift; | ||
const root = j(file.source); | ||
let dirtyFlag = false; | ||
|
||
// Remove the require statement for 'is-array-buffer' | ||
root.find(j.VariableDeclaration).forEach((path) => { | ||
const declarations = path.value.declarations; | ||
if ( | ||
declarations.length === 1 && | ||
j.VariableDeclarator.check(declarations[0]) | ||
) { | ||
const declarator = declarations[0]; | ||
if ( | ||
j.CallExpression.check(declarator.init) && | ||
j.Identifier.check(declarator.id) | ||
) { | ||
const callExpr = declarator.init; | ||
if ( | ||
j.Identifier.check(callExpr.callee) && | ||
callExpr.callee.name === 'require' | ||
) { | ||
const args = callExpr.arguments; | ||
if ( | ||
args.length === 1 && | ||
j.Literal.check(args[0]) && | ||
args[0].value === 'is-array-buffer' | ||
) { | ||
j(path).remove(); | ||
dirtyFlag = true; | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
|
||
// Remove ESM import statement for 'is-array-buffer' | ||
root.find(j.ImportDeclaration, { | ||
source: { value: 'is-array-buffer' } | ||
}).forEach(path => { | ||
j(path).remove(); | ||
dirtyFlag = true; | ||
}); | ||
|
||
// Replace isArrayBuffer calls with (foo instanceof ArrayBuffer) | ||
root | ||
.find(j.CallExpression, { | ||
callee: { | ||
type: 'Identifier', | ||
name: 'isArrayBuffer', | ||
}, | ||
}) | ||
.forEach((path) => { | ||
const args = path.value.arguments; | ||
if (args.length === 1) { | ||
const arg = args[0]; | ||
const newExpression = j.binaryExpression( | ||
'instanceof', | ||
arg, | ||
j.identifier('ArrayBuffer'), | ||
); | ||
j(path).replaceWith(newExpression); | ||
dirtyFlag = true; | ||
} | ||
}); | ||
|
||
return dirtyFlag ? root.toSource(options) : 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
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,26 @@ | ||
var assert = require('assert'); | ||
|
||
assert(!(function () {} instanceof ArrayBuffer)); | ||
assert(!(null instanceof ArrayBuffer)); | ||
assert( | ||
!(function* () { | ||
yield 42; | ||
return Infinity; | ||
} instanceof ArrayBuffer), | ||
); | ||
assert(!(Symbol('foo') instanceof ArrayBuffer)); | ||
assert(!(1n instanceof ArrayBuffer)); | ||
assert(!(Object(1n) instanceof ArrayBuffer)); | ||
|
||
assert(!(new Set() instanceof ArrayBuffer)); | ||
assert(!(new WeakSet() instanceof ArrayBuffer)); | ||
assert(!(new Map() instanceof ArrayBuffer)); | ||
assert(!(new WeakMap() instanceof ArrayBuffer)); | ||
assert(!(new WeakRef({}) instanceof ArrayBuffer)); | ||
assert(!(new FinalizationRegistry(() => {}) instanceof ArrayBuffer)); | ||
assert(!(new SharedArrayBuffer() instanceof ArrayBuffer)); | ||
|
||
assert(new ArrayBuffer() instanceof ArrayBuffer); | ||
|
||
class MyArrayBuffer extends ArrayBuffer {} | ||
assert(new MyArrayBuffer() instanceof ArrayBuffer); |
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,27 @@ | ||
var assert = require('assert'); | ||
var isArrayBuffer = require('is-array-buffer'); | ||
|
||
assert(!isArrayBuffer(function () {})); | ||
assert(!isArrayBuffer(null)); | ||
assert( | ||
!isArrayBuffer(function* () { | ||
yield 42; | ||
return Infinity; | ||
}), | ||
); | ||
assert(!isArrayBuffer(Symbol('foo'))); | ||
assert(!isArrayBuffer(1n)); | ||
assert(!isArrayBuffer(Object(1n))); | ||
|
||
assert(!isArrayBuffer(new Set())); | ||
assert(!isArrayBuffer(new WeakSet())); | ||
assert(!isArrayBuffer(new Map())); | ||
assert(!isArrayBuffer(new WeakMap())); | ||
assert(!isArrayBuffer(new WeakRef({}))); | ||
assert(!isArrayBuffer(new FinalizationRegistry(() => {}))); | ||
assert(!isArrayBuffer(new SharedArrayBuffer())); | ||
|
||
assert(isArrayBuffer(new ArrayBuffer())); | ||
|
||
class MyArrayBuffer extends ArrayBuffer {} | ||
assert(isArrayBuffer(new MyArrayBuffer())); |
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,26 @@ | ||
var assert = require('assert'); | ||
|
||
assert(!(function () {} instanceof ArrayBuffer)); | ||
assert(!(null instanceof ArrayBuffer)); | ||
assert( | ||
!(function* () { | ||
yield 42; | ||
return Infinity; | ||
} instanceof ArrayBuffer), | ||
); | ||
assert(!(Symbol('foo') instanceof ArrayBuffer)); | ||
assert(!(1n instanceof ArrayBuffer)); | ||
assert(!(Object(1n) instanceof ArrayBuffer)); | ||
|
||
assert(!(new Set() instanceof ArrayBuffer)); | ||
assert(!(new WeakSet() instanceof ArrayBuffer)); | ||
assert(!(new Map() instanceof ArrayBuffer)); | ||
assert(!(new WeakMap() instanceof ArrayBuffer)); | ||
assert(!(new WeakRef({}) instanceof ArrayBuffer)); | ||
assert(!(new FinalizationRegistry(() => {}) instanceof ArrayBuffer)); | ||
assert(!(new SharedArrayBuffer() instanceof ArrayBuffer)); | ||
|
||
assert(new ArrayBuffer() instanceof ArrayBuffer); | ||
|
||
class MyArrayBuffer extends ArrayBuffer {} | ||
assert(new MyArrayBuffer() instanceof ArrayBuffer); |
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,26 @@ | ||
var assert = require('assert'); | ||
|
||
assert(!(function () {} instanceof ArrayBuffer)); | ||
assert(!(null instanceof ArrayBuffer)); | ||
assert( | ||
!(function* () { | ||
yield 42; | ||
return Infinity; | ||
} instanceof ArrayBuffer), | ||
); | ||
assert(!(Symbol('foo') instanceof ArrayBuffer)); | ||
assert(!(1n instanceof ArrayBuffer)); | ||
assert(!(Object(1n) instanceof ArrayBuffer)); | ||
|
||
assert(!(new Set() instanceof ArrayBuffer)); | ||
assert(!(new WeakSet() instanceof ArrayBuffer)); | ||
assert(!(new Map() instanceof ArrayBuffer)); | ||
assert(!(new WeakMap() instanceof ArrayBuffer)); | ||
assert(!(new WeakRef({}) instanceof ArrayBuffer)); | ||
assert(!(new FinalizationRegistry(() => {}) instanceof ArrayBuffer)); | ||
assert(!(new SharedArrayBuffer() instanceof ArrayBuffer)); | ||
|
||
assert(new ArrayBuffer() instanceof ArrayBuffer); | ||
|
||
class MyArrayBuffer extends ArrayBuffer {} | ||
assert(new MyArrayBuffer() instanceof ArrayBuffer); |
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,27 @@ | ||
var assert = require('assert'); | ||
import isArrayBuffer from 'is-array-buffer'; | ||
|
||
assert(!isArrayBuffer(function () {})); | ||
assert(!isArrayBuffer(null)); | ||
assert( | ||
!isArrayBuffer(function* () { | ||
yield 42; | ||
return Infinity; | ||
}), | ||
); | ||
assert(!isArrayBuffer(Symbol('foo'))); | ||
assert(!isArrayBuffer(1n)); | ||
assert(!isArrayBuffer(Object(1n))); | ||
|
||
assert(!isArrayBuffer(new Set())); | ||
assert(!isArrayBuffer(new WeakSet())); | ||
assert(!isArrayBuffer(new Map())); | ||
assert(!isArrayBuffer(new WeakMap())); | ||
assert(!isArrayBuffer(new WeakRef({}))); | ||
assert(!isArrayBuffer(new FinalizationRegistry(() => {}))); | ||
assert(!isArrayBuffer(new SharedArrayBuffer())); | ||
|
||
assert(isArrayBuffer(new ArrayBuffer())); | ||
|
||
class MyArrayBuffer extends ArrayBuffer {} | ||
assert(isArrayBuffer(new MyArrayBuffer())); |
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,26 @@ | ||
var assert = require('assert'); | ||
|
||
assert(!(function () {} instanceof ArrayBuffer)); | ||
assert(!(null instanceof ArrayBuffer)); | ||
assert( | ||
!(function* () { | ||
yield 42; | ||
return Infinity; | ||
} instanceof ArrayBuffer), | ||
); | ||
assert(!(Symbol('foo') instanceof ArrayBuffer)); | ||
assert(!(1n instanceof ArrayBuffer)); | ||
assert(!(Object(1n) instanceof ArrayBuffer)); | ||
|
||
assert(!(new Set() instanceof ArrayBuffer)); | ||
assert(!(new WeakSet() instanceof ArrayBuffer)); | ||
assert(!(new Map() instanceof ArrayBuffer)); | ||
assert(!(new WeakMap() instanceof ArrayBuffer)); | ||
assert(!(new WeakRef({}) instanceof ArrayBuffer)); | ||
assert(!(new FinalizationRegistry(() => {}) instanceof ArrayBuffer)); | ||
assert(!(new SharedArrayBuffer() instanceof ArrayBuffer)); | ||
|
||
assert(new ArrayBuffer() instanceof ArrayBuffer); | ||
|
||
class MyArrayBuffer extends ArrayBuffer {} | ||
assert(new MyArrayBuffer() instanceof ArrayBuffer); |
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,5 @@ | ||
let str = ' '; | ||
let str2 = 'Hello'; | ||
|
||
console.log(str1.trim() === ''); // true | ||
console.log(str2.trim() === ''); // false |
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,7 @@ | ||
var isWhitespace = require('is-whitespace'); | ||
|
||
let str = ' '; | ||
let str2 = 'Hello'; | ||
|
||
console.log(isWhitespace(str1)); // true | ||
console.log(isWhitespace(str2)); // false |
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,5 @@ | ||
let str = ' '; | ||
let str2 = 'Hello'; | ||
|
||
console.log(str1.trim() === ''); // true | ||
console.log(str2.trim() === ''); // false |