Skip to content

Commit

Permalink
Fixed #132 (#133)
Browse files Browse the repository at this point in the history
* Fixed #132

* Adjusted rules code style.
  • Loading branch information
RebeccaStevens authored Mar 24, 2019
1 parent f410a28 commit fd31899
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 92 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

* readonly-array rule with the option ignore-prefix set will now ignore nested arrays within an ignored variable. See [#132](https://github.com/jonaskello/tslint-immutable/issues/132). See PR [#133](https://github.com/jonaskello/tslint-immutable/pull/133)

## [v5.5.1] - 2019-03-22

* no-array-mutation now checks nested arrays. See [#134](https://github.com/jonaskello/tslint-immutable/issues/134). See PR [#135](https://github.com/jonaskello/tslint-immutable/pull/135)
Expand Down
46 changes: 19 additions & 27 deletions src/noArrayMutationRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { isAssignmentKind } from "tsutils/util";
import {
createInvalidNode,
CheckNodeResult,
createCheckNodeTypedRule,
InvalidNode
createCheckNodeTypedRule
} from "./shared/check-node";
import * as Ignore from "./shared/ignore";
import { isAccessExpression, AccessExpression } from "./shared/typeguard";
Expand Down Expand Up @@ -88,18 +87,10 @@ const newArrayReturningMethods: ReadonlyArray<string> = [
const constructorFunctions = ["from", "of"];

function checkTypedNode(
node: ts.BinaryExpression,
ctx: Lint.WalkContext<Options>,
checker: ts.TypeChecker
): CheckNodeResult {
return { invalidNodes: getInvalidNodes(node, ctx, checker) };
}

function getInvalidNodes(
node: ts.Node,
ctx: Lint.WalkContext<Options>,
checker: ts.TypeChecker
): ReadonlyArray<InvalidNode> {
): CheckNodeResult {
if (utils.isBinaryExpression(node)) {
return checkBinaryExpression(node, ctx, checker);
}
Expand All @@ -119,7 +110,8 @@ function getInvalidNodes(
if (utils.isCallExpression(node)) {
return checkCallExpression(node, ctx, checker);
}
return [];

return { invalidNodes: [] };
}

/**
Expand All @@ -130,7 +122,7 @@ function checkBinaryExpression(
node: ts.BinaryExpression,
ctx: Lint.WalkContext<Options>,
checker: ts.TypeChecker
): ReadonlyArray<InvalidNode> {
): CheckNodeResult {
if (
!Ignore.isIgnoredPrefix(
node.getText(node.getSourceFile()),
Expand All @@ -144,10 +136,10 @@ function checkBinaryExpression(
);

if (isArrayType(leftExpressionType)) {
return [createInvalidNode(node, [])];
return { invalidNodes: [createInvalidNode(node, [])] };
}
}
return [];
return { invalidNodes: [] };
}

/**
Expand All @@ -157,7 +149,7 @@ function checkDeleteExpression(
node: ts.DeleteExpression,
ctx: Lint.WalkContext<Options>,
checker: ts.TypeChecker
): ReadonlyArray<InvalidNode> {
): CheckNodeResult {
if (
!Ignore.isIgnoredPrefix(
node.expression.getText(node.getSourceFile()),
Expand All @@ -170,10 +162,10 @@ function checkDeleteExpression(
);

if (isArrayType(expressionType)) {
return [createInvalidNode(node, [])];
return { invalidNodes: [createInvalidNode(node, [])] };
}
}
return [];
return { invalidNodes: [] };
}

/**
Expand All @@ -183,7 +175,7 @@ function checkPrefixUnaryExpression(
node: ts.PrefixUnaryExpression,
ctx: Lint.WalkContext<Options>,
checker: ts.TypeChecker
): ReadonlyArray<InvalidNode> {
): CheckNodeResult {
if (
!Ignore.isIgnoredPrefix(
node.operand.getText(node.getSourceFile()),
Expand All @@ -197,10 +189,10 @@ function checkPrefixUnaryExpression(
);

if (isArrayType(operandExpressionType)) {
return [createInvalidNode(node, [])];
return { invalidNodes: [createInvalidNode(node, [])] };
}
}
return [];
return { invalidNodes: [] };
}

/**
Expand All @@ -210,7 +202,7 @@ function checkPostfixUnaryExpression(
node: ts.PostfixUnaryExpression,
ctx: Lint.WalkContext<Options>,
checker: ts.TypeChecker
): ReadonlyArray<InvalidNode> {
): CheckNodeResult {
if (
!Ignore.isIgnoredPrefix(
node.getText(node.getSourceFile()),
Expand All @@ -224,10 +216,10 @@ function checkPostfixUnaryExpression(
);

if (isArrayType(operandExpressionType)) {
return [createInvalidNode(node, [])];
return { invalidNodes: [createInvalidNode(node, [])] };
}
}
return [];
return { invalidNodes: [] };
}

/**
Expand All @@ -237,7 +229,7 @@ function checkCallExpression(
node: ts.CallExpression,
ctx: Lint.WalkContext<Options>,
checker: ts.TypeChecker
): ReadonlyArray<InvalidNode> {
): CheckNodeResult {
if (
!Ignore.isIgnoredPrefix(
node.getText(node.getSourceFile()),
Expand All @@ -258,10 +250,10 @@ function checkCallExpression(
);

if (isArrayType(expressionType)) {
return [createInvalidNode(node, [])];
return { invalidNodes: [createInvalidNode(node, [])] };
}
}
return [];
return { invalidNodes: [] };
}

/**
Expand Down
28 changes: 17 additions & 11 deletions src/noLetRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as Lint from "tslint";
import * as utils from "tsutils/typeguard/2.8";
import * as Ignore from "./shared/ignore";
import {
InvalidNode,
createInvalidNode,
CheckNodeResult,
createCheckNodeRule
Expand All @@ -21,27 +20,34 @@ function checkNode(
node: ts.Node,
ctx: Lint.WalkContext<Options>
): CheckNodeResult {
const variableStatementFailures = checkVariableStatement(node, ctx);
const forStatementsFailures = checkForStatements(node, ctx);
const results = [
checkVariableStatement(node, ctx),
checkForStatements(node, ctx)
];

return {
invalidNodes: [...variableStatementFailures, ...forStatementsFailures]
invalidNodes: results.reduce(
(merged, result) => [...merged, ...result.invalidNodes],
[]
),
skipChildren: results.some(result => result.skipChildren === true)
};
}

function checkVariableStatement(
node: ts.Node,
ctx: Lint.WalkContext<Options>
): ReadonlyArray<InvalidNode> {
): CheckNodeResult {
if (utils.isVariableStatement(node)) {
return checkDeclarationList(node.declarationList, ctx);
}
return [];
return { invalidNodes: [] };
}

function checkForStatements(
node: ts.Node,
ctx: Lint.WalkContext<Options>
): ReadonlyArray<InvalidNode> {
): CheckNodeResult {
if (
(utils.isForStatement(node) ||
utils.isForInStatement(node) ||
Expand All @@ -52,13 +58,13 @@ function checkForStatements(
) {
return checkDeclarationList(node.initializer, ctx);
}
return [];
return { invalidNodes: [] };
}

function checkDeclarationList(
declarationList: ts.VariableDeclarationList,
ctx: Lint.WalkContext<Options>
): ReadonlyArray<InvalidNode> {
): CheckNodeResult {
if (Lint.isNodeFlagSet(declarationList, ts.NodeFlags.Let)) {
// It is a let declaration, now check each variable that is declared
const invalidVariableDeclarationNodes = [];
Expand Down Expand Up @@ -94,7 +100,7 @@ function checkDeclarationList(
addFix = false;
}
}
return invalidVariableDeclarationNodes;
return { invalidNodes: invalidVariableDeclarationNodes };
}
return [];
return { invalidNodes: [] };
}
Loading

0 comments on commit fd31899

Please sign in to comment.