mirror of
https://github.com/aphrody-labs/oxc.git
synced 2026-10-17 12:56:09 +00:00
fix(codegen, packages/codegen): remove extraneous space before RestElement in ArrayPattern (#27613)
Fix both Rust and JS codegens. Both printed an extraneous space before `...` for a `RestElement` as first element of an `ArrayPattern`. ```javascript // Before const [ ...rest] = arr; // After const [...rest] = arr; ``` [Playground](https://playground.oxc.rs/?options=%7B%22run%22%3A%7B%22lint%22%3Afalse%2C%22formatter%22%3Afalse%2C%22transform%22%3Afalse%2C%22isolatedDeclarations%22%3Afalse%2C%22whitespace%22%3Afalse%2C%22mangle%22%3Afalse%2C%22mangleProps%22%3Afalse%2C%22compress%22%3Afalse%2C%22scope%22%3Atrue%2C%22symbol%22%3Atrue%2C%22cfg%22%3Afalse%7D%2C%22parser%22%3A%7B%22extension%22%3A%22js%22%2C%22allowReturnOutsideFunction%22%3Atrue%2C%22preserveParens%22%3Atrue%2C%22allowV8Intrinsics%22%3Atrue%2C%22semanticErrors%22%3Atrue%7D%2C%22linter%22%3A%7B%7D%2C%22formatter%22%3A%7B%22useTabs%22%3Afalse%2C%22tabWidth%22%3A2%2C%22endOfLine%22%3A%22lf%22%2C%22printWidth%22%3A80%2C%22singleQuote%22%3Afalse%2C%22jsxSingleQuote%22%3Afalse%2C%22quoteProps%22%3A%22as-needed%22%2C%22trailingComma%22%3A%22all%22%2C%22semi%22%3Atrue%2C%22arrowParens%22%3A%22always%22%2C%22bracketSpacing%22%3Atrue%2C%22bracketSameLine%22%3Afalse%2C%22objectWrap%22%3A%22preserve%22%2C%22singleAttributePerLine%22%3Afalse%7D%2C%22transformer%22%3A%7B%22target%22%3A%22es2015%22%2C%22useDefineForClassFields%22%3Atrue%2C%22experimentalDecorators%22%3Atrue%2C%22emitDecoratorMetadata%22%3Atrue%2C%22optimizeEnums%22%3Atrue%2C%22optimizeConstEnums%22%3Atrue%7D%2C%22isolatedDeclarations%22%3A%7B%22stripInternal%22%3Afalse%7D%2C%22codegen%22%3A%7B%22normal%22%3Atrue%2C%22jsdoc%22%3Atrue%2C%22annotation%22%3Atrue%2C%22legal%22%3Atrue%7D%2C%22compress%22%3A%7B%7D%2C%22mangle%22%3A%7B%22topLevel%22%3Atrue%2C%22keepNames%22%3Afalse%7D%2C%22mangleProps%22%3A%7B%22include%22%3A%22_%24%22%2C%22reserved%22%3A%5B%5D%2C%22quoted%22%3Afalse%2C%22debug%22%3Afalse%7D%2C%22controlFlow%22%3A%7B%22verbose%22%3Afalse%7D%2C%22inject%22%3A%7B%22inject%22%3A%7B%7D%7D%2C%22define%22%3A%7B%22define%22%3A%7B%7D%7D%7D&code=const+%5B...x%5D+%3D+y%3B)
This commit is contained in:
1 parent
27f5b45b71
commit
760b60ba14
4 files changed
+38
-3
No files matched your search
@@ -3205,7 +3205,9 @@ impl Gen for ArrayPattern<'_> {
|
||||
}
|
||||
}
|
||||
if let Some(rest) = &self.rest {
|
||||
p.print_soft_space();
|
||||
if !self.elements.is_empty() {
|
||||
p.print_soft_space();
|
||||
}
|
||||
rest.print(p, ctx);
|
||||
}
|
||||
p.print_ascii_byte(b']');
|
||||
|
||||
@@ -18,6 +18,13 @@ fn cases() {
|
||||
#[test]
|
||||
fn decl() {
|
||||
test_same("const [foo, ...bar] = qux;\n");
|
||||
test_same("const [...bar] = qux;\n");
|
||||
test_same("const [, ...bar] = qux;\n");
|
||||
test_same("const [, , , ...bar] = qux;\n");
|
||||
test_same("function foo([bar, ...qux]) {}\n");
|
||||
test_same("function foo([...bar]) {}\n");
|
||||
test_same("function foo([, ...bar]) {}\n");
|
||||
test_same("function foo([, , , ...bar]) {}\n");
|
||||
test_same("const { foo, ...bar } = qux;\n");
|
||||
test_minify("const [foo] = bar", "const[foo]=bar;");
|
||||
test_minify("const {foo} = bar", "const{foo}=bar;");
|
||||
@@ -475,6 +482,10 @@ fn r#yield() {
|
||||
|
||||
#[test]
|
||||
fn arrow() {
|
||||
test_same("([bar, ...qux]) => {};\n");
|
||||
test_same("([...bar]) => {};\n");
|
||||
test_same("([, ...bar]) => {};\n");
|
||||
test_same("([, , , ...bar]) => {};\n");
|
||||
test_minify("x => a, b", "x=>a,b;");
|
||||
test_minify("x => (a, b)", "x=>(a,b);");
|
||||
test_minify("x => (a => b)", "x=>(a=>b);");
|
||||
|
||||
@@ -171,7 +171,7 @@ export function printPropertyKey(key: ESTree.PropertyKey, state: State): void {
|
||||
*/
|
||||
function printArrayBindingPattern(node: ESTree.ArrayPattern, state: State): void {
|
||||
// Oxc stores the rest element separately from the other elements.
|
||||
// A trailing comma is printed before it, and a space always precedes it.
|
||||
// A comma and a space separate it from any elements before it.
|
||||
const { elements } = node;
|
||||
let { length } = elements;
|
||||
|
||||
@@ -196,7 +196,7 @@ function printArrayBindingPattern(node: ESTree.ArrayPattern, state: State): void
|
||||
}
|
||||
|
||||
if (rest !== null) {
|
||||
write(state, " ", CAT_OTHER);
|
||||
if (length > 0) write(state, " ", CAT_OTHER);
|
||||
writeWithMap(state, "...", CAT_OTHER, rest.start, rest.end, rest);
|
||||
printBindingPattern(rest.argument, state);
|
||||
if (TS) {
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
const [foo, ...a] = arr;
|
||||
const [...b] = arr;
|
||||
const [, ...c] = arr;
|
||||
const [, , , ...d] = arr;
|
||||
const [...[e]] = arr;
|
||||
const [...{ length }] = arr;
|
||||
for (const [...f] of arr);
|
||||
|
||||
function f1([bar, ...g]) {}
|
||||
function f2([...h]) {}
|
||||
function f3([, ...i]) {}
|
||||
function f4([, , , ...j]) {}
|
||||
|
||||
([bar, ...k]) => {};
|
||||
([...l]) => {};
|
||||
([, ...m]) => {};
|
||||
([, , , ...n]) => {};
|
||||
|
||||
[foo, ...o] = arr;
|
||||
[...p] = arr;
|
||||
[, ...q] = arr;
|
||||
[, , , ...r] = arr;
|
||||
Reference in new issue
Block a user