chore(es/typescript): Improve decorator handling of fast strip (#9178)

**Description:**

Given that decorators will be a part of JavaScript, as specified in the [proposal](https://github.com/tc39/proposal-decorators), we retain them in the output rather than reporting an error.
This commit is contained in:
magic-akari 2024-07-08 20:57:40 +08:00 committed by GitHub
parent 67915bfe56
commit 962170fb70
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 12 additions and 19 deletions

View File

@ -9,13 +9,12 @@ use swc_common::{
BytePos, FileName, SourceMap, Span, Spanned,
};
use swc_ecma_ast::{
ArrowExpr, BindingIdent, Class, ClassDecl, ClassMethod, ClassProp, Decorator, EsVersion,
ExportAll, ExportDecl, ExportSpecifier, FnDecl, Ident, ImportDecl, ImportSpecifier,
NamedExport, Param, Pat, Program, TsAsExpr, TsConstAssertion, TsEnumDecl, TsExportAssignment,
TsImportEqualsDecl, TsIndexSignature, TsInstantiation, TsInterfaceDecl, TsModuleDecl,
TsModuleName, TsNamespaceDecl, TsNonNullExpr, TsParamPropParam, TsSatisfiesExpr,
TsTypeAliasDecl, TsTypeAnn, TsTypeAssertion, TsTypeParamDecl, TsTypeParamInstantiation,
VarDecl,
ArrowExpr, BindingIdent, Class, ClassDecl, ClassMethod, ClassProp, EsVersion, ExportAll,
ExportDecl, ExportSpecifier, FnDecl, Ident, ImportDecl, ImportSpecifier, NamedExport, Param,
Pat, Program, TsAsExpr, TsConstAssertion, TsEnumDecl, TsExportAssignment, TsImportEqualsDecl,
TsIndexSignature, TsInstantiation, TsInterfaceDecl, TsModuleDecl, TsModuleName,
TsNamespaceDecl, TsNonNullExpr, TsParamPropParam, TsSatisfiesExpr, TsTypeAliasDecl, TsTypeAnn,
TsTypeAssertion, TsTypeParamDecl, TsTypeParamInstantiation, VarDecl,
};
use swc_ecma_parser::{
lexer::Lexer,
@ -364,12 +363,6 @@ impl Visit for TsStrip {
self.add_replacement(n.span);
}
fn visit_decorator(&mut self, n: &Decorator) {
HANDLER.with(|handler| {
handler.span_err(n.span, "Decorators are not supported");
});
}
fn visit_export_all(&mut self, n: &ExportAll) {
if n.type_only {
self.add_replacement(n.span);
@ -419,6 +412,8 @@ impl Visit for TsStrip {
let comma = self.get_next_token(import.span_hi());
if comma.token == Token::Comma {
span = span.with_hi(comma.span.hi);
} else {
debug_assert_eq!(comma.token, Token::RBrace);
}
self.add_replacement(span);
}
@ -439,6 +434,8 @@ impl Visit for TsStrip {
let comma = self.get_next_token(e.span_hi());
if comma.token == Token::Comma {
span = span.with_hi(comma.span.hi);
} else {
debug_assert_eq!(comma.token, Token::RBrace);
}
self.add_replacement(span);
}
@ -484,6 +481,8 @@ impl Visit for TsStrip {
let comma = self.get_next_token(span.hi);
if comma.token == Token::Comma {
span = span.with_hi(comma.span.hi);
} else {
debug_assert_eq!(comma.token, Token::RParen);
}
self.add_replacement(span);

View File

@ -1,5 +0,0 @@
x Decorators are not supported
,----
1 | class Foo { @decorator foo() { } }
: ^^^^^^^^^^
`----

View File

@ -1 +0,0 @@
class Foo { @decorator foo() { } }