Skip to content

Commit

Permalink
require "experimentalDecorators": true (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed May 16, 2023
1 parent ead6b72 commit 6f2c029
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 111 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

This release makes the following changes to esbuild's `tsconfig.json` support:

* Using experimental decorators now requires `"experimentalDecorators": true` ([#104](https://github.com/evanw/esbuild/issues/104))

Previously esbuild would always compile decorators in TypeScript code using TypeScript's experimental decorator transform. Now that standard JavaScript decorators are close to being finalized, esbuild will now require you to use `"experimentalDecorators": true` to do this. This new requirement makes it possible for esbuild to introduce a transform for standard JavaScript decorators in TypeScript code in the future. Such a transform has not been implemented yet, however.

* TypeScript's `target` no longer affects esbuild's `target` ([#2628](https://github.com/evanw/esbuild/issues/2628))

Some people requested that esbuild support TypeScript's `target` setting, so support for it was added (in [version 0.12.4](https://github.com/evanw/esbuild/releases/v0.12.4)). However, esbuild supports reading from multiple `tsconfig.json` files within a single build, which opens up the possibility that different files in the build have different language targets configured. There isn't really any reason to do this and it can lead to unexpected results. So with this release, the `target` setting in `tsconfig.json` will no longer affect esbuild's own `target` setting. You will have to use esbuild's own target setting instead (which is a single, global value).
Expand Down
66 changes: 53 additions & 13 deletions internal/bundler_tests/bundler_ts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,30 @@ func TestTSMinifiedBundleCommonJS(t *testing.T) {
})
}

func TestTSExperimentalDecoratorsNoConfig(t *testing.T) {
ts_suite.expectBundled(t, bundled{
files: map[string]string{
"/entry.ts": `
@decorator
class Foo {}
`,
"/tsconfig.json": `{
"compilerOptions": {
"experimentalDecorators": false
}
}`,
},
entryPaths: []string{"/entry.ts"},
options: config.Options{
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
},
expectedScanLog: "entry.ts: ERROR: Experimental decorators are not currently enabled\n" +
"NOTE: To use experimental decorators in TypeScript with esbuild, you need to enable them by adding \"experimentalDecorators\": true in your \"tsconfig.json\" file. " +
"TypeScript's experimental decorators are currently the only kind of decorators that esbuild supports.\n",
})
}

func TestTSExperimentalDecorators(t *testing.T) {
ts_suite.expectBundled(t, bundled{
files: map[string]string{
Expand Down Expand Up @@ -942,7 +966,8 @@ func TestTSExperimentalDecorators(t *testing.T) {
`,
"/tsconfig.json": `{
"compilerOptions": {
"useDefineForClassFields": false
"useDefineForClassFields": false,
"experimentalDecorators": true
}
}`,
},
Expand All @@ -961,6 +986,11 @@ func TestTSExperimentalDecoratorsKeepNames(t *testing.T) {
@decoratorMustComeAfterName
class Foo {}
`,
"/tsconfig.json": `{
"compilerOptions": {
"experimentalDecorators": true
}
}`,
},
entryPaths: []string{"/entry.ts"},
options: config.Options{
Expand Down Expand Up @@ -1001,7 +1031,8 @@ func TestTSExperimentalDecoratorScopeIssue2147(t *testing.T) {
`,
"/tsconfig.json": `{
"compilerOptions": {
"useDefineForClassFields": false
"useDefineForClassFields": false,
"experimentalDecorators": true
}
}`,
},
Expand Down Expand Up @@ -1439,14 +1470,17 @@ func TestTSComputedClassFieldUseDefineFalse(t *testing.T) {
}
new Foo()
`,
"/tsconfig.json": `{
"compilerOptions": {
"useDefineForClassFields": false,
"experimentalDecorators": true
}
}`,
},
entryPaths: []string{"/entry.ts"},
options: config.Options{
Mode: config.ModePassThrough,
AbsOutputFile: "/out.js",
TS: config.TSOptions{Config: config.TSConfig{
UseDefineForClassFields: config.False,
}},
},
})
}
Expand All @@ -1465,14 +1499,17 @@ func TestTSComputedClassFieldUseDefineTrue(t *testing.T) {
}
new Foo()
`,
"/tsconfig.json": `{
"compilerOptions": {
"useDefineForClassFields": true,
"experimentalDecorators": true
}
}`,
},
entryPaths: []string{"/entry.ts"},
options: config.Options{
Mode: config.ModePassThrough,
AbsOutputFile: "/out.js",
TS: config.TSOptions{Config: config.TSConfig{
UseDefineForClassFields: config.True,
}},
},
})
}
Expand All @@ -1491,14 +1528,17 @@ func TestTSComputedClassFieldUseDefineTrueLower(t *testing.T) {
}
new Foo()
`,
"/tsconfig.json": `{
"compilerOptions": {
"useDefineForClassFields": true,
"experimentalDecorators": true
}
}`,
},
entryPaths: []string{"/entry.ts"},
options: config.Options{
Mode: config.ModePassThrough,
AbsOutputFile: "/out.js",
TS: config.TSOptions{Config: config.TSConfig{
UseDefineForClassFields: config.True,
}},
Mode: config.ModePassThrough,
AbsOutputFile: "/out.js",
UnsupportedJSFeatures: compat.ClassField,
},
})
Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ type TSConfigJSX struct {
// Note: This can currently only contain primitive values. It's compared
// for equality using a structural equality comparison by the JS parser.
type TSConfig struct {
ExperimentalDecorators MaybeBool
ImportsNotUsedAsValues TSImportsNotUsedAsValues
PreserveValueImports MaybeBool
Target TSTarget
Expand Down
5 changes: 5 additions & 0 deletions internal/js_parser/js_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -6259,6 +6259,11 @@ func (p *parser) parseDecorators(decoratorScope *js_ast.Scope, classKeyword logg
[]logger.MsgData{p.tracker.MsgData(classKeyword, "This is a class expression, not a class declaration:")})
} else if (context & decoratorBeforeClassExpr) != 0 {
p.log.AddError(&p.tracker, p.lexer.Range(), "Experimental decorators cannot be used in expression position in TypeScript")
} else if p.options.ts.Config.ExperimentalDecorators != config.True {
p.log.AddErrorWithNotes(&p.tracker, p.lexer.Range(), "Experimental decorators are not currently enabled", []logger.MsgData{{
Text: "To use experimental decorators in TypeScript with esbuild, you need to enable them by adding \"experimentalDecorators\": true in your \"tsconfig.json\" file. " +
"TypeScript's experimental decorators are currently the only kind of decorators that esbuild supports.",
}})
}
} else {
if (context & decoratorInFnArgs) != 0 {
Expand Down
Loading

0 comments on commit 6f2c029

Please sign in to comment.