main
Last change
on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
1.4 KB
|
Rev | Line | |
---|
[d565449] | 1 | /**
|
---|
| 2 | * @fileoverview Prevent JSX prop spreading the same expression multiple times
|
---|
| 3 | * @author Simon Schick
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | 'use strict';
|
---|
| 7 |
|
---|
| 8 | const docsUrl = require('../util/docsUrl');
|
---|
| 9 | const report = require('../util/report');
|
---|
| 10 |
|
---|
| 11 | // ------------------------------------------------------------------------------
|
---|
| 12 | // Rule Definition
|
---|
| 13 | // ------------------------------------------------------------------------------
|
---|
| 14 |
|
---|
| 15 | const messages = {
|
---|
| 16 | noMultiSpreading: 'Spreading the same expression multiple times is forbidden',
|
---|
| 17 | };
|
---|
| 18 |
|
---|
| 19 | module.exports = {
|
---|
| 20 | meta: {
|
---|
| 21 | docs: {
|
---|
| 22 | description: 'Disallow JSX prop spreading the same identifier multiple times',
|
---|
| 23 | category: 'Best Practices',
|
---|
| 24 | recommended: false,
|
---|
| 25 | url: docsUrl('jsx-props-no-spread-multi'),
|
---|
| 26 | },
|
---|
| 27 | messages,
|
---|
| 28 | },
|
---|
| 29 |
|
---|
| 30 | create(context) {
|
---|
| 31 | return {
|
---|
| 32 | JSXOpeningElement(node) {
|
---|
| 33 | const spreads = node.attributes.filter(
|
---|
| 34 | (attr) => attr.type === 'JSXSpreadAttribute'
|
---|
| 35 | && attr.argument.type === 'Identifier'
|
---|
| 36 | );
|
---|
| 37 | if (spreads.length < 2) {
|
---|
| 38 | return;
|
---|
| 39 | }
|
---|
| 40 | // We detect duplicate expressions by their identifier
|
---|
| 41 | const identifierNames = new Set();
|
---|
| 42 | spreads.forEach((spread) => {
|
---|
| 43 | if (identifierNames.has(spread.argument.name)) {
|
---|
| 44 | report(context, messages.noMultiSpreading, 'noMultiSpreading', {
|
---|
| 45 | node: spread,
|
---|
| 46 | });
|
---|
| 47 | }
|
---|
| 48 | identifierNames.add(spread.argument.name);
|
---|
| 49 | });
|
---|
| 50 | },
|
---|
| 51 | };
|
---|
| 52 | },
|
---|
| 53 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.