source: imaps-frontend/node_modules/eslint-plugin-react/lib/rules/no-adjacent-inline-elements.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: 3.1 KB
RevLine 
[d565449]1/**
2 * @fileoverview Prevent adjacent inline elements not separated by whitespace.
3 * @author Sean Hayes
4 */
5
6'use strict';
7
8const docsUrl = require('../util/docsUrl');
9const isCreateElement = require('../util/isCreateElement');
10const report = require('../util/report');
[0c6b92a]11const astUtil = require('../util/ast');
[d565449]12
13// ------------------------------------------------------------------------------
14// Helpers
15// ------------------------------------------------------------------------------
16
17// https://developer.mozilla.org/en-US/docs/Web/HTML/Inline_elements
18const inlineNames = [
19 'a',
20 'b',
21 'big',
22 'i',
23 'small',
24 'tt',
25 'abbr',
26 'acronym',
27 'cite',
28 'code',
29 'dfn',
30 'em',
31 'kbd',
32 'strong',
33 'samp',
34 'time',
35 'var',
36 'bdo',
37 'br',
38 'img',
39 'map',
40 'object',
41 'q',
42 'script',
43 'span',
44 'sub',
45 'sup',
46 'button',
47 'input',
48 'label',
49 'select',
50 'textarea',
51];
52// Note: raw &nbsp; will be transformed into \u00a0.
53const whitespaceRegex = /(?:^\s|\s$)/;
54
55function isInline(node) {
56 if (node.type === 'Literal') {
57 // Regular whitespace will be removed.
58 const value = node.value;
59 // To properly separate inline elements, each end of the literal will need
60 // whitespace.
61 return !whitespaceRegex.test(value);
62 }
63 if (node.type === 'JSXElement' && inlineNames.indexOf(node.openingElement.name.name) > -1) {
64 return true;
65 }
[0c6b92a]66 if (astUtil.isCallExpression(node) && inlineNames.indexOf(node.arguments[0].value) > -1) {
[d565449]67 return true;
68 }
69 return false;
70}
71
72// ------------------------------------------------------------------------------
73// Rule Definition
74// ------------------------------------------------------------------------------
75
76const messages = {
77 inlineElement: 'Child elements which render as inline HTML elements should be separated by a space or wrapped in block level elements.',
78};
79
[0c6b92a]80/** @type {import('eslint').Rule.RuleModule} */
[d565449]81module.exports = {
82 meta: {
83 docs: {
84 description: 'Disallow adjacent inline elements not separated by whitespace.',
85 category: 'Best Practices',
86 recommended: false,
87 url: docsUrl('no-adjacent-inline-elements'),
88 },
89 schema: [],
90
91 messages,
92 },
93 create(context) {
94 function validate(node, children) {
95 let currentIsInline = false;
96 let previousIsInline = false;
97 if (!children) {
98 return;
99 }
100 for (let i = 0; i < children.length; i++) {
101 currentIsInline = isInline(children[i]);
102 if (previousIsInline && currentIsInline) {
103 report(context, messages.inlineElement, 'inlineElement', {
104 node,
105 });
106 return;
107 }
108 previousIsInline = currentIsInline;
109 }
110 }
111 return {
112 JSXElement(node) {
113 validate(node, node.children);
114 },
115 CallExpression(node) {
116 if (!isCreateElement(context, node)) {
117 return;
118 }
119 if (node.arguments.length < 2 || !node.arguments[2]) {
120 return;
121 }
[0c6b92a]122 const children = 'elements' in node.arguments[2] ? node.arguments[2].elements : undefined;
[d565449]123 validate(node, children);
124 },
125 };
126 },
127};
Note: See TracBrowser for help on using the repository browser.