Skip to content

Commit

Permalink
fix: remove referenced identifiers (#949)
Browse files Browse the repository at this point in the history
  • Loading branch information
luhc228 committed Aug 26, 2021
1 parent 0ca31d0 commit 21a810a
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 21 deletions.
4 changes: 4 additions & 0 deletions extensions/react-refactor/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## 1.0.2

- fix: remove referenced identifiers in ArrayPattern and ObjectPattern

## 1.0.1

- fix .vscodeignore configuration [#576](https://github.com/microsoft/vscode-vsce/issues/576)
Expand Down
2 changes: 1 addition & 1 deletion extensions/react-refactor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Easily refactor Component in React/Rax.",
"publisher": "iceworks-team",
"icon": "assets/logo.png",
"version": "1.0.1",
"version": "1.0.2",
"engines": {
"vscode": "^1.41.0"
},
Expand Down
8 changes: 4 additions & 4 deletions extensions/react-refactor/src/__tests__/suite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class CoverageRunner {
this.options = options;
}

public setupCoverage(): void {
setupCoverage(): void {
// Set up Code Coverage, hooking require so that instrumented code is returned
const self = this;
self.instrumenter = new istanbul.Instrumenter({ coverageVariable: self.coverageVar });
Expand Down Expand Up @@ -102,7 +102,7 @@ class CoverageRunner {
*
* @memberOf CoverageRunner
*/
public reportCoverage(): void {
reportCoverage(): void {
const self = this;
istanbul.hook.unhookRequire();
let cov: any;
Expand Down Expand Up @@ -173,11 +173,11 @@ export function run(): Promise<void> {
}

// Add files to the test suite
files.forEach(f => mocha.addFile(path.resolve(testsRoot, f)));
files.forEach((f) => mocha.addFile(path.resolve(testsRoot, f)));

try {
// Run the mocha test
mocha.run(failures => {
mocha.run((failures) => {
if (failures > 0) {
e(new Error(`${failures} tests failed.`));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,26 @@ export function removeUselessReferences(ast: any, originUnrefIdentifiers: string
}

if (path && path.node) {
binding.path.remove();
const { id } = path.node as any;
if (id && (t.isArrayPattern(id) || t.isObjectPattern(id))) {
// const [a, b] = [1, 2]
// const { a, b } = obj;
const { properties, elements } = id as any;
if (properties) {
const propertyIndex = properties.findIndex(({ key }) => key.name === name);
if (propertyIndex) {
properties.splice(propertyIndex, 1);
}
}
if (elements) {
const elementIndex = elements.findIndex((item) => item.name === name);
if (elementIndex) {
elements.splice(elementIndex, 1);
}
}
} else {
binding.path.remove();
}
}
} else {
// remove identifier in the parent scope
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
function handleValidIdentifier(identifierPath, callback) {
switch (identifierPath.parent.type) {
case 'ObjectProperty':
if (identifierPath.parent.key !== identifierPath.node) {
callback();
}
break;
case 'MemberExpression':
if (identifierPath.parent) {
switch (identifierPath.parent.type) {
case 'ObjectProperty':
if (identifierPath.parent.key !== identifierPath.node) {
callback();
}
break;
case 'MemberExpression':
// For list[index]
if (identifierPath.parent.computed) {
callback();
} else if (identifierPath.parent.property !== identifierPath.node) {
if (identifierPath.parent.computed) {
callback();
} else if (identifierPath.parent.property !== identifierPath.node) {
callback();
}
break;
default:
callback();
}
break;
default:
callback();
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion extensions/react-refactor/src/utils/prettierFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function prettierFormat(code: string) {
return prettier.format(code, {
singleQuote: true,
trailingComma: 'es5',
parser: 'babel',
parser: 'typescript',
});
}

Expand Down

0 comments on commit 21a810a

Please sign in to comment.