-
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.
Merge pull request #70 from filipre/symbol.prototype.description
`symbol.prototype.description`
- Loading branch information
Showing
7 changed files
with
106 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,67 @@ | ||
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: 'symbol.prototype.description', | ||
transform: ({ file }) => { | ||
const j = jscodeshift; | ||
const root = j(file.source); | ||
|
||
// Remove import/require statement | ||
const { identifier } = removeImport( | ||
'symbol.prototype.description', | ||
root, | ||
j, | ||
); | ||
|
||
// Remove description.shim() | ||
root | ||
.find(j.ExpressionStatement, { | ||
expression: { | ||
callee: { | ||
type: 'MemberExpression', | ||
object: { name: identifier }, | ||
property: { name: 'shim' }, | ||
}, | ||
}, | ||
}) | ||
.forEach((path) => { | ||
j(path).remove(); | ||
}); | ||
|
||
// Replace description(Symbol('foo')) with Symbol('foo').description | ||
root | ||
.find(j.CallExpression, { | ||
callee: { | ||
type: 'Identifier', | ||
name: identifier, | ||
}, | ||
}) | ||
.forEach((path) => { | ||
const args = path.node.arguments; | ||
const newArgs = | ||
args.length === 1 && j.CallExpression.check(args[0]) | ||
? args[0].arguments | ||
: []; | ||
path.replace( | ||
j.memberExpression( | ||
j.callExpression(j.identifier('Symbol'), newArgs), | ||
j.identifier('description'), | ||
), | ||
); | ||
}); | ||
|
||
return root.toSource({ quote: 'single' }); | ||
}, | ||
}; | ||
} |
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,6 @@ | ||
var assert = require('assert'); | ||
|
||
assert(Symbol('foo').description === 'foo'); | ||
assert(Symbol().description === undefined); | ||
assert(Symbol(undefined).description === undefined); | ||
assert(Symbol(null).description === 'null'); |
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 description = require('symbol.prototype.description'); | ||
var assert = require('assert'); | ||
|
||
assert(description(Symbol('foo')) === 'foo'); | ||
assert(description(Symbol()) === undefined); | ||
assert(description(Symbol(undefined)) === undefined); | ||
assert(description(Symbol(null)) === 'null'); |
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,6 @@ | ||
var assert = require('assert'); | ||
|
||
assert(Symbol('foo').description === 'foo'); | ||
assert(Symbol().description === undefined); | ||
assert(Symbol(undefined).description === undefined); | ||
assert(Symbol(null).description === 'null'); |
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,6 @@ | ||
var assert = require('assert'); | ||
|
||
assert(Symbol('foo').description === Symbol('foo').description); | ||
assert(Symbol().description === Symbol().description); | ||
assert(Symbol(undefined).description === Symbol(undefined).description); | ||
assert(Symbol(null).description === Symbol(null).description); |
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,8 @@ | ||
var description = require('symbol.prototype.description'); | ||
var assert = require('assert'); | ||
|
||
description.shim(); | ||
assert(description(Symbol('foo')) === Symbol('foo').description); | ||
assert(description(Symbol()) === Symbol().description); | ||
assert(description(Symbol(undefined)) === Symbol(undefined).description); | ||
assert(description(Symbol(null)) === Symbol(null).description); |
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,6 @@ | ||
var assert = require('assert'); | ||
|
||
assert(Symbol('foo').description === Symbol('foo').description); | ||
assert(Symbol().description === Symbol().description); | ||
assert(Symbol(undefined).description === Symbol(undefined).description); | ||
assert(Symbol(null).description === Symbol(null).description); |