Skip to content

Commit

Permalink
fix(eslint-plugin): [no-unnec-conditions] better check for isArrayType
Browse files Browse the repository at this point in the history
  • Loading branch information
Retsam committed Nov 27, 2019
1 parent 1ebc414 commit 4e9531d
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions packages/eslint-plugin/src/rules/no-unnecessary-condition.ts
Expand Up @@ -42,6 +42,25 @@ const isLiteral = (type: ts.Type): boolean =>
isLiteralType(type);
// #endregion

// Array type utilities
// #region
function isTypeReference(type: ts.Type): type is ts.TypeReference {
return !!(
type.getFlags() & ts.TypeFlags.Object &&
(type as ts.ObjectType).objectFlags & ts.ObjectFlags.Reference
);
}

// There's a built-in checker.isArrayType, but it's marked as internal.
function isArrayType(type: ts.Type): boolean {
return (
isTypeReference(type) &&
(type.target.symbol.name === 'Array' ||
type.target.symbol.name === 'ReadonlyArray')
);
}
// #endregion

export type Options = [
{
allowConstantLoopConditions?: boolean;
Expand Down Expand Up @@ -117,10 +136,7 @@ export default createRule<Options, MessageId>({
}

function nodeIsArrayType(node: TSESTree.Node): boolean {
const type = getNodeType(node);
// TODO: is there a better way to determine if this is an array type?
// Currently just checking for a `lastIndexOf` method.
return !!type.getProperty('lastIndexOf');
return isArrayType(getNodeType(node));
}

/**
Expand Down

0 comments on commit 4e9531d

Please sign in to comment.