[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');
|
---|
| 11 |
|
---|
| 12 | // ------------------------------------------------------------------------------
|
---|
| 13 | // Rule Definition
|
---|
| 14 | // ------------------------------------------------------------------------------
|
---|
| 15 |
|
---|
| 16 | const messages = {
|
---|
| 17 | thisRefsDeprecated: 'Using this.refs is deprecated.',
|
---|
| 18 | stringInRefDeprecated: 'Using string literals in ref attributes is deprecated.',
|
---|
| 19 | };
|
---|
| 20 |
|
---|
| 21 | /** @type {import('eslint').Rule.RuleModule} */
|
---|
| 22 | module.exports = {
|
---|
| 23 | meta: {
|
---|
| 24 | docs: {
|
---|
| 25 | description: 'Disallow using string references',
|
---|
| 26 | category: 'Best Practices',
|
---|
| 27 | recommended: true,
|
---|
| 28 | url: docsUrl('no-string-refs'),
|
---|
| 29 | },
|
---|
| 30 |
|
---|
| 31 | messages,
|
---|
| 32 |
|
---|
| 33 | schema: [{
|
---|
| 34 | type: 'object',
|
---|
| 35 | properties: {
|
---|
| 36 | noTemplateLiterals: {
|
---|
| 37 | type: 'boolean',
|
---|
| 38 | },
|
---|
| 39 | },
|
---|
| 40 | additionalProperties: false,
|
---|
| 41 | }],
|
---|
| 42 | },
|
---|
| 43 |
|
---|
| 44 | create(context) {
|
---|
| 45 | const detectTemplateLiterals = context.options[0] ? context.options[0].noTemplateLiterals : false;
|
---|
| 46 | /**
|
---|
| 47 | * Checks if we are using refs
|
---|
| 48 | * @param {ASTNode} node The AST node being checked.
|
---|
| 49 | * @returns {Boolean} True if we are using refs, false if not.
|
---|
| 50 | */
|
---|
| 51 | function isRefsUsage(node) {
|
---|
| 52 | return !!(
|
---|
| 53 | (componentUtil.getParentES6Component(context, node) || componentUtil.getParentES5Component(context, node))
|
---|
| 54 | && node.object.type === 'ThisExpression'
|
---|
| 55 | && node.property.name === 'refs'
|
---|
| 56 | );
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | /**
|
---|
| 60 | * Checks if we are using a ref attribute
|
---|
| 61 | * @param {ASTNode} node The AST node being checked.
|
---|
| 62 | * @returns {Boolean} True if we are using a ref attribute, false if not.
|
---|
| 63 | */
|
---|
| 64 | function isRefAttribute(node) {
|
---|
| 65 | return !!(
|
---|
| 66 | node.type === 'JSXAttribute'
|
---|
| 67 | && node.name
|
---|
| 68 | && node.name.name === 'ref'
|
---|
| 69 | );
|
---|
| 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 !!(
|
---|
| 79 | node.value
|
---|
| 80 | && node.value.type === 'Literal'
|
---|
| 81 | && typeof node.value.value === 'string'
|
---|
| 82 | );
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | /**
|
---|
| 86 | * Checks if a node contains a string value within a jsx expression
|
---|
| 87 | * @param {ASTNode} node The AST node being checked.
|
---|
| 88 | * @returns {Boolean} True if the node contains a string value within a jsx expression, false if not.
|
---|
| 89 | */
|
---|
| 90 | function containsStringExpressionContainer(node) {
|
---|
| 91 | return !!(
|
---|
| 92 | node.value
|
---|
| 93 | && node.value.type === 'JSXExpressionContainer'
|
---|
| 94 | && node.value.expression
|
---|
| 95 | && ((node.value.expression.type === 'Literal' && typeof node.value.expression.value === 'string')
|
---|
| 96 | || (node.value.expression.type === 'TemplateLiteral' && detectTemplateLiterals))
|
---|
| 97 | );
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | return {
|
---|
| 101 | MemberExpression(node) {
|
---|
| 102 | if (isRefsUsage(node)) {
|
---|
| 103 | report(context, messages.thisRefsDeprecated, 'thisRefsDeprecated', {
|
---|
| 104 | node,
|
---|
| 105 | });
|
---|
| 106 | }
|
---|
| 107 | },
|
---|
| 108 | JSXAttribute(node) {
|
---|
| 109 | if (
|
---|
| 110 | isRefAttribute(node)
|
---|
| 111 | && (containsStringLiteral(node) || containsStringExpressionContainer(node))
|
---|
| 112 | ) {
|
---|
| 113 | report(context, messages.stringInRefDeprecated, 'stringInRefDeprecated', {
|
---|
| 114 | node,
|
---|
| 115 | });
|
---|
| 116 | }
|
---|
| 117 | },
|
---|
| 118 | };
|
---|
| 119 | },
|
---|
| 120 | };
|
---|