Skip to content

Commit 6ad177c

Browse files
committed
add some js decorator printing tests
1 parent 0e1696f commit 6ad177c

3 files changed

Lines changed: 185 additions & 0 deletions

File tree

internal/bundler_tests/bundler_default_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8448,3 +8448,87 @@ NOTE: You can mark the path "bar" as external to exclude it from the bundle, whi
84488448
`,
84498449
})
84508450
}
8451+
8452+
func TestDecoratorPrintingESM(t *testing.T) {
8453+
default_suite.expectBundled(t, bundled{
8454+
files: map[string]string{
8455+
"/entry.js": `
8456+
import { constant } from './constants'
8457+
import { imported } from 'somewhere'
8458+
import { undef } from './empty'
8459+
8460+
_ = class Outer {
8461+
#bar;
8462+
8463+
classes = [
8464+
class { @imported @imported() imported },
8465+
class { @unbound @unbound() unbound },
8466+
class { @constant @constant() constant },
8467+
class { @undef @undef() undef },
8468+
8469+
class { @(element[access]) indexed },
8470+
class { @foo.#bar private },
8471+
class { @foo.\u30FF unicode },
8472+
class { @(() => {}) arrow },
8473+
]
8474+
}
8475+
`,
8476+
"/constants.js": `
8477+
export const constant = 123
8478+
`,
8479+
"/empty.js": ``,
8480+
},
8481+
entryPaths: []string{"/entry.js"},
8482+
options: config.Options{
8483+
Mode: config.ModeBundle,
8484+
OutputFormat: config.FormatESModule,
8485+
AbsOutputFile: "/out.js",
8486+
ExternalPackages: true,
8487+
MinifySyntax: true,
8488+
},
8489+
expectedCompileLog: `entry.js: WARNING: Import "undef" will always be undefined because the file "empty.js" has no exports
8490+
`,
8491+
})
8492+
}
8493+
8494+
func TestDecoratorPrintingCJS(t *testing.T) {
8495+
default_suite.expectBundled(t, bundled{
8496+
files: map[string]string{
8497+
"/entry.js": `
8498+
import { constant } from './constants'
8499+
import { imported } from 'somewhere'
8500+
import { undef } from './empty'
8501+
8502+
_ = class Outer {
8503+
#bar;
8504+
8505+
classes = [
8506+
class { @imported @imported() imported },
8507+
class { @unbound @unbound() unbound },
8508+
class { @constant @constant() constant },
8509+
class { @undef @undef() undef },
8510+
8511+
class { @(element[access]) indexed },
8512+
class { @foo.#bar private },
8513+
class { @foo.\u30FF unicode },
8514+
class { @(() => {}) arrow },
8515+
]
8516+
}
8517+
`,
8518+
"/constants.js": `
8519+
export const constant = 123
8520+
`,
8521+
"/empty.js": ``,
8522+
},
8523+
entryPaths: []string{"/entry.js"},
8524+
options: config.Options{
8525+
Mode: config.ModeBundle,
8526+
OutputFormat: config.FormatCommonJS,
8527+
AbsOutputFile: "/out.js",
8528+
ExternalPackages: true,
8529+
MinifySyntax: true,
8530+
},
8531+
expectedCompileLog: `entry.js: WARNING: Import "undef" will always be undefined because the file "empty.js" has no exports
8532+
`,
8533+
})
8534+
}

internal/bundler_tests/snapshots/snapshots_default.txt

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,102 @@ for (const d in x)
920920
for (const e of x)
921921
console.log(e);
922922

923+
================================================================================
924+
TestDecoratorPrintingCJS
925+
---------- /out.js ----------
926+
// entry.js
927+
var import_somewhere = require("somewhere");
928+
_ = class {
929+
#bar;
930+
classes = [
931+
class {
932+
@(import_somewhere.imported)
933+
@(0, import_somewhere.imported)()
934+
imported;
935+
},
936+
class {
937+
@unbound
938+
@unbound()
939+
unbound;
940+
},
941+
class {
942+
@(123)
943+
@123()
944+
constant;
945+
},
946+
class {
947+
@(void 0)
948+
@(void 0)()
949+
undef;
950+
},
951+
class {
952+
@(element[access])
953+
indexed;
954+
},
955+
class {
956+
@foo.#bar
957+
private;
958+
},
959+
class {
960+
@(foo["ヿ"])
961+
unicode;
962+
},
963+
class {
964+
@(() => {
965+
})
966+
arrow;
967+
}
968+
];
969+
};
970+
971+
================================================================================
972+
TestDecoratorPrintingESM
973+
---------- /out.js ----------
974+
// entry.js
975+
import { imported } from "somewhere";
976+
_ = class {
977+
#bar;
978+
classes = [
979+
class {
980+
@(imported)
981+
@imported()
982+
imported;
983+
},
984+
class {
985+
@unbound
986+
@unbound()
987+
unbound;
988+
},
989+
class {
990+
@(123)
991+
@123()
992+
constant;
993+
},
994+
class {
995+
@(void 0)
996+
@(void 0)()
997+
undef;
998+
},
999+
class {
1000+
@(element[access])
1001+
indexed;
1002+
},
1003+
class {
1004+
@foo.#bar
1005+
private;
1006+
},
1007+
class {
1008+
@(foo["ヿ"])
1009+
unicode;
1010+
},
1011+
class {
1012+
@(() => {
1013+
})
1014+
arrow;
1015+
}
1016+
];
1017+
};
1018+
9231019
================================================================================
9241020
TestDefineAssignWarning
9251021
---------- /out/read.js ----------

internal/js_parser/js_parser_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2036,6 +2036,11 @@ func TestDecorators(t *testing.T) {
20362036
expectParseError(t, "@new Function() class Foo {}", "<stdin>: ERROR: Expected identifier but found \"new\"\n")
20372037
expectParseError(t, "@() => {} class Foo {}", "<stdin>: ERROR: Unexpected \")\"\n")
20382038

2039+
// See: https://github.com/microsoft/TypeScript/issues/55336
2040+
expectParseError(t, "@x().y() class Foo {}",
2041+
"<stdin>: ERROR: Expected \"class\" after decorator but found \".\"\n"+
2042+
"<stdin>: NOTE: The preceding decorator is here:\nNOTE: Decorators can only be used with class declarations.\n")
2043+
20392044
errorText := "<stdin>: ERROR: Transforming JavaScript decorators to the configured target environment is not supported yet\n"
20402045
expectParseErrorWithUnsupportedFeatures(t, compat.Decorators, "@dec class Foo {}", errorText)
20412046
expectParseErrorWithUnsupportedFeatures(t, compat.Decorators, "class Foo { @dec x }", errorText)

0 commit comments

Comments
 (0)