Skip to content

Commit

Permalink
Merge pull request #70 from filipre/symbol.prototype.description
Browse files Browse the repository at this point in the history
`symbol.prototype.description`
  • Loading branch information
thepassle authored Jul 31, 2024
2 parents 90fdb79 + 6a22a47 commit 7813711
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 0 deletions.
67 changes: 67 additions & 0 deletions codemods/symbol.prototype.description/index.js
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' });
},
};
}
6 changes: 6 additions & 0 deletions test/fixtures/symbol.prototype.description/case-1/after.js
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');
7 changes: 7 additions & 0 deletions test/fixtures/symbol.prototype.description/case-1/before.js
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');
6 changes: 6 additions & 0 deletions test/fixtures/symbol.prototype.description/case-1/result.js
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');
6 changes: 6 additions & 0 deletions test/fixtures/symbol.prototype.description/case-2/after.js
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);
8 changes: 8 additions & 0 deletions test/fixtures/symbol.prototype.description/case-2/before.js
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);
6 changes: 6 additions & 0 deletions test/fixtures/symbol.prototype.description/case-2/result.js
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);

0 comments on commit 7813711

Please sign in to comment.