Skip to content

Commit

Permalink
add some js decorator printing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Sep 23, 2023
1 parent 0e1696f commit 6ad177c
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 0 deletions.
84 changes: 84 additions & 0 deletions internal/bundler_tests/bundler_default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8448,3 +8448,87 @@ NOTE: You can mark the path "bar" as external to exclude it from the bundle, whi
`,
})
}

func TestDecoratorPrintingESM(t *testing.T) {
default_suite.expectBundled(t, bundled{
files: map[string]string{
"/entry.js": `
import { constant } from './constants'
import { imported } from 'somewhere'
import { undef } from './empty'
_ = class Outer {
#bar;
classes = [
class { @imported @imported() imported },
class { @unbound @unbound() unbound },
class { @constant @constant() constant },
class { @undef @undef() undef },
class { @(element[access]) indexed },
class { @foo.#bar private },
class { @foo.\u30FF unicode },
class { @(() => {}) arrow },
]
}
`,
"/constants.js": `
export const constant = 123
`,
"/empty.js": ``,
},
entryPaths: []string{"/entry.js"},
options: config.Options{
Mode: config.ModeBundle,
OutputFormat: config.FormatESModule,
AbsOutputFile: "/out.js",
ExternalPackages: true,
MinifySyntax: true,
},
expectedCompileLog: `entry.js: WARNING: Import "undef" will always be undefined because the file "empty.js" has no exports
`,
})
}

func TestDecoratorPrintingCJS(t *testing.T) {
default_suite.expectBundled(t, bundled{
files: map[string]string{
"/entry.js": `
import { constant } from './constants'
import { imported } from 'somewhere'
import { undef } from './empty'
_ = class Outer {
#bar;
classes = [
class { @imported @imported() imported },
class { @unbound @unbound() unbound },
class { @constant @constant() constant },
class { @undef @undef() undef },
class { @(element[access]) indexed },
class { @foo.#bar private },
class { @foo.\u30FF unicode },
class { @(() => {}) arrow },
]
}
`,
"/constants.js": `
export const constant = 123
`,
"/empty.js": ``,
},
entryPaths: []string{"/entry.js"},
options: config.Options{
Mode: config.ModeBundle,
OutputFormat: config.FormatCommonJS,
AbsOutputFile: "/out.js",
ExternalPackages: true,
MinifySyntax: true,
},
expectedCompileLog: `entry.js: WARNING: Import "undef" will always be undefined because the file "empty.js" has no exports
`,
})
}
96 changes: 96 additions & 0 deletions internal/bundler_tests/snapshots/snapshots_default.txt
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,102 @@ for (const d in x)
for (const e of x)
console.log(e);

================================================================================
TestDecoratorPrintingCJS
---------- /out.js ----------
// entry.js
var import_somewhere = require("somewhere");
_ = class {
#bar;
classes = [
class {
@(import_somewhere.imported)
@(0, import_somewhere.imported)()
imported;
},
class {
@unbound
@unbound()
unbound;
},
class {
@(123)
@123()
constant;
},
class {
@(void 0)
@(void 0)()
undef;
},
class {
@(element[access])
indexed;
},
class {
@foo.#bar
private;
},
class {
@(foo["ヿ"])
unicode;
},
class {
@(() => {
})
arrow;
}
];
};

================================================================================
TestDecoratorPrintingESM
---------- /out.js ----------
// entry.js
import { imported } from "somewhere";
_ = class {
#bar;
classes = [
class {
@(imported)
@imported()
imported;
},
class {
@unbound
@unbound()
unbound;
},
class {
@(123)
@123()
constant;
},
class {
@(void 0)
@(void 0)()
undef;
},
class {
@(element[access])
indexed;
},
class {
@foo.#bar
private;
},
class {
@(foo["ヿ"])
unicode;
},
class {
@(() => {
})
arrow;
}
];
};

================================================================================
TestDefineAssignWarning
---------- /out/read.js ----------
Expand Down
5 changes: 5 additions & 0 deletions internal/js_parser/js_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2036,6 +2036,11 @@ func TestDecorators(t *testing.T) {
expectParseError(t, "@new Function() class Foo {}", "<stdin>: ERROR: Expected identifier but found \"new\"\n")
expectParseError(t, "@() => {} class Foo {}", "<stdin>: ERROR: Unexpected \")\"\n")

// See: https://github.com/microsoft/TypeScript/issues/55336
expectParseError(t, "@x().y() class Foo {}",
"<stdin>: ERROR: Expected \"class\" after decorator but found \".\"\n"+
"<stdin>: NOTE: The preceding decorator is here:\nNOTE: Decorators can only be used with class declarations.\n")

errorText := "<stdin>: ERROR: Transforming JavaScript decorators to the configured target environment is not supported yet\n"
expectParseErrorWithUnsupportedFeatures(t, compat.Decorators, "@dec class Foo {}", errorText)
expectParseErrorWithUnsupportedFeatures(t, compat.Decorators, "class Foo { @dec x }", errorText)
Expand Down

0 comments on commit 6ad177c

Please sign in to comment.