-
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.
feat: add `es-set-tostringtag`
- Loading branch information
Showing
4 changed files
with
149 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
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: 'es-set-tostringtag', | ||
transform: ({ file }) => { | ||
const j = jscodeshift; | ||
const root = j(file.source); | ||
let dirtyFlag = false; | ||
|
||
const { identifier } = removeImport('es-set-tostringtag', root, j); | ||
|
||
root | ||
.find(j.CallExpression, { | ||
callee: { | ||
type: 'Identifier', | ||
name: identifier, | ||
}, | ||
}) | ||
.forEach((path) => { | ||
const args = path.value.arguments; | ||
const newExpression = j.callExpression( | ||
j.memberExpression( | ||
j.identifier('Object'), | ||
j.identifier('defineProperty'), | ||
), | ||
[ | ||
args[0], | ||
j.memberExpression( | ||
j.identifier('Symbol'), | ||
j.identifier('toStringTag'), | ||
), | ||
j.objectExpression([ | ||
j.property( | ||
'init', | ||
j.identifier('configurable'), | ||
j.booleanLiteral(true), | ||
), | ||
// @ts-expect-error | ||
j.property('init', j.identifier('value'), args[1]), | ||
]), | ||
], | ||
); | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const assert = require("assert"); | ||
|
||
const obj = {}; | ||
const sentinel = {}; | ||
|
||
Object.defineProperty(obj, Symbol.toStringTag, { | ||
configurable: true, | ||
value: sentinel, | ||
}); | ||
|
||
assert.equal( | ||
obj[Symbol.toStringTag], | ||
sentinel, | ||
"toStringTag property is as expected" | ||
); | ||
|
||
assert.equal(String(obj), "[object Object]", "toStringTag works"); | ||
|
||
var tagged = {}; | ||
tagged[Symbol.toStringTag] = "already tagged"; | ||
assert.equal(String(tagged), "[object already tagged]", "toStringTag works"); | ||
|
||
Object.defineProperty(tagged, Symbol.toStringTag, { | ||
configurable: true, | ||
value: "new tag", | ||
}); | ||
assert.equal( | ||
String(tagged), | ||
"[object already tagged]", | ||
"toStringTag is unchanged" | ||
); |
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 @@ | ||
const assert = require("assert"); | ||
const setToStringTag = require("es-set-tostringtag"); | ||
|
||
const obj = {}; | ||
const sentinel = {}; | ||
|
||
setToStringTag(obj, sentinel); | ||
|
||
assert.equal( | ||
obj[Symbol.toStringTag], | ||
sentinel, | ||
"toStringTag property is as expected" | ||
); | ||
|
||
assert.equal(String(obj), "[object Object]", "toStringTag works"); | ||
|
||
var tagged = {}; | ||
tagged[Symbol.toStringTag] = "already tagged"; | ||
assert.equal(String(tagged), "[object already tagged]", "toStringTag works"); | ||
|
||
setToStringTag(tagged, "new tag"); | ||
assert.equal( | ||
String(tagged), | ||
"[object already tagged]", | ||
"toStringTag is unchanged" | ||
); |
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,31 @@ | ||
const assert = require("assert"); | ||
|
||
const obj = {}; | ||
const sentinel = {}; | ||
|
||
Object.defineProperty(obj, Symbol.toStringTag, { | ||
configurable: true, | ||
value: sentinel | ||
}); | ||
|
||
assert.equal( | ||
obj[Symbol.toStringTag], | ||
sentinel, | ||
"toStringTag property is as expected" | ||
); | ||
|
||
assert.equal(String(obj), "[object Object]", "toStringTag works"); | ||
|
||
var tagged = {}; | ||
tagged[Symbol.toStringTag] = "already tagged"; | ||
assert.equal(String(tagged), "[object already tagged]", "toStringTag works"); | ||
|
||
Object.defineProperty(tagged, Symbol.toStringTag, { | ||
configurable: true, | ||
value: "new tag" | ||
}); | ||
assert.equal( | ||
String(tagged), | ||
"[object already tagged]", | ||
"toStringTag is unchanged" | ||
); |