Skip to content

Commit 7383d0d

Browse files
committed
decorators: printing preserves newline-tail status
1 parent 7edc83d commit 7383d0d

10 files changed

Lines changed: 148 additions & 201 deletions

File tree

internal/bundler_tests/snapshots/snapshots_dce.txt

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -412,30 +412,24 @@ var fn = () => {
412412
var Class = @fn class {
413413
};
414414
var Field = class {
415-
@fn
416-
field;
415+
@fn field;
417416
};
418417
var Method = class {
419-
@fn
420-
method() {
418+
@fn method() {
421419
}
422420
};
423421
var Accessor = class {
424-
@fn
425-
accessor accessor;
422+
@fn accessor accessor;
426423
};
427424
var StaticField = class {
428-
@fn
429-
static field;
425+
@fn static field;
430426
};
431427
var StaticMethod = class {
432-
@fn
433-
static method() {
428+
@fn static method() {
434429
}
435430
};
436431
var StaticAccessor = class {
437-
@fn
438-
static accessor accessor;
432+
@fn static accessor accessor;
439433
};
440434

441435
================================================================================

internal/bundler_tests/snapshots/snapshots_default.txt

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -928,41 +928,29 @@ _ = class {
928928
#bar;
929929
classes = [
930930
class {
931-
@import_somewhere.imported
932-
@((0, import_somewhere.imported)())
933-
imported;
931+
@import_somewhere.imported @((0, import_somewhere.imported)()) imported;
934932
},
935933
class {
936-
@unbound
937-
@unbound()
938-
unbound;
934+
@unbound @unbound() unbound;
939935
},
940936
class {
941-
@(123)
942-
@(123())
943-
constant;
937+
@(123) @(123()) constant;
944938
},
945939
class {
946-
@(void 0)
947-
@((void 0)())
948-
undef;
940+
@(void 0) @((void 0)()) undef;
949941
},
950942
class {
951-
@(element[access])
952-
indexed;
943+
@(element[access]) indexed;
953944
},
954945
class {
955-
@foo.#bar
956-
private;
946+
@foo.#bar private;
957947
},
958948
class {
959-
@(foo["ヿ"])
960-
unicode;
949+
@(foo["ヿ"]) unicode;
961950
},
962951
class {
963952
@(() => {
964-
})
965-
arrow;
953+
}) arrow;
966954
}
967955
];
968956
};
@@ -976,41 +964,29 @@ _ = class {
976964
#bar;
977965
classes = [
978966
class {
979-
@imported
980-
@imported()
981-
imported;
967+
@imported @imported() imported;
982968
},
983969
class {
984-
@unbound
985-
@unbound()
986-
unbound;
970+
@unbound @unbound() unbound;
987971
},
988972
class {
989-
@(123)
990-
@(123())
991-
constant;
973+
@(123) @(123()) constant;
992974
},
993975
class {
994-
@(void 0)
995-
@((void 0)())
996-
undef;
976+
@(void 0) @((void 0)()) undef;
997977
},
998978
class {
999-
@(element[access])
1000-
indexed;
979+
@(element[access]) indexed;
1001980
},
1002981
class {
1003-
@foo.#bar
1004-
private;
982+
@foo.#bar private;
1005983
},
1006984
class {
1007-
@(foo["ヿ"])
1008-
unicode;
985+
@(foo["ヿ"]) unicode;
1009986
},
1010987
class {
1011988
@(() => {
1012-
})
1013-
arrow;
989+
}) arrow;
1014990
}
1015991
];
1016992
};

internal/bundler_tests/snapshots/snapshots_lower.txt

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -624,26 +624,14 @@ TestJavaScriptDecoratorsESNext
624624
@x.y()
625625
@(new y.x())
626626
export default class Foo {
627-
@x
628-
@y
629-
mUndef;
630-
@x
631-
@y
632-
mDef = 1;
633-
@x
634-
@y
635-
method() {
627+
@x @y mUndef;
628+
@x @y mDef = 1;
629+
@x @y method() {
636630
return new Foo();
637631
}
638-
@x
639-
@y
640-
static sUndef;
641-
@x
642-
@y
643-
static sDef = new Foo();
644-
@x
645-
@y
646-
static sMethod() {
632+
@x @y static sUndef;
633+
@x @y static sDef = new Foo();
634+
@x @y static sMethod() {
647635
return new Foo();
648636
}
649637
}

internal/bundler_tests/snapshots/snapshots_ts.txt

Lines changed: 20 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,74 +1178,34 @@ TestTSExperimentalDecoratorsNoConfig
11781178
---------- /out.js ----------
11791179
// entry.ts
11801180
var Foo = @x.y() @(new y.x()) class _Foo {
1181-
@x
1182-
@y
1183-
mUndef;
1184-
@x
1185-
@y
1186-
mDef = 1;
1187-
@x
1188-
@y
1189-
method() {
1181+
@x @y mUndef;
1182+
@x @y mDef = 1;
1183+
@x @y method() {
11901184
return new _Foo();
11911185
}
1192-
@x
1193-
@y
1194-
accessor aUndef;
1195-
@x
1196-
@y
1197-
accessor aDef = 1;
1198-
@x
1199-
@y
1200-
static sUndef;
1201-
@x
1202-
@y
1203-
static sDef = new _Foo();
1204-
@x
1205-
@y
1206-
static sMethod() {
1186+
@x @y accessor aUndef;
1187+
@x @y accessor aDef = 1;
1188+
@x @y static sUndef;
1189+
@x @y static sDef = new _Foo();
1190+
@x @y static sMethod() {
12071191
return new _Foo();
12081192
}
1209-
@x
1210-
@y
1211-
static accessor asUndef;
1212-
@x
1213-
@y
1214-
static accessor asDef = 1;
1215-
@x
1216-
@y
1217-
#mUndef;
1218-
@x
1219-
@y
1220-
#mDef = 1;
1221-
@x
1222-
@y
1223-
#method() {
1193+
@x @y static accessor asUndef;
1194+
@x @y static accessor asDef = 1;
1195+
@x @y #mUndef;
1196+
@x @y #mDef = 1;
1197+
@x @y #method() {
12241198
return new _Foo();
12251199
}
1226-
@x
1227-
@y
1228-
accessor #aUndef;
1229-
@x
1230-
@y
1231-
accessor #aDef = 1;
1232-
@x
1233-
@y
1234-
static #sUndef;
1235-
@x
1236-
@y
1237-
static #sDef = 1;
1238-
@x
1239-
@y
1240-
static #sMethod() {
1200+
@x @y accessor #aUndef;
1201+
@x @y accessor #aDef = 1;
1202+
@x @y static #sUndef;
1203+
@x @y static #sDef = 1;
1204+
@x @y static #sMethod() {
12411205
return new _Foo();
12421206
}
1243-
@x
1244-
@y
1245-
static accessor #asUndef;
1246-
@x
1247-
@y
1248-
static accessor #asDef = 1;
1207+
@x @y static accessor #asUndef;
1208+
@x @y static accessor #asDef = 1;
12491209
};
12501210
export {
12511211
Foo as default

internal/js_ast/js_ast.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,9 @@ var OpTable = []OpTableEntry{
242242
}
243243

244244
type Decorator struct {
245-
Value Expr
246-
AtLoc logger.Loc
245+
Value Expr
246+
AtLoc logger.Loc
247+
OmitNewlineAfter bool
247248
}
248249

249250
type PropertyKind uint8

internal/js_parser/js_parser.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6620,7 +6620,11 @@ func (p *parser) parseDecorators(decoratorScope *js_ast.Scope, classKeyword logg
66206620
// special parser that doesn't allow normal expressions (e.g. "?.").
66216621
value = p.parseDecorator()
66226622
}
6623-
decorators = append(decorators, js_ast.Decorator{Value: value, AtLoc: atLoc})
6623+
decorators = append(decorators, js_ast.Decorator{
6624+
Value: value,
6625+
AtLoc: atLoc,
6626+
OmitNewlineAfter: !p.lexer.HasNewlineBefore,
6627+
})
66246628
}
66256629

66266630
// Avoid "popScope" because this decorator scope is not hierarchical

internal/js_parser/js_parser_test.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2000,31 +2000,31 @@ func TestAutoAccessors(t *testing.T) {
20002000
}
20012001

20022002
func TestDecorators(t *testing.T) {
2003-
expectPrinted(t, "@x @y class Foo {}", "@x\n@y\nclass Foo {\n}\n")
2004-
expectPrinted(t, "@x @y export class Foo {}", "@x\n@y\nexport class Foo {\n}\n")
2005-
expectPrinted(t, "@x @y export default class Foo {}", "@x\n@y\nexport default class Foo {\n}\n")
2003+
expectPrinted(t, "@x @y class Foo {}", "@x @y class Foo {\n}\n")
2004+
expectPrinted(t, "@x @y export class Foo {}", "@x @y export class Foo {\n}\n")
2005+
expectPrinted(t, "@x @y export default class Foo {}", "@x @y export default class Foo {\n}\n")
20062006
expectPrinted(t, "_ = @x @y class {}", "_ = @x @y class {\n};\n")
20072007

2008-
expectPrinted(t, "class Foo { @x y }", "class Foo {\n @x\n y;\n}\n")
2009-
expectPrinted(t, "class Foo { @x y() {} }", "class Foo {\n @x\n y() {\n }\n}\n")
2010-
expectPrinted(t, "class Foo { @x static y }", "class Foo {\n @x\n static y;\n}\n")
2011-
expectPrinted(t, "class Foo { @x static y() {} }", "class Foo {\n @x\n static y() {\n }\n}\n")
2012-
expectPrinted(t, "class Foo { @x accessor y }", "class Foo {\n @x\n accessor y;\n}\n")
2008+
expectPrinted(t, "class Foo { @x y }", "class Foo {\n @x y;\n}\n")
2009+
expectPrinted(t, "class Foo { @x y() {} }", "class Foo {\n @x y() {\n }\n}\n")
2010+
expectPrinted(t, "class Foo { @x static y }", "class Foo {\n @x static y;\n}\n")
2011+
expectPrinted(t, "class Foo { @x static y() {} }", "class Foo {\n @x static y() {\n }\n}\n")
2012+
expectPrinted(t, "class Foo { @x accessor y }", "class Foo {\n @x accessor y;\n}\n")
20132013

2014-
expectPrinted(t, "class Foo { @x #y }", "class Foo {\n @x\n #y;\n}\n")
2015-
expectPrinted(t, "class Foo { @x #y() {} }", "class Foo {\n @x\n #y() {\n }\n}\n")
2016-
expectPrinted(t, "class Foo { @x static #y }", "class Foo {\n @x\n static #y;\n}\n")
2017-
expectPrinted(t, "class Foo { @x static #y() {} }", "class Foo {\n @x\n static #y() {\n }\n}\n")
2018-
expectPrinted(t, "class Foo { @x accessor #y }", "class Foo {\n @x\n accessor #y;\n}\n")
2014+
expectPrinted(t, "class Foo { @x #y }", "class Foo {\n @x #y;\n}\n")
2015+
expectPrinted(t, "class Foo { @x #y() {} }", "class Foo {\n @x #y() {\n }\n}\n")
2016+
expectPrinted(t, "class Foo { @x static #y }", "class Foo {\n @x static #y;\n}\n")
2017+
expectPrinted(t, "class Foo { @x static #y() {} }", "class Foo {\n @x static #y() {\n }\n}\n")
2018+
expectPrinted(t, "class Foo { @x accessor #y }", "class Foo {\n @x accessor #y;\n}\n")
20192019

20202020
expectParseError(t, "class Foo { x(@y z) {} }", "<stdin>: ERROR: Parameter decorators are not allowed in JavaScript\n")
20212021
expectParseError(t, "class Foo { @x static {} }", "<stdin>: ERROR: Expected \";\" but found \"{\"\n")
20222022

20232023
expectPrinted(t, "@\na\n(\n)\n@\n(\nb\n)\nclass\nFoo\n{\n}\n", "@a()\n@b\nclass Foo {\n}\n")
2024-
expectPrinted(t, "@(a, b) class Foo {}\n", "@(a, b)\nclass Foo {\n}\n")
2025-
expectPrinted(t, "@x() class Foo {}", "@x()\nclass Foo {\n}\n")
2026-
expectPrinted(t, "@x.y() class Foo {}", "@x.y()\nclass Foo {\n}\n")
2027-
expectPrinted(t, "@(() => {}) class Foo {}", "@(() => {\n})\nclass Foo {\n}\n")
2024+
expectPrinted(t, "@(a, b) class Foo {}", "@(a, b) class Foo {\n}\n")
2025+
expectPrinted(t, "@x() class Foo {}", "@x() class Foo {\n}\n")
2026+
expectPrinted(t, "@x.y() class Foo {}", "@x.y() class Foo {\n}\n")
2027+
expectPrinted(t, "@(() => {}) class Foo {}", "@(() => {\n}) class Foo {\n}\n")
20282028
expectPrinted(t, "class Foo { #x = @y.#x.y.#x class {} }", "class Foo {\n #x = @y.#x.y.#x class {\n };\n}\n")
20292029
expectParseError(t, "@123 class Foo {}", "<stdin>: ERROR: Expected identifier but found \"123\"\n")
20302030
expectParseError(t, "@x[y] class Foo {}", "<stdin>: ERROR: Expected \";\" but found \"class\"\n")
@@ -2052,12 +2052,12 @@ func TestDecorators(t *testing.T) {
20522052
expectParseError(t, "@x abstract\nclass Foo {}", "<stdin>: ERROR: Decorators are not valid here\n")
20532053

20542054
// Check decorator locations in relation to the "export" keyword
2055-
expectPrinted(t, "@x export class Foo {}", "@x\nexport class Foo {\n}\n")
2056-
expectPrinted(t, "export @x class Foo {}", "@x\nexport class Foo {\n}\n")
2057-
expectPrinted(t, "@x export default class {}", "@x\nexport default class {\n}\n")
2058-
expectPrinted(t, "export default @x class {}", "@x\nexport default class {\n}\n")
2059-
expectPrinted(t, "@x export default class Foo {}", "@x\nexport default class Foo {\n}\n")
2060-
expectPrinted(t, "export default @x class Foo {}", "@x\nexport default class Foo {\n}\n")
2055+
expectPrinted(t, "@x export class Foo {}", "@x export class Foo {\n}\n")
2056+
expectPrinted(t, "export @x class Foo {}", "@x export class Foo {\n}\n")
2057+
expectPrinted(t, "@x export default class {}", "@x export default class {\n}\n")
2058+
expectPrinted(t, "export default @x class {}", "@x export default class {\n}\n")
2059+
expectPrinted(t, "@x export default class Foo {}", "@x export default class Foo {\n}\n")
2060+
expectPrinted(t, "export default @x class Foo {}", "@x export default class Foo {\n}\n")
20612061
expectPrinted(t, "export default (@x class {})", "export default (@x class {\n});\n")
20622062
expectPrinted(t, "export default (@x class Foo {})", "export default (@x class Foo {\n});\n")
20632063
expectParseError(t, "export @x default class {}", "<stdin>: ERROR: Unexpected \"default\"\n")

0 commit comments

Comments
 (0)