|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2018 Palantir Technologies, Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import * as Lint from "tslint"; |
| 19 | +import { isCallExpression, isClassDeclaration, isPropertyAccessExpression } from "tsutils"; |
| 20 | +import * as ts from "typescript"; |
| 21 | + |
| 22 | +export class Rule extends Lint.Rules.AbstractRule { |
| 23 | + /* tslint:disable:object-literal-sort-keys */ |
| 24 | + public static metadata: Lint.IRuleMetadata = { |
| 25 | + ruleName: "no-access-state-in-setstate", |
| 26 | + description: "Reports usage of this.state within setState", |
| 27 | + rationale: Lint.Utils.dedent` |
| 28 | + Usage of this.state might result in errors when two state calls are |
| 29 | + called in batch and thus referencing old state and not the current state. |
| 30 | + See [setState()](https://reactjs.org/docs/react-component.html#setstate) in the React API reference. |
| 31 | + `, |
| 32 | + options: null, |
| 33 | + optionsDescription: "", |
| 34 | + type: "functionality", |
| 35 | + typescriptOnly: false, |
| 36 | + }; |
| 37 | + /* tslint:enable:object-literal-sort-keys */ |
| 38 | + |
| 39 | + public static OBJECT_ARG_FAILURE = |
| 40 | + "References to this.state are not allowed in the setState state change object."; |
| 41 | + |
| 42 | + public static CALLBACK_ARG_FAILURE = |
| 43 | + "References to this.state are not allowed in the setState updater, use the callback arguments instead."; |
| 44 | + |
| 45 | + public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { |
| 46 | + return this.applyWithFunction(sourceFile, walk); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +function walk(ctx: Lint.WalkContext<void>): void { |
| 51 | + return ts.forEachChild(ctx.sourceFile, callbackForEachChild); |
| 52 | + |
| 53 | + function callbackForEachChild(node: ts.Node): void { |
| 54 | + if (!isClassDeclaration(node)) { |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + ts.forEachChild(node, callbackForEachChildInClass); |
| 59 | + } |
| 60 | + |
| 61 | + function callbackForEachChildInClass(node: ts.Node): void { |
| 62 | + if (!isCallExpression(node)) { |
| 63 | + return ts.forEachChild(node, callbackForEachChildInClass); |
| 64 | + } |
| 65 | + |
| 66 | + const callExpressionArguments = node.arguments; |
| 67 | + |
| 68 | + if (!isPropertyAccessExpression(node.expression) || callExpressionArguments.length === 0) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + const propertyAccessExpression = node.expression; |
| 73 | + |
| 74 | + const isThisPropertyAccess = propertyAccessExpression.expression.kind === ts.SyntaxKind.ThisKeyword; |
| 75 | + const isSetStateCall = propertyAccessExpression.name.text === "setState"; |
| 76 | + |
| 77 | + if (!isThisPropertyAccess || !isSetStateCall) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + const firstArgument = node.arguments[0]; |
| 82 | + |
| 83 | + if (ts.isObjectLiteralExpression(firstArgument)) { |
| 84 | + ts.forEachChild(firstArgument, callbackForEachChildInSetStateObjectArgument); |
| 85 | + } else if (ts.isArrowFunction(firstArgument) || ts.isFunctionExpression(firstArgument)) { |
| 86 | + ts.forEachChild(firstArgument, callbackForEachChildInSetStateCallbackArgument); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + function callbackForEachChildInSetStateObjectArgument(node: ts.Node): void { |
| 91 | + if (!isPropertyAccessExpression(node) || !isPropertyAccessExpression(node.expression)) { |
| 92 | + return ts.forEachChild(node, callbackForEachChildInSetStateObjectArgument); |
| 93 | + } |
| 94 | + |
| 95 | + if ( |
| 96 | + node.expression.expression.kind !== ts.SyntaxKind.ThisKeyword || |
| 97 | + node.expression.name.text !== "state" |
| 98 | + ) { |
| 99 | + return ts.forEachChild(node, callbackForEachChildInSetStateObjectArgument); |
| 100 | + } |
| 101 | + |
| 102 | + ctx.addFailureAtNode(node, Rule.OBJECT_ARG_FAILURE); |
| 103 | + } |
| 104 | + |
| 105 | + function callbackForEachChildInSetStateCallbackArgument(node: ts.Node): void { |
| 106 | + if (!isPropertyAccessExpression(node) || !isPropertyAccessExpression(node.expression)) { |
| 107 | + return ts.forEachChild(node, callbackForEachChildInSetStateCallbackArgument); |
| 108 | + } |
| 109 | + |
| 110 | + if ( |
| 111 | + node.expression.expression.kind !== ts.SyntaxKind.ThisKeyword || |
| 112 | + node.expression.name.text !== "state" |
| 113 | + ) { |
| 114 | + return ts.forEachChild(node, callbackForEachChildInSetStateCallbackArgument); |
| 115 | + } |
| 116 | + |
| 117 | + ctx.addFailureAtNode(node, Rule.CALLBACK_ARG_FAILURE); |
| 118 | + } |
| 119 | +} |
0 commit comments