1 | /**
|
---|
2 | * @fileoverview Enforce that namespaces are not used in React elements
|
---|
3 | * @author Yacine Hmito
|
---|
4 | */
|
---|
5 |
|
---|
6 | 'use strict';
|
---|
7 |
|
---|
8 | const elementType = require('jsx-ast-utils/elementType');
|
---|
9 | const docsUrl = require('../util/docsUrl');
|
---|
10 | const isCreateElement = require('../util/isCreateElement');
|
---|
11 | const report = require('../util/report');
|
---|
12 |
|
---|
13 | // ------------------------------------------------------------------------------
|
---|
14 | // Rule Definition
|
---|
15 | // ------------------------------------------------------------------------------
|
---|
16 |
|
---|
17 | const messages = {
|
---|
18 | noNamespace: 'React component {{name}} must not be in a namespace, as React does not support them',
|
---|
19 | };
|
---|
20 |
|
---|
21 | /** @type {import('eslint').Rule.RuleModule} */
|
---|
22 | module.exports = {
|
---|
23 | meta: {
|
---|
24 | docs: {
|
---|
25 | description: 'Enforce that namespaces are not used in React elements',
|
---|
26 | category: 'Possible Errors',
|
---|
27 | recommended: false,
|
---|
28 | url: docsUrl('no-namespace'),
|
---|
29 | },
|
---|
30 |
|
---|
31 | messages,
|
---|
32 |
|
---|
33 | schema: [],
|
---|
34 | },
|
---|
35 |
|
---|
36 | create(context) {
|
---|
37 | return {
|
---|
38 | CallExpression(node) {
|
---|
39 | if (isCreateElement(context, node) && node.arguments.length > 0 && node.arguments[0].type === 'Literal') {
|
---|
40 | const name = node.arguments[0].value;
|
---|
41 | if (typeof name !== 'string' || name.indexOf(':') === -1) return undefined;
|
---|
42 | report(context, messages.noNamespace, 'noNamespace', {
|
---|
43 | node,
|
---|
44 | data: {
|
---|
45 | name,
|
---|
46 | },
|
---|
47 | });
|
---|
48 | }
|
---|
49 | },
|
---|
50 | JSXOpeningElement(node) {
|
---|
51 | const name = elementType(node);
|
---|
52 | if (typeof name !== 'string' || name.indexOf(':') === -1) return undefined;
|
---|
53 | report(context, messages.noNamespace, 'noNamespace', {
|
---|
54 | node,
|
---|
55 | data: {
|
---|
56 | name,
|
---|
57 | },
|
---|
58 | });
|
---|
59 | },
|
---|
60 | };
|
---|
61 | },
|
---|
62 | };
|
---|