[d565449] | 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');
|
---|
[0c6b92a] | 11 | const testReactVersion = require('../util/version').testReactVersion;
|
---|
[d565449] | 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) {
|
---|
[0c6b92a] | 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
|
---|
[d565449] | 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.
|
---|
[0c6b92a] | 51 | * @returns {boolean} True if we are using refs, false if not.
|
---|
[d565449] | 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.
|
---|
[0c6b92a] | 64 | * @returns {boolean} True if we are using a ref attribute, false if not.
|
---|
[d565449] | 65 | */
|
---|
| 66 | function isRefAttribute(node) {
|
---|
[0c6b92a] | 67 | return node.type === 'JSXAttribute'
|
---|
| 68 | && !!node.name
|
---|
| 69 | && node.name.name === 'ref';
|
---|
[d565449] | 70 | }
|
---|
| 71 |
|
---|
| 72 | /**
|
---|
| 73 | * Checks if a node contains a string value
|
---|
| 74 | * @param {ASTNode} node The AST node being checked.
|
---|
[0c6b92a] | 75 | * @returns {boolean} True if the node contains a string value, false if not.
|
---|
[d565449] | 76 | */
|
---|
| 77 | function containsStringLiteral(node) {
|
---|
[0c6b92a] | 78 | return !!node.value
|
---|
[d565449] | 79 | && node.value.type === 'Literal'
|
---|
[0c6b92a] | 80 | && typeof node.value.value === 'string';
|
---|
[d565449] | 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.
|
---|
[0c6b92a] | 86 | * @returns {boolean} True if the node contains a string value within a jsx expression, false if not.
|
---|
[d565449] | 87 | */
|
---|
| 88 | function containsStringExpressionContainer(node) {
|
---|
[0c6b92a] | 89 | return !!node.value
|
---|
[d565449] | 90 | && node.value.type === 'JSXExpressionContainer'
|
---|
| 91 | && node.value.expression
|
---|
| 92 | && ((node.value.expression.type === 'Literal' && typeof node.value.expression.value === 'string')
|
---|
[0c6b92a] | 93 | || (node.value.expression.type === 'TemplateLiteral' && detectTemplateLiterals));
|
---|
[d565449] | 94 | }
|
---|
| 95 |
|
---|
| 96 | return {
|
---|
| 97 | MemberExpression(node) {
|
---|
[0c6b92a] | 98 | if (checkRefsUsage && isRefsUsage(node)) {
|
---|
[d565449] | 99 | report(context, messages.thisRefsDeprecated, 'thisRefsDeprecated', {
|
---|
| 100 | node,
|
---|
| 101 | });
|
---|
| 102 | }
|
---|
| 103 | },
|
---|
[0c6b92a] | 104 |
|
---|
[d565449] | 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 | };
|
---|