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

Commit

Permalink
initial no direct mutation state identification
Browse files Browse the repository at this point in the history
  • Loading branch information
macovedj committed May 7, 2020
1 parent 1e01c03 commit 0d7069a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/@romejs/diagnostics/categories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ type LintDiagnosticCategory =
| 'lint/noDebugger'
| 'lint/noDelete'
| 'lint/noDeleteVars'
| 'lint/noDirectMutationState'
| 'lint/noDupeArgs'
| 'lint/noDuplicateCase'
| 'lint/noDuplicateGroupNamesInRegularExpressions'
Expand Down
4 changes: 4 additions & 0 deletions packages/@romejs/diagnostics/descriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,10 @@ export const descriptions = createMessages({
category: 'lint/noChildrenProp',
message: 'children should not be passed as a prop',
},
NO_DIRECT_MUTATION_STATE: {
category: 'lint/noDirectMutationState',
message: 'Cannot directly mutate state, try using setState or spreading',
},
PREFER_BLOCK_STATEMENT: {
category: 'lint/preferBlockStatements',
message: 'Block statements are preferred in this position',
Expand Down
2 changes: 2 additions & 0 deletions packages/@romejs/js-compiler/lint/rules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import noCondAssign from './regular/noCondAssign';
import noDebugger from './regular/noDebugger';
import noDelete from './regular/noDelete';
import noDeleteVars from './regular/noDeleteVars';
import noDirectMutationState from './react/noDirectMutationState';
import noDupeArgs from './regular/noDupeArgs';
import noDuplicateCase from './regular/noDuplicateCase';
import noDuplicateGroupNamesInRegularExpressions from './regular/noDuplicateGroupNamesInRegularExpressions';
Expand Down Expand Up @@ -79,6 +80,7 @@ export const lintTransforms = [
noDebugger,
noDelete,
noDeleteVars,
noDirectMutationState,
noDupeArgs,
noDuplicateCase,
noDuplicateGroupNamesInRegularExpressions,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import {Path} from '@romejs/js-compiler';
import {AnyNode} from '@romejs/js-ast';
import {descriptions} from '@romejs/diagnostics';

export default {
name: 'noDirectMutationState',
enter(path: Path): AnyNode {
const {node} = path;

if (
node.type === 'ClassDeclaration'
) {
for (let bodyNode of node.meta.body) {
if (bodyNode.type === 'ClassMethod') {
for (let bodyBodyNode of bodyNode.body.body) {
if (
bodyBodyNode.type === 'ExpressionStatement' &&
bodyBodyNode.expression.type === 'AssignmentExpression' &&
bodyBodyNode.expression.left.type === 'MemberExpression' &&
bodyBodyNode.expression.left.object.type === 'MemberExpression' &&
bodyBodyNode.expression.left.object.object.type === 'ThisExpression' &&
bodyBodyNode.expression.left.object.property.value.type === 'Identifier' &&
bodyBodyNode.expression.left.object.property.value.name === 'state'
) {
path.context.addNodeDiagnostic(node, descriptions.LINT.NO_DIRECT_MUTATION_STATE);
}
}
}
}
}

return node;
},
};

0 comments on commit 0d7069a

Please sign in to comment.