Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Improve prefer-array-literal rule #862

Merged
3 changes: 3 additions & 0 deletions src/preferArrayLiteralRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ function walk(ctx: Lint.WalkContext<Options>, checker: ts.TypeChecker | undefine
// See https://github.com/microsoft/tslint-microsoft-contrib/issues/859
const argument = callArguments[0];
const argumentType = checker.getTypeAtLocation(argument);
// Looks like TS returns type or array elements when SpreadElement is passed to getTypeAtLocation.
// Additional check for SpreadElement is required to catch case when array has type number[] and its element will pass assignability check.
// SpreadElement shouldn't be allowed because result of `Array(...arr)` call will dependepend on array lenght and might produce unexpected results.
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
if (!tsutils.isTypeAssignableToNumber(checker, argumentType) || argument.kind === ts.SyntaxKind.SpreadElement) {
const failureString = Rule.getSizeParamFailureString(type);
ctx.addFailureAt(node.getStart(), node.getWidth(), failureString);
Expand Down