@@ -2181,10 +2181,11 @@ func (p *parser) parseProperty(startLoc logger.Loc, kind js_ast.PropertyKind, op
21812181 scopeIndex := len(p.scopesInOrder)
21822182
21832183 if prop, ok := p.parseProperty(startLoc, kind, opts, nil); ok &&
2184- prop.Kind == js_ast.PropertyNormal && prop.ValueOrNil.Data == nil && len(opts.decorators) > 0 {
2184+ prop.Kind == js_ast.PropertyNormal && prop.ValueOrNil.Data == nil &&
2185+ (p.options.ts.Config.ExperimentalDecorators == config.True && len(opts.decorators) > 0) {
21852186 // If this is a well-formed class field with the "declare" keyword,
21862187 // only keep the declaration to preserve its side-effects when
2187- // there are TypeScript decorators present:
2188+ // there are TypeScript experimental decorators present:
21882189 //
21892190 // class Foo {
21902191 // // Remove this
@@ -2194,6 +2195,15 @@ func (p *parser) parseProperty(startLoc logger.Loc, kind js_ast.PropertyKind, op
21942195 // @decorator(console.log('side effect 2')) declare bar
21952196 // }
21962197 //
2198+ // This behavior is surprisingly somehow valid with TypeScript
2199+ // experimental decorators, which was possibly by accident.
2200+ // TypeScript does not allow this with JavaScript decorators.
2201+ //
2202+ // References:
2203+ //
2204+ // https://github.com/evanw/esbuild/issues/1675
2205+ // https://github.com/microsoft/TypeScript/issues/46345
2206+ //
21972207 prop.Kind = js_ast.PropertyDeclare
21982208 return prop, true
21992209 }
@@ -6241,6 +6251,7 @@ func (p *parser) parseClass(classKeyword logger.Range, name *ast.LocRef, classOp
62416251
62426252 // Parse decorators for this property
62436253 firstDecoratorLoc := p.lexer.Loc()
6254+ scopeIndex := len(p.scopesInOrder)
62446255 opts.decorators = p.parseDecorators(p.currentScope, classKeyword, opts.decoratorContext)
62456256
62466257 // This property may turn out to be a type in TypeScript, which should be ignored
@@ -6261,6 +6272,9 @@ func (p *parser) parseClass(classKeyword logger.Range, name *ast.LocRef, classOp
62616272 hasConstructor = true
62626273 }
62636274 }
6275+ } else if !classOpts.isTypeScriptDeclare && len(opts.decorators) > 0 {
6276+ p.log.AddError(&p.tracker, logger.Range{Loc: firstDecoratorLoc, Len: 1}, "Decorators are not valid here")
6277+ p.discardScopesUpTo(scopeIndex)
62646278 }
62656279 }
62666280
0 commit comments