-
-
Couldn't load subscription status.
- Fork 1.3k
fix(es/parser): Support TypeScript non-null assertion in destructuring assignment #11168
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
base: main
Are you sure you want to change the base?
Conversation
…g assignment This fixes the parser to correctly handle TypeScript non-null assertion operators (!) in destructuring assignment patterns, specifically in array element access expressions. Previously, code like: ```typescript [arr[i]!, arr[j]!] = [arr[j]!, arr[i]!] ``` Would fail with "Not a pattern" error (SyntaxError::InvalidPat), while the TypeScript compiler successfully parses this code. The fix adds support for `Expr::TsNonNull`, `Expr::TsTypeAssertion`, `Expr::TsInstantiation`, and `Expr::TsSatisfies` to the list of allowed expression types in the `PatType::AssignElement` match block. These TypeScript expression types are valid simple assignment targets when their inner expressions are valid. This aligns with how the `ExprExt` trait already correctly implements `is_valid_simple_assignment_target` for these expression types by delegating to the inner expression. Fixes #11142 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
|
|
|
|
🤖 This pull request has been linked to DevBird Task #870 View the task details and manage the automated development workflow in DevBird. |
📋 DevBird Task PromptObjectiveFix the SWC parser to correctly handle TypeScript non-null assertion operators (!) in destructuring assignment patterns, specifically in array element access expressions. Problem DescriptionThe SWC parser currently fails to parse code like: [arrayCopy[currentIndex]!, arrayCopy[randomIndex]!] = [arrayCopy[randomIndex]!, arrayCopy[currentIndex]!]It generates a "Not a pattern" error (SyntaxError::InvalidPat), while the TypeScript compiler successfully parses this code. Root CauseThe issue is in the When checking if an expression can be used as an assignment pattern (PatType::AssignElement), the code lists specific expression types that are allowed (lines 131-154):
However, SolutionAdd The fix should recognize that TypeScript non-null assertion expressions are valid simple assignment targets when their inner expression is valid. The Implementation Details
Documentation & ReferencesTypeScript Non-Null Assertion Operator:
SWC AST Structure:
Related SWC Code:
Testing Requirements
Success Criteria
Project-Specific Guidelines
GitHub Issue
This comment was automatically added by DevBird. You can disable this feature in DevBird Settings. |
|
Claude finished @DeLino[bot]'s task —— View job Code Review Complete ✅I've thoroughly reviewed this pull request that fixes TypeScript non-null assertion in destructuring assignments. Here's my comprehensive feedback: Review Checklist
Overall Assessment: ✅ LOOKS GOODThis is a well-implemented fix that correctly addresses the parser issue while maintaining consistency with existing code patterns. Code Quality & Best Practices ✅Strengths:
Location: Performance Considerations ✅
Security Considerations ✅
Test Coverage ✅Excellent test coverage in ✅ Original issue case: Array element swapping with non-null assertions The test file demonstrates the parser correctly produces Technical Implementation ✅The fix correctly addresses the root cause identified in the DevBird task:
This aligns perfectly with how the Adherence to Project Guidelines ✅Follows all CLAUDE.md requirements:
Minor SuggestionsDocumentation: Consider adding a brief comment explaining why these TS expression types are valid assignment targets: // TypeScript expression types that wrap valid assignment targets
| Expr::TsAs(..)
| Expr::TsNonNull(..) // Non-null assertion: expr!
| Expr::TsTypeAssertion(..) // Type assertion: <Type>expr
| Expr::TsInstantiation(..) // Type instantiation: expr<Type>
| Expr::TsSatisfies(..) => { // Satisfies expression: expr satisfies TypeHowever, this is optional since the existing code doesn't document the other expression types either. Changeset Status
|
CodSpeed Performance ReportMerging #11168 will not alter performanceComparing Summary
|
Summary
This PR fixes the parser to correctly handle TypeScript non-null assertion operators (
!) in destructuring assignment patterns, specifically in array element access expressions.Problem
Previously, code like this would fail to parse:
The parser would generate a "Not a pattern" error (
SyntaxError::InvalidPat), while the TypeScript compiler successfully parses this code.Root Cause
The issue was in the
reparse_expr_as_pat_innerfunction incrates/swc_ecma_lexer/src/common/parser/pat.rs. When checking if an expression can be used as an assignment pattern (PatType::AssignElement), the code listed specific expression types that are allowed, butExpr::TsNonNullwas not in this list.Solution
Added support for the following TypeScript expression types to the
PatType::AssignElementmatch block:Expr::TsNonNull(non-null assertion:expr!)Expr::TsTypeAssertion(type assertion:<Type>expr)Expr::TsInstantiation(type instantiation:expr<Type>)Expr::TsSatisfies(satisfies expression:expr satisfies Type)These TypeScript expression types are valid simple assignment targets when their inner expressions are valid. This aligns with how the
ExprExttrait already correctly implementsis_valid_simple_assignment_targetfor these expression types by delegating to the inner expression.Testing
Added comprehensive test cases in
crates/swc_ecma_parser/tests/typescript/issue-11142/covering:All existing tests continue to pass.
Related
Fixes #11142
🤖 Generated with Claude Code