Skip to content

Commit be76cbc

Browse files
authored
Merge pull request #50 from MiniCheese26/feat/add-get-symbol-description
feat: Add `get-symbol-description`
2 parents 98460e9 + d161b9d commit be76cbc

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import jscodeshift from 'jscodeshift';
2+
import { removeImport } from '../shared.js';
3+
4+
/**
5+
* @typedef {import('../../types.js').Codemod} Codemod
6+
* @typedef {import('../../types.js').CodemodOptions} CodemodOptions
7+
*/
8+
9+
/**
10+
* @param {CodemodOptions} [options]
11+
* @returns {Codemod}
12+
*/
13+
export default function (options) {
14+
return {
15+
name: 'get-symbol-description',
16+
transform: ({ file }) => {
17+
const j = jscodeshift;
18+
const root = j(file.source);
19+
let dirtyFlag = false;
20+
21+
const { identifier } = removeImport('get-symbol-description', root, j);
22+
23+
root
24+
.find(j.CallExpression, {
25+
callee: {
26+
type: 'Identifier',
27+
name: identifier,
28+
},
29+
})
30+
.forEach((path) => {
31+
const args = path.value.arguments;
32+
if (args.length === 1) {
33+
const newExpression = j.memberExpression(
34+
//@ts-ignore
35+
args[0],
36+
j.identifier('description'),
37+
);
38+
39+
j(path).replaceWith(newExpression);
40+
dirtyFlag = true;
41+
}
42+
});
43+
44+
return dirtyFlag ? root.toSource(options) : file.source;
45+
},
46+
};
47+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var assert = require("assert");
2+
3+
var testSymbol = symbol("foo");
4+
5+
assert.equal(
6+
testSymbol.description,
7+
"foo",
8+
);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var symbolDescription = require("get-symbol-description");
2+
var assert = require("assert");
3+
4+
var testSymbol = symbol("foo");
5+
6+
assert.equal(
7+
symbolDescription(testSymbol),
8+
"foo",
9+
);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var assert = require("assert");
2+
3+
var testSymbol = symbol("foo");
4+
5+
assert.equal(
6+
testSymbol.description,
7+
"foo",
8+
);

0 commit comments

Comments
 (0)