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

Check reserved identifiers in function params #129

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions crates/stc_ts_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,11 @@ pub enum Error {
span: Span,
},

/// TS1359
ExpectedIdentifierReservedKeyword {
span: Span,
},

TS1166 {
span: Span,
},
Expand Down Expand Up @@ -1517,6 +1522,7 @@ impl Error {
Error::TS2370 { .. } => 2370,
Error::WrongOverloadSignature { .. } => 2394,
Error::TS1166 { .. } => 1166,
Error::ExpectedIdentifierReservedKeyword { .. } => 1359,
Error::TS1345 { .. } => 1345,
Error::TS2353 { .. } => 2353,
Error::ConstructorImplMissingOrNotFollowedByDecl { .. } => 2390,
Expand Down
23 changes: 22 additions & 1 deletion crates/stc_ts_file_analyzer/src/analyzer/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use std::borrow::Cow;
use itertools::{EitherOrBoth, Itertools};
use rnode::{Fold, FoldWith};
use stc_ts_ast_rnode::{
RBindingIdent, RFnDecl, RFnExpr, RFunction, RIdent, RParamOrTsParamProp, RPat, RTsEntityName,
RBindingIdent, RFnDecl, RFnExpr, RFunction, RIdent, RParam, RParamOrTsParamProp, RPat,
RTsEntityName,
};
use stc_ts_errors::{Error, Errors};
use stc_ts_type_ops::Fix;
Expand Down Expand Up @@ -62,6 +63,7 @@ impl Analyzer<'_, '_> {
// TODO(kdy1): Move this to parser
let mut has_optional = false;
for p in &f.params {
validate_parameter_name(child, &p, f.is_async, f.is_generator);
if has_optional {
match p.pat {
RPat::Ident(RBindingIdent {
Expand Down Expand Up @@ -478,6 +480,25 @@ impl Analyzer<'_, '_> {
}
}

// Check for reserved words used as identifiers (yield, await, etc.)
//
// TODO(@littledivy): Generalize this to work regardless of 'function' context.
fn validate_parameter_name(ctx: &mut Analyzer, p: &RParam, is_async: bool, is_generator: bool) {
Comment on lines +485 to +486
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This should be made context-aware so it can be used with in top level and not just function params. Maybe in the parser? I saw @kdy1 that you have a similar TODO a few lines above.

Similar to https://github.com/microsoft/TypeScript/blob/3d2b4017eb6b9a2b94bc673291e56ae95e8beddd/src/compiler/binder.ts#L2178

Copy link
Member

Choose a reason for hiding this comment

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

I think it's better to make a method of Analyzer for consistency.
I may refactor it to split the analyzer, but let's use a method at now

match &p.pat {
RPat::Ident(ident) => {
if is_async && ident.id.sym == *"await" {
ctx.storage
.report(Error::ExpectedIdentifierReservedKeyword { span: p.span() });
}
if is_generator && ident.id.sym == *"yield" {
ctx.storage
.report(Error::ExpectedIdentifierReservedKeyword { span: p.span() });
}
}
_ => {}
}
}

#[validator]
impl Analyzer<'_, '_> {
/// NOTE: This method **should not call f.fold_children_with(self)**
Expand Down
1 change: 1 addition & 0 deletions crates/stc_ts_type_checker/tests/conformance.pass.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ async/es2017/functionDeclarations/asyncFunctionDeclaration1_es2017.ts
async/es2017/functionDeclarations/asyncFunctionDeclaration2_es2017.ts
async/es2017/functionDeclarations/asyncFunctionDeclaration3_es2017.ts
async/es2017/functionDeclarations/asyncFunctionDeclaration4_es2017.ts
async/es2017/functionDeclarations/asyncFunctionDeclaration5_es2017.ts
async/es2017/functionDeclarations/asyncFunctionDeclaration8_es2017.ts
async/es5/asyncArrowFunction/arrowFunctionWithParameterNameAsync_es5.ts
async/es5/asyncArrowFunction/asyncArrowFunction10_es5.ts
Expand Down
5 changes: 5 additions & 0 deletions crates/stc_ts_type_checker/tests/tsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ fn create_test(path: PathBuf) -> Option<Box<dyn FnOnce() + Send + Sync>> {

if let Ok(errors) = load_expected_errors(&path) {
for err in errors {
// Don't ignore certain TS1 tests
if err.code == "TS1359" {
continue;
}

if err.code.starts_with("TS1") && err.code.len() == 6 {
return None;
}
Expand Down