Skip to content

Commit

Permalink
Rename jsx-fragments mode
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzherdev committed Sep 24, 2018
1 parent 31da0ce commit 45019af
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
14 changes: 6 additions & 8 deletions lib/rules/jsx-fragments.js
Expand Up @@ -29,12 +29,12 @@ module.exports = {
fixable: 'code',

schema: [{
enum: ['always', 'never']
enum: ['syntax', 'element']
}]
},

create: function(context) {
const configuration = context.options[0] || 'always';
const configuration = context.options[0] || 'syntax';
const sourceCode = context.getSourceCode();
const reactPragma = pragmaUtil.getFromContext(context);
const fragmentPragma = pragmaUtil.getFragmentFromContext(context);
Expand Down Expand Up @@ -116,7 +116,7 @@ module.exports = {
}

const jsxElements = [];
const fragmentNames = [];
const fragmentNames = [`${reactPragma}.${fragmentPragma}`];

// --------------------------------------------------------------------------
// Public
Expand All @@ -132,7 +132,7 @@ module.exports = {
return;
}

if (configuration === 'never') {
if (configuration === 'element') {
context.report({
node,
message: `Prefer ${reactPragma}.${fragmentPragma} over fragment shorthand`,
Expand All @@ -154,19 +154,17 @@ module.exports = {
},

'Program:exit'() {
const possibleFragmentNames = fragmentNames.concat([`${reactPragma}.${fragmentPragma}`]);

jsxElements.forEach(node => {
const openingEl = node.openingElement;
const elName = elementType(openingEl);

if (possibleFragmentNames.indexOf(elName) !== -1 || refersToReactFragment(elName)) {
if (fragmentNames.indexOf(elName) !== -1 || refersToReactFragment(elName)) {
if (reportOnReactVersion(node)) {
return;
}

const attrs = openingEl.attributes;
if (configuration === 'always' && !(attrs && attrs.length > 0)) {
if (configuration === 'syntax' && !(attrs && attrs.length > 0)) {
context.report({
node,
message: `Prefer fragment shorthand over ${reactPragma}.${fragmentPragma}`,
Expand Down
26 changes: 13 additions & 13 deletions tests/lib/rules/jsx-fragments.js
Expand Up @@ -47,39 +47,39 @@ ruleTester.run('jsx-fragments', rule, {
settings
}, {
code: '<Act.Frag><Foo /></Act.Frag>',
options: ['never'],
options: ['element'],
settings
}, {
code: `
import Act, { Frag as F } from 'react';
<F><Foo /></F>;
`,
options: ['never'],
options: ['element'],
settings
}, {
code: `
const F = Act.Frag;
<F><Foo /></F>;
`,
options: ['never'],
options: ['element'],
settings
}, {
code: `
const { Frag } = Act;
<Frag><Foo /></Frag>;
`,
options: ['never'],
options: ['element'],
settings
}, {
code: `
const { Frag } = require('react');
<Frag><Foo /></Frag>;
`,
options: ['never'],
options: ['element'],
settings
}, {
code: '<Act.Frag key="key"><Foo /></Act.Frag>',
options: ['always'],
options: ['syntax'],
settings
}],

Expand All @@ -99,15 +99,15 @@ ruleTester.run('jsx-fragments', rule, {
}, {
code: '<><Foo /></>',
parser: 'babel-eslint',
options: ['never'],
options: ['element'],
settings,
errors: [{
message: 'Prefer Act.Frag over fragment shorthand'
}],
output: '<Act.Frag><Foo /></Act.Frag>'
}, {
code: '<Act.Frag><Foo /></Act.Frag>',
options: ['always'],
options: ['syntax'],
settings,
errors: [{
message: 'Prefer fragment shorthand over Act.Frag'
Expand All @@ -118,7 +118,7 @@ ruleTester.run('jsx-fragments', rule, {
import Act, { Frag as F } from 'react';
<F><Foo /></F>;
`,
options: ['always'],
options: ['syntax'],
settings,
errors: [{
message: 'Prefer fragment shorthand over Act.Frag'
Expand All @@ -132,7 +132,7 @@ ruleTester.run('jsx-fragments', rule, {
import Act, { Frag } from 'react';
<Frag><Foo /></Frag>;
`,
options: ['always'],
options: ['syntax'],
settings,
errors: [{
message: 'Prefer fragment shorthand over Act.Frag'
Expand All @@ -146,7 +146,7 @@ ruleTester.run('jsx-fragments', rule, {
const F = Act.Frag;
<F><Foo /></F>;
`,
options: ['always'],
options: ['syntax'],
settings,
errors: [{
message: 'Prefer fragment shorthand over Act.Frag'
Expand All @@ -160,7 +160,7 @@ ruleTester.run('jsx-fragments', rule, {
const { Frag } = Act;
<Frag><Foo /></Frag>;
`,
options: ['always'],
options: ['syntax'],
settings,
errors: [{
message: 'Prefer fragment shorthand over Act.Frag'
Expand All @@ -174,7 +174,7 @@ ruleTester.run('jsx-fragments', rule, {
const { Frag } = require('react');
<Frag><Foo /></Frag>;
`,
options: ['always'],
options: ['syntax'],
settings,
errors: [{
message: 'Prefer fragment shorthand over Act.Frag'
Expand Down

0 comments on commit 45019af

Please sign in to comment.