Skip to content

Commit

Permalink
Correctly simplify assignments with parentheses (#3926)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Jan 16, 2021
1 parent d660063 commit d70415f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/ast/nodes/AssignmentExpression.ts
Expand Up @@ -76,7 +76,8 @@ export default class AssignmentExpression extends NodeBase {
this.right.render(code, options, {
renderedParentType: renderedParentType || this.parent.type
});
code.remove(this.start, this.right.start);
const operatorPos = findFirstOccurrenceOutsideComment(code.original, '=', this.left.end);
code.remove(this.start, findNonWhiteSpace(code.original, operatorPos + 1));
}
if (options.format === 'system') {
const exportNames =
Expand Down
7 changes: 4 additions & 3 deletions src/utils/renderHelpers.ts
Expand Up @@ -50,11 +50,12 @@ export function findFirstOccurrenceOutsideComment(code: string, searchString: st
}
}

const WHITESPACE = /\s/;
const NON_WHITESPACE = /\S/g;

export function findNonWhiteSpace(code: string, index: number) {
while (index < code.length && WHITESPACE.test(code[index])) index++;
return index;
NON_WHITESPACE.lastIndex = index;
const result = NON_WHITESPACE.exec(code)!;
return result.index;
}

// This assumes "code" only contains white-space and comments
Expand Down
@@ -0,0 +1,6 @@
module.exports = {
description: 'correctly simplifies assignments with right-hand-sides in parentheses (#3924)',
context: {
someObject: { isTrue: true }
}
};
2 changes: 2 additions & 0 deletions test/function/samples/unused-parenthesed-assignment/main.js
@@ -0,0 +1,2 @@
let unused = false;
unused = (someObject.isTrue === true);

0 comments on commit d70415f

Please sign in to comment.