Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(es/parser): typeof on #private Fields #4302

Merged
merged 4 commits into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions crates/swc_ecma_ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,15 @@ pub use self::{
TsExprWithTypeArgs, TsExternalModuleRef, TsFnOrConstructorType, TsFnParam, TsFnType,
TsGetterSignature, TsImportEqualsDecl, TsImportType, TsIndexSignature, TsIndexedAccessType,
TsInferType, TsInstantiation, TsInterfaceBody, TsInterfaceDecl, TsIntersectionType,
TsKeywordType, TsKeywordTypeKind, TsLit, TsLitType, TsMappedType, TsMethodSignature,
TsModuleBlock, TsModuleDecl, TsModuleName, TsModuleRef, TsNamespaceBody, TsNamespaceDecl,
TsNamespaceExportDecl, TsNonNullExpr, TsOptionalType, TsParamProp, TsParamPropParam,
TsParenthesizedType, TsPropertySignature, TsQualifiedName, TsRestType, TsSetterSignature,
TsThisType, TsThisTypeOrIdent, TsTplLitType, TsTupleElement, TsTupleType, TsType,
TsTypeAliasDecl, TsTypeAnn, TsTypeAssertion, TsTypeElement, TsTypeLit, TsTypeOperator,
TsTypeOperatorOp, TsTypeParam, TsTypeParamDecl, TsTypeParamInstantiation, TsTypePredicate,
TsTypeQuery, TsTypeQueryExpr, TsTypeRef, TsUnionOrIntersectionType, TsUnionType,
TsKeywordType, TsKeywordTypeKind, TsLit, TsLitType, TsMappedType, TsMemberName,
TsMethodSignature, TsModuleBlock, TsModuleDecl, TsModuleName, TsModuleRef, TsNamespaceBody,
TsNamespaceDecl, TsNamespaceExportDecl, TsNonNullExpr, TsOptionalType, TsParamProp,
TsParamPropParam, TsParenthesizedType, TsPropertySignature, TsQualifiedName, TsRestType,
TsSetterSignature, TsThisType, TsThisTypeOrIdent, TsTplLitType, TsTupleElement,
TsTupleType, TsType, TsTypeAliasDecl, TsTypeAnn, TsTypeAssertion, TsTypeElement, TsTypeLit,
TsTypeOperator, TsTypeOperatorOp, TsTypeParam, TsTypeParamDecl, TsTypeParamInstantiation,
TsTypePredicate, TsTypeQuery, TsTypeQueryExpr, TsTypeRef, TsUnionOrIntersectionType,
TsUnionType,
},
};

Expand Down
16 changes: 14 additions & 2 deletions crates/swc_ecma_ast/src/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
lit::{Bool, Number, Str},
module::ModuleItem,
pat::{ArrayPat, AssignPat, ObjectPat, Pat, RestPat},
BigInt, BindingIdent, TplElement,
BigInt, BindingIdent, PrivateName, TplElement,
};

#[ast_node("TsTypeAnnotation")]
Expand Down Expand Up @@ -101,7 +101,7 @@ pub struct TsQualifiedName {
#[span(lo)]
pub left: TsEntityName,
#[span(hi)]
pub right: Ident,
pub right: TsMemberName,
}

#[ast_node]
Expand All @@ -116,6 +116,18 @@ pub enum TsEntityName {
Ident(Ident),
}

#[ast_node]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
#[allow(variant_size_differences)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub enum TsMemberName {
#[tag("Identifier")]
Ident(Ident),

#[tag("PrivateName")]
PrivateName(PrivateName),
}

// ================
// TypeScript type members (for type literal / interface / class)
// ================
Expand Down
10 changes: 10 additions & 0 deletions crates/swc_ecma_codegen/src/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ where
}
}

#[emitter]
fn emit_ts_member_name(&mut self, n: &TsMemberName) -> Result {
self.emit_leading_comments_of_span(n.span(), false)?;

match n {
TsMemberName::Ident(n) => emit!(n),
TsMemberName::PrivateName(n) => emit!(n),
}
}

#[emitter]
fn emit_ts_enum_decl(&mut self, n: &TsEnumDecl) -> Result {
self.emit_leading_comments_of_span(n.span(), false)?;
Expand Down
31 changes: 21 additions & 10 deletions crates/swc_ecma_parser/src/parser/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,11 @@ impl<I: Tokens> Parser<I> {
}

/// `tsParseEntityName`
fn parse_ts_entity_name(&mut self, allow_reserved_words: bool) -> PResult<TsEntityName> {
fn parse_ts_entity_name(
&mut self,
allow_reserved_words: bool,
allow_private_identifiers: bool,
) -> PResult<TsEntityName> {
debug_assert!(self.input.syntax().typescript());

let init = self.parse_ident_name()?;
Expand All @@ -196,7 +200,7 @@ impl<I: Tokens> Parser<I> {
let mut entity = TsEntityName::Ident(init);
while eat!(self, '.') {
let dot_start = cur_pos!(self);
if !is!(self, '#') && !is!(self, IdentName) {
if !allow_private_identifiers && !is!(self, '#') && !is!(self, IdentName) {
self.emit_err(
Span::new(dot_start, dot_start, Default::default()),
SyntaxError::TS1003,
Expand All @@ -205,10 +209,13 @@ impl<I: Tokens> Parser<I> {
}

let left = entity;
let right = if allow_reserved_words {
self.parse_ident_name()?
let right = if allow_private_identifiers {
self.parse_maybe_private_name()?
.either(Into::into, Into::into)
} else if allow_reserved_words {
self.parse_ident_name()?.into()
} else {
self.parse_ident(false, false)?
self.parse_ident(false, false)?.into()
};
entity = TsEntityName::TsQualifiedName(Box::new(TsQualifiedName { left, right }));
}
Expand All @@ -225,7 +232,9 @@ impl<I: Tokens> Parser<I> {

let has_modifier = self.eat_any_ts_modifier()?;

let type_name = self.parse_ts_entity_name(/* allow_reserved_words */ true)?;
let type_name = self.parse_ts_entity_name(
/* allow_reserved_words */ true, /* allow_private_identifiers */ false,
)?;
trace_cur!(self, parse_ts_type_ref__type_args);
let type_params = if !self.input.had_line_break_before_cur() && is!(self, '<') {
Some(self.parse_ts_type_args()?)
Expand Down Expand Up @@ -317,7 +326,7 @@ impl<I: Tokens> Parser<I> {
expect!(self, ')');

let qualifier = if eat!(self, '.') {
self.parse_ts_entity_name(false).map(Some)?
self.parse_ts_entity_name(false, false).map(Some)?
} else {
None
};
Expand Down Expand Up @@ -347,7 +356,7 @@ impl<I: Tokens> Parser<I> {
} else {
self.parse_ts_entity_name(
// allow_reserved_word
true,
true, true,
)
.map(From::from)?
};
Expand Down Expand Up @@ -1126,8 +1135,10 @@ impl<I: Tokens> Parser<I> {
if self.is_ts_external_module_ref()? {
self.parse_ts_external_module_ref().map(From::from)
} else {
self.parse_ts_entity_name(/* allow_reserved_words */ false)
.map(From::from)
self.parse_ts_entity_name(
/* allow_reserved_words */ false, /* allow_private_identifiers */ false,
)
.map(From::from)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class C {
#a = 'a';

constructor() {
const a: typeof this.#a = ''; // Ok
const b: typeof this.#a = 1; // Error
}
}
Loading