mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 20:32:10 +00:00
feat(linter): check ArrayPattern in javascript
This commit is contained in:
parent
76f7e58036
commit
39795dbb1b
3 changed files with 2239 additions and 280 deletions
|
|
@ -2,7 +2,7 @@
|
|||
use oxc_ast::{
|
||||
ast::*,
|
||||
syntax_directed_operations::{BoundNames, IsSimpleParameterList, PropName},
|
||||
AstKind, Atom, ModuleKind, Span,
|
||||
AstKind, Atom, GetSpan, ModuleKind, Span,
|
||||
};
|
||||
use oxc_diagnostics::{
|
||||
miette::{self, Diagnostic},
|
||||
|
|
@ -54,6 +54,7 @@ impl Rule for EarlyErrorJavaScript {
|
|||
|
||||
AstKind::FormalParameters(params) => check_formal_parameters(params, node, ctx),
|
||||
AstKind::FormalParameter(param) => check_formal_parameter(param, ctx),
|
||||
AstKind::ArrayPattern(pat) => check_array_pattern(pat, ctx),
|
||||
|
||||
AstKind::ObjectExpression(expr) => check_object_expression(expr, ctx),
|
||||
AstKind::BinaryExpression(expr) => check_binary_expression(expr, ctx),
|
||||
|
|
@ -819,6 +820,26 @@ fn check_formal_parameter(param: &FormalParameter, ctx: &LintContext) {
|
|||
}
|
||||
}
|
||||
|
||||
fn check_array_pattern(pattern: &ArrayPattern, ctx: &LintContext) {
|
||||
#[derive(Debug, Error, Diagnostic)]
|
||||
#[error("A rest parameter cannot have an initializer")]
|
||||
#[diagnostic()]
|
||||
struct ARestParameterCannotHaveAnInitializer(#[label] Span);
|
||||
|
||||
for elem in pattern.elements.iter().flatten() {
|
||||
match &elem.kind {
|
||||
// function foo([...x = []]) { }
|
||||
// ^^^^ A rest element cannot have an initializer
|
||||
BindingPatternKind::RestElement(pat)
|
||||
if matches!(pat.argument.kind, BindingPatternKind::AssignmentPattern(_)) =>
|
||||
{
|
||||
ctx.diagnostic(ARestParameterCannotHaveAnInitializer(elem.span()));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn check_object_expression(obj_expr: &ObjectExpression, ctx: &LintContext) {
|
||||
// ObjectLiteral : { PropertyDefinitionList }
|
||||
// It is a Syntax Error if PropertyNameList of PropertyDefinitionList contains any duplicate entries for "__proto__"
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -1,7 +1,7 @@
|
|||
TypeScript Summary:
|
||||
AST Parsed : 2305/2338 (98.59%)
|
||||
Positive Passed: 2305/2338 (98.59%)
|
||||
Negative Passed: 660/2532 (26.07%)
|
||||
Negative Passed: 661/2532 (26.11%)
|
||||
Expect Syntax Error: "Symbols/ES5SymbolProperty2.ts"
|
||||
Expect Syntax Error: "Symbols/ES5SymbolProperty6.ts"
|
||||
Expect Syntax Error: "additionalChecks/noPropertyAccessFromIndexSignature1.ts"
|
||||
|
|
@ -520,7 +520,6 @@ Expect Syntax Error: "es6/destructuring/optionalBindingParametersInOverloads2.ts
|
|||
Expect Syntax Error: "es6/destructuring/restElementWithAssignmentPattern2.ts"
|
||||
Expect Syntax Error: "es6/destructuring/restElementWithAssignmentPattern4.ts"
|
||||
Expect Syntax Error: "es6/destructuring/restElementWithBindingPattern2.ts"
|
||||
Expect Syntax Error: "es6/destructuring/restElementWithInitializer1.ts"
|
||||
Expect Syntax Error: "es6/destructuring/restElementWithNullInitializer.ts"
|
||||
Expect Syntax Error: "es6/for-ofStatements/for-of10.ts"
|
||||
Expect Syntax Error: "es6/for-ofStatements/for-of11.ts"
|
||||
|
|
@ -5112,6 +5111,13 @@ Expect to Parse: "salsa/privateIdentifierExpando.ts"
|
|||
· ─────
|
||||
╰────
|
||||
|
||||
× A rest parameter cannot have an initializer
|
||||
╭─[es6/destructuring/restElementWithInitializer1.ts:1:1]
|
||||
1 │ var a: number[];
|
||||
2 │ var [...x = a] = a; // Error, rest element cannot have initializer
|
||||
· ────────
|
||||
╰────
|
||||
|
||||
× Invalid assignment
|
||||
╭─[es6/destructuring/restElementWithInitializer2.ts:2:1]
|
||||
2 │ var x: number[];
|
||||
|
|
|
|||
Loading…
Reference in a new issue