source: imaps-frontend/node_modules/eslint-plugin-react/lib/rules/no-set-state.js@ 0c6b92a

main
Last change on this file since 0c6b92a was 0c6b92a, checked in by stefan toskovski <stefantoska84@…>, 5 weeks ago

Pred finalna verzija

  • Property mode set to 100644
File size: 2.3 KB
RevLine 
[d565449]1/**
2 * @fileoverview Prevent usage of setState
3 * @author Mark Dalgleish
4 */
5
6'use strict';
7
8const values = require('object.values');
9
10const Components = require('../util/Components');
11const docsUrl = require('../util/docsUrl');
12const report = require('../util/report');
13
14// ------------------------------------------------------------------------------
15// Rule Definition
16// ------------------------------------------------------------------------------
17
18const messages = {
19 noSetState: 'Do not use setState',
20};
21
22/** @type {import('eslint').Rule.RuleModule} */
23module.exports = {
24 meta: {
25 docs: {
26 description: 'Disallow usage of setState',
27 category: 'Stylistic Issues',
28 recommended: false,
29 url: docsUrl('no-set-state'),
30 },
31
32 messages,
33
34 schema: [],
35 },
36
37 create: Components.detect((context, components, utils) => {
38 /**
39 * Checks if the component is valid
40 * @param {Object} component The component to process
[0c6b92a]41 * @returns {boolean} True if the component is valid, false if not.
[d565449]42 */
43 function isValid(component) {
[0c6b92a]44 return !!component && !component.useSetState;
[d565449]45 }
46
47 /**
48 * Reports usages of setState for a given component
49 * @param {Object} component The component to process
50 */
51 function reportSetStateUsages(component) {
52 for (let i = 0, j = component.setStateUsages.length; i < j; i++) {
[0c6b92a]53 const setStateUsage = component.setStateUsages[i];
[d565449]54 report(context, messages.noSetState, 'noSetState', {
55 node: setStateUsage,
56 });
57 }
58 }
59
60 return {
61 CallExpression(node) {
62 const callee = node.callee;
63 if (
64 callee.type !== 'MemberExpression'
65 || callee.object.type !== 'ThisExpression'
66 || callee.property.name !== 'setState'
67 ) {
68 return;
69 }
70 const component = components.get(utils.getParentComponent(node));
71 const setStateUsages = (component && component.setStateUsages) || [];
72 setStateUsages.push(callee);
73 components.set(node, {
74 useSetState: true,
75 setStateUsages,
76 });
77 },
78
79 'Program:exit'() {
80 values(components.list())
81 .filter((component) => !isValid(component))
82 .forEach((component) => {
83 reportSetStateUsages(component);
84 });
85 },
86 };
87 }),
88};
Note: See TracBrowser for help on using the repository browser.