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): Add disallowAssertKeywords option #8913

Closed
Closed
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions crates/swc/tests/rust_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ fn shopify_2_same_opt() {
syntax: Some(Syntax::Typescript(TsConfig {
tsx: true,
decorators: false,
disallow_assert_keywords: false,
dts: false,
no_early_errors: false,
disallow_ambiguous_jsx_like: false,
Expand Down
1 change: 1 addition & 0 deletions crates/swc/tests/tsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ fn matrix(input: &Path) -> Vec<TestUnitData> {
syntax: Some(Syntax::Typescript(TsConfig {
tsx: is_jsx,
decorators,
disallow_assert_keywords: false,
dts: false,
no_early_errors: false,
disallow_ambiguous_jsx_like: false,
Expand Down
5 changes: 5 additions & 0 deletions crates/swc_ecma_parser/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ pub enum SyntaxError {

ReservedTypeAssertion,
ReservedArrowTypeParam,

InvalidAssertKeywords,
}

impl SyntaxError {
Expand Down Expand Up @@ -754,6 +756,9 @@ impl SyntaxError {
as in `<T,>() => ...`."
.into(),
SyntaxError::InvalidAssignTarget => "Invalid assignment target".into(),
SyntaxError::InvalidAssertKeywords => "The `assert` keyword is disallowed. Use the \
`with` keyword instead for import attributes"
.into(),
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions crates/swc_ecma_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,20 @@ impl Syntax {
}
}

pub fn disallow_assert_keywords(self) -> bool {
match self {
Syntax::Es(EsConfig {
disallow_assert_keywords,
..
}) => disallow_assert_keywords,
#[cfg(feature = "typescript")]
Syntax::Typescript(TsConfig {
disallow_assert_keywords,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should unconditionally follow the behavior of tsc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see. The issue I want to solve with this PR is that it prevents the assert keywords from being used in TypeScript, so I'll try to confirm if tsc can handle it.

..
}) => disallow_assert_keywords,
}
}

/// Should we parse jsx?
pub fn jsx(self) -> bool {
match self {
Expand Down Expand Up @@ -315,6 +329,9 @@ pub struct TsConfig {
#[serde(default)]
pub decorators: bool,

#[serde(default)]
pub disallow_assert_keywords: bool,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and this


/// `.d.ts`
#[serde(skip, default)]
pub dts: bool,
Expand Down Expand Up @@ -360,6 +377,9 @@ pub struct EsConfig {
#[serde(default, alias = "importAssertions")]
pub import_attributes: bool,

#[serde(default)]
pub disallow_assert_keywords: bool,

#[serde(default, rename = "allowSuperOutsideMethod")]
pub allow_super_outside_method: bool,

Expand Down
86 changes: 60 additions & 26 deletions crates/swc_ecma_parser/src/parser/stmt/module_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,25 @@ impl<I: Tokens> Parser<I> {
_ => unreachable!(),
};
let _ = cur!(self, false);
let with_start = cur_pos!(self);
let with = if self.input.syntax().import_attributes()
&& !self.input.had_line_break_before_cur()
&& (eat!(self, "assert") || eat!(self, "with"))
{
match *self.parse_object::<Box<Expr>>()? {
Expr::Object(v) => Some(Box::new(v)),
_ => unreachable!(),
if eat!(self, "assert") {
if self.input.syntax().disallow_assert_keywords() {
self.emit_err(span!(self, with_start), SyntaxError::InvalidAssertKeywords);
}
match *self.parse_object::<Box<Expr>>()? {
Expr::Object(v) => Some(Box::new(v)),
_ => unreachable!(),
}
} else if eat!(self, "with") {
match *self.parse_object::<Box<Expr>>()? {
Expr::Object(v) => Some(Box::new(v)),
_ => unreachable!(),
}
} else {
None
}
} else {
None
Expand Down Expand Up @@ -178,17 +190,28 @@ impl<I: Tokens> Parser<I> {
};

let _ = cur!(self, false);
let with = if self.input.syntax().import_attributes()
&& !self.input.had_line_break_before_cur()
&& (eat!(self, "assert") || eat!(self, "with"))
{
match *self.parse_object::<Box<Expr>>()? {
Expr::Object(v) => Some(Box::new(v)),
_ => unreachable!(),
}
} else {
None
};
let with_start = cur_pos!(self);
let with =
if self.input.syntax().import_attributes() && !self.input.had_line_break_before_cur() {
if eat!(self, "assert") {
if self.input.syntax().disallow_assert_keywords() {
self.emit_err(span!(self, with_start), SyntaxError::InvalidAssertKeywords);
}
match *self.parse_object::<Box<Expr>>()? {
Expr::Object(v) => Some(Box::new(v)),
_ => unreachable!(),
}
} else if eat!(self, "with") {
match *self.parse_object::<Box<Expr>>()? {
Expr::Object(v) => Some(Box::new(v)),
_ => unreachable!(),
}
} else {
None
}
} else {
None
};

expect!(self, ';');

Expand Down Expand Up @@ -847,17 +870,28 @@ impl<I: Tokens> Parser<I> {
_ => unexpected!(self, "a string literal"),
};
let _ = cur!(self, false);
let with = if self.input.syntax().import_attributes()
&& !self.input.had_line_break_before_cur()
&& (eat!(self, "assert") || eat!(self, "with"))
{
match *self.parse_object::<Box<Expr>>()? {
Expr::Object(v) => Some(Box::new(v)),
_ => unreachable!(),
}
} else {
None
};
let with_start = cur_pos!(self);
let with =
if self.input.syntax().import_attributes() && !self.input.had_line_break_before_cur() {
if eat!(self, "assert") {
if self.input.syntax().disallow_assert_keywords() {
self.emit_err(span!(self, with_start), SyntaxError::InvalidAssertKeywords);
}
match *self.parse_object::<Box<Expr>>()? {
Expr::Object(v) => Some(Box::new(v)),
_ => unreachable!(),
}
} else if eat!(self, "with") {
match *self.parse_object::<Box<Expr>>()? {
Expr::Object(v) => Some(Box::new(v)),
_ => unreachable!(),
}
} else {
None
}
} else {
None
};
expect!(self, ';');
Ok((src, with))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ fn identity(entry: PathBuf) {
Syntax::Typescript(TsConfig {
tsx: file_name.contains("tsx"),
decorators: true,
disallow_assert_keywords: false,
dts: false,
no_early_errors: false,
disallow_ambiguous_jsx_like: false,
Expand Down
5 changes: 5 additions & 0 deletions packages/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,10 @@ export interface TsParserConfig {
* @deprecated Always true because it's in ecmascript spec.
*/
dynamicImport?: boolean;
/**
* Defaults to `false`
*/
disallowAssertKeywords?: boolean;
}

export interface EsParserConfig {
Expand Down Expand Up @@ -763,6 +767,7 @@ export interface EsParserConfig {
* Defaults to `false`
*/
explicitResourceManagement?: boolean;
disallowAssertKeywords?: boolean;
}

/**
Expand Down
Loading