From 950f85515bef01b021cc1edda7ac6993d3cc5fa5 Mon Sep 17 00:00:00 2001 From: XiaoPi <530257315@qq.com> Date: Sun, 21 Jul 2024 22:51:17 +0800 Subject: [PATCH] fix: find whitespace from operator position to start --- src/ast/nodes/LogicalExpression.ts | 13 +++++++------ src/utils/renderHelpers.ts | 16 ++++++++++++++++ .../_config.js | 4 ++++ .../_expected.js | 5 +++++ .../main.js | 4 ++++ 5 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 test/form/samples/remove-whitespace-of-logical-expression/_config.js create mode 100644 test/form/samples/remove-whitespace-of-logical-expression/_expected.js create mode 100644 test/form/samples/remove-whitespace-of-logical-expression/main.js diff --git a/src/ast/nodes/LogicalExpression.ts b/src/ast/nodes/LogicalExpression.ts index 62e9b8781b59..e5fa9d6f5349 100644 --- a/src/ast/nodes/LogicalExpression.ts +++ b/src/ast/nodes/LogicalExpression.ts @@ -2,6 +2,7 @@ import type MagicString from 'magic-string'; import { BLANK, EMPTY_ARRAY } from '../../utils/blank'; import { findFirstOccurrenceOutsideComment, + findLastWhiteSpaceReverse, findNonWhiteSpace, type NodeRenderOptions, removeLineBreaks, @@ -185,12 +186,12 @@ export default class LogicalExpression extends NodeBase implements Deoptimizable }: NodeRenderOptions = BLANK ): void { if (!this.left.included || !this.right.included) { + const operatorPos = findFirstOccurrenceOutsideComment( + code.original, + this.operator, + this.left.end + ); if (this.right.included) { - const operatorPos = findFirstOccurrenceOutsideComment( - code.original, - this.operator, - this.left.end - ); const removePos = findNonWhiteSpace(code.original, operatorPos + 2); code.remove(this.start, removePos); if (preventASI) { @@ -198,7 +199,7 @@ export default class LogicalExpression extends NodeBase implements Deoptimizable } this.left.removeAnnotations(code); } else { - code.remove(this.left.end, this.end); + code.remove(findLastWhiteSpaceReverse(code.original, this.left.end, operatorPos), this.end); } this.getUsedBranch()!.render(code, options, { isCalleeOfRenderedParent, diff --git a/src/utils/renderHelpers.ts b/src/utils/renderHelpers.ts index 0b217537bed5..3deeb5bd618e 100644 --- a/src/utils/renderHelpers.ts +++ b/src/utils/renderHelpers.ts @@ -68,6 +68,22 @@ export function findNonWhiteSpace(code: string, index: number): number { return result.index; } +const WHITESPACE = /\s/; + +export function findLastWhiteSpaceReverse(code: string, start: number, end: number): number { + end--; + while (true) { + if (start + 1 >= end) { + return end; + } + if (WHITESPACE.test(code[end])) { + end--; + } else { + return end; + } + } +} + // This assumes "code" only contains white-space and comments // Returns position of line-comment if applicable export function findFirstLineBreakOutsideComment(code: string): [number, number] { diff --git a/test/form/samples/remove-whitespace-of-logical-expression/_config.js b/test/form/samples/remove-whitespace-of-logical-expression/_config.js new file mode 100644 index 000000000000..c895784e49b7 --- /dev/null +++ b/test/form/samples/remove-whitespace-of-logical-expression/_config.js @@ -0,0 +1,4 @@ +module.exports = defineTest({ + description: 'remove the white space of the removed part of LogicalExpression', + solo: true +}); diff --git a/test/form/samples/remove-whitespace-of-logical-expression/_expected.js b/test/form/samples/remove-whitespace-of-logical-expression/_expected.js new file mode 100644 index 000000000000..eff392be5017 --- /dev/null +++ b/test/form/samples/remove-whitespace-of-logical-expression/_expected.js @@ -0,0 +1,5 @@ +function App() { + set_class(`abc ${'red'}`); +} + +export { App as default }; diff --git a/test/form/samples/remove-whitespace-of-logical-expression/main.js b/test/form/samples/remove-whitespace-of-logical-expression/main.js new file mode 100644 index 000000000000..d473aa4b4568 --- /dev/null +++ b/test/form/samples/remove-whitespace-of-logical-expression/main.js @@ -0,0 +1,4 @@ +export default function App() { + let color; + set_class(`abc ${(color || 'red') ?? ''}`); +}