1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | 'use strict';
|
7 |
|
8 | const has = require('has');
|
9 | const docsUrl = require('../util/docsUrl');
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | module.exports = {
|
16 | meta: {
|
17 | docs: {
|
18 | description: 'Enforce no duplicate props',
|
19 | category: 'Possible Errors',
|
20 | recommended: true,
|
21 | url: docsUrl('jsx-no-duplicate-props')
|
22 | },
|
23 |
|
24 | schema: [{
|
25 | type: 'object',
|
26 | properties: {
|
27 | ignoreCase: {
|
28 | type: 'boolean'
|
29 | }
|
30 | },
|
31 | additionalProperties: false
|
32 | }]
|
33 | },
|
34 |
|
35 | create(context) {
|
36 | const configuration = context.options[0] || {};
|
37 | const ignoreCase = configuration.ignoreCase || false;
|
38 |
|
39 | return {
|
40 | JSXOpeningElement(node) {
|
41 | const props = {};
|
42 |
|
43 | node.attributes.forEach((decl) => {
|
44 | if (decl.type === 'JSXSpreadAttribute') {
|
45 | return;
|
46 | }
|
47 |
|
48 | let name = decl.name.name;
|
49 |
|
50 | if (typeof name !== 'string') {
|
51 | return;
|
52 | }
|
53 |
|
54 | if (ignoreCase) {
|
55 | name = name.toLowerCase();
|
56 | }
|
57 |
|
58 | if (has(props, name)) {
|
59 | context.report({
|
60 | node: decl,
|
61 | message: 'No duplicate props allowed'
|
62 | });
|
63 | } else {
|
64 | props[name] = 1;
|
65 | }
|
66 | });
|
67 | }
|
68 | };
|
69 | }
|
70 | };
|