Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly simplify assignments with parentheses #3926

Merged
merged 1 commit into from Jan 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);