Movatterモバイル変換


[0]ホーム

URL:


CodeQL documentation
CodeQL resources

Non-linear pattern

ID: js/non-linear-patternKind: problemSecurity severity: Severity: errorPrecision: very-highTags:   - quality   - reliability   - correctness   - language-featuresQuery suites:   - javascript-security-and-quality.qls

Click to see the query in the CodeQL repository

If the same pattern variable is bound multiple times in the same object or array pattern, the last binding overwrites all of the earlier ones. This is most likely unintended and should be avoided.

In TypeScript, a common mistake is to try to write type annotations inside a pattern. This is not possible, and the type annotation should come after the pattern.

Recommendation

Rename the pattern variables to have different names. In an array pattern, elements that do not need to be bound can be omitted.

Example

In the following example, the functiondistanceFromOrigin uses an array pattern to decompose its argumentpoint. The pattern bindsx twice: first,x is bound topoint[0], the first element ofpoint; this binding is then immediately overwritten by a binding topoint[1], which is probably unintended.

functiondistanceFromOrigin(point){var[x,x]=point;returnMath.sqrt(x*x+y*y);}

From context, it appears that the second binding should have been for variabley like this:

functiondistanceFromOrigin(point){var[x,y]=point;returnMath.sqrt(x*x+y*y);}

This can sometimes happen in TypeScript, due to the apparent similarity between property patterns and type annotations. In the following example, the function uses a pattern parameter with propertiesx andy. These appear to have typenumber, but are in fact untyped properties both stored in a variable namednumber.

functiondistance({x:number,y:number}){returnMath.sqrt(x*x+y*y);}

It is not possible to specify type annotations inside a pattern. The correct way is to specify the type after the parameter:

functiondistance({x,y}:{x:number,y:number}){returnMath.sqrt(x*x+y*y);}

References


[8]ページ先頭

©2009-2025 Movatter.jp