1 | /**
|
---|
2 | * @fileoverview Prevent string definitions for references and prevent referencing this.refs
|
---|
3 | * @author Tom Hastjarjanto
|
---|
4 | */
|
---|
5 |
|
---|
6 | 'use strict';
|
---|
7 |
|
---|
8 | const componentUtil = require('../util/componentUtil');
|
---|
9 | const docsUrl = require('../util/docsUrl');
|
---|
10 | const report = require('../util/report');
|
---|
11 | const testReactVersion = require('../util/version').testReactVersion;
|
---|
12 |
|
---|
13 | // ------------------------------------------------------------------------------
|
---|
14 | // Rule Definition
|
---|
15 | // ------------------------------------------------------------------------------
|
---|
16 |
|
---|
17 | const messages = {
|
---|
18 | thisRefsDeprecated: 'Using this.refs is deprecated.',
|
---|
19 | stringInRefDeprecated: 'Using string literals in ref attributes is deprecated.',
|
---|
20 | };
|
---|
21 |
|
---|
22 | /** @type {import('eslint').Rule.RuleModule} */
|
---|
23 | module.exports = {
|
---|
24 | meta: {
|
---|
25 | docs: {
|
---|
26 | description: 'Disallow using string references',
|
---|
27 | category: 'Best Practices',
|
---|
28 | recommended: true,
|
---|
29 | url: docsUrl('no-string-refs'),
|
---|
30 | },
|
---|
31 |
|
---|
32 | messages,
|
---|
33 |
|
---|
34 | schema: [{
|
---|
35 | type: 'object',
|
---|
36 | properties: {
|
---|
37 | noTemplateLiterals: {
|
---|
38 | type: 'boolean',
|
---|
39 | },
|
---|
40 | },
|
---|
41 | additionalProperties: false,
|
---|
42 | }],
|
---|
43 | },
|
---|
44 |
|
---|
45 | create(context) {
|
---|
46 | const checkRefsUsage = testReactVersion(context, '< 18.3.0'); // `this.refs` is writable in React 18.3.0 and later, see https://github.com/facebook/react/pull/28867
|
---|
47 | const detectTemplateLiterals = context.options[0] ? context.options[0].noTemplateLiterals : false;
|
---|
48 | /**
|
---|
49 | * Checks if we are using refs
|
---|
50 | * @param {ASTNode} node The AST node being checked.
|
---|
51 | * @returns {boolean} True if we are using refs, false if not.
|
---|
52 | */
|
---|
53 | function isRefsUsage(node) {
|
---|
54 | return !!(
|
---|
55 | (componentUtil.getParentES6Component(context, node) || componentUtil.getParentES5Component(context, node))
|
---|
56 | && node.object.type === 'ThisExpression'
|
---|
57 | && node.property.name === 'refs'
|
---|
58 | );
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Checks if we are using a ref attribute
|
---|
63 | * @param {ASTNode} node The AST node being checked.
|
---|
64 | * @returns {boolean} True if we are using a ref attribute, false if not.
|
---|
65 | */
|
---|
66 | function isRefAttribute(node) {
|
---|
67 | return node.type === 'JSXAttribute'
|
---|
68 | && !!node.name
|
---|
69 | && node.name.name === 'ref';
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Checks if a node contains a string value
|
---|
74 | * @param {ASTNode} node The AST node being checked.
|
---|
75 | * @returns {boolean} True if the node contains a string value, false if not.
|
---|
76 | */
|
---|
77 | function containsStringLiteral(node) {
|
---|
78 | return !!node.value
|
---|
79 | && node.value.type === 'Literal'
|
---|
80 | && typeof node.value.value === 'string';
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Checks if a node contains a string value within a jsx expression
|
---|
85 | * @param {ASTNode} node The AST node being checked.
|
---|
86 | * @returns {boolean} True if the node contains a string value within a jsx expression, false if not.
|
---|
87 | */
|
---|
88 | function containsStringExpressionContainer(node) {
|
---|
89 | return !!node.value
|
---|
90 | && node.value.type === 'JSXExpressionContainer'
|
---|
91 | && node.value.expression
|
---|
92 | && ((node.value.expression.type === 'Literal' && typeof node.value.expression.value === 'string')
|
---|
93 | || (node.value.expression.type === 'TemplateLiteral' && detectTemplateLiterals));
|
---|
94 | }
|
---|
95 |
|
---|
96 | return {
|
---|
97 | MemberExpression(node) {
|
---|
98 | if (checkRefsUsage && isRefsUsage(node)) {
|
---|
99 | report(context, messages.thisRefsDeprecated, 'thisRefsDeprecated', {
|
---|
100 | node,
|
---|
101 | });
|
---|
102 | }
|
---|
103 | },
|
---|
104 |
|
---|
105 | JSXAttribute(node) {
|
---|
106 | if (
|
---|
107 | isRefAttribute(node)
|
---|
108 | && (containsStringLiteral(node) || containsStringExpressionContainer(node))
|
---|
109 | ) {
|
---|
110 | report(context, messages.stringInRefDeprecated, 'stringInRefDeprecated', {
|
---|
111 | node,
|
---|
112 | });
|
---|
113 | }
|
---|
114 | },
|
---|
115 | };
|
---|
116 | },
|
---|
117 | };
|
---|