Skip to content

Commit

Permalink
Add option to react/jsx-key rule
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Feb 20, 2021
1 parent 84add52 commit 261274b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ module.exports = {
'react/jsx-key': [
'error',
{
checkFragmentShorthand: true
checkFragmentShorthand: true,
checkKeyMustBeforeSpread: true
}
],
'react/jsx-max-props-per-line': [
Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"node": ">=10"
},
"scripts": {
"//": "TODO: Fix tests",
"test": "ava"
},
"files": [
Expand Down Expand Up @@ -53,15 +54,15 @@
"devDependencies": {
"ava": "^2.1.0",
"eslint": "^7.1.0",
"eslint-plugin-react": "^7.20.0",
"eslint-plugin-react": "^7.22.0",
"eslint-plugin-react-hooks": "^4.0.4",
"is-plain-obj": "^2.1.0",
"react": "^16.13.1",
"is-plain-obj": "^3.0.0",
"react": "^17.0.1",
"temp-write": "^4.0.0"
},
"peerDependencies": {
"eslint": ">=7",
"eslint-plugin-react": ">=7.20.0",
"eslint-plugin-react-hooks": ">=4.0.4"
"eslint-plugin-react": ">=7.22.0",
"eslint-plugin-react-hooks": ">=4.2.0"
}
}
28 changes: 14 additions & 14 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,40 @@ import isPlainObj from 'is-plain-obj';
import tempWrite from 'temp-write';
import eslint from 'eslint';

const hasRule = (errors, ruleId) => errors.some(x => x.ruleId === ruleId);
const hasRule = (errors, ruleId) => errors.some(error => error.ruleId === ruleId);

function runEslint(str, conf) {
function runEslint(string, config) {
const linter = new eslint.CLIEngine({
useEslintrc: false,
configFile: tempWrite.sync(JSON.stringify(conf))
configFile: tempWrite.sync(JSON.stringify(config))
});

return linter.executeOnText(str).results[0].messages;
return linter.executeOnText(string).results[0].messages;
}

test('main', t => {
const conf = require('../space');
const config = require('../space');

t.true(isPlainObj(conf));
t.true(isPlainObj(conf.rules));
t.true(isPlainObj(config));
t.true(isPlainObj(config.rules));

const errors = runEslint('var app = <div className="foo">Unicorn</div>', conf);
const errors = runEslint('var app = <div className="foo">Unicorn</div>', config);
t.true(hasRule(errors, 'react/react-in-jsx-scope'));
});

test('space', t => {
const conf = require('../space');
const config = require('../space');

t.true(isPlainObj(conf));
t.true(isPlainObj(conf.rules));
t.true(isPlainObj(config));
t.true(isPlainObj(config.rules));

const errors = runEslint('<App>\n\t<Hello/>\n</App>', conf);
const errors = runEslint('<App>\n\t<Hello/>\n</App>', config);
t.true(hasRule(errors, 'react/jsx-indent'));
});

test('no errors', t => {
const conf = require('..');
const config = require('..');

const errors = runEslint('var React = require(\'react\');\nvar el = <div/>;', conf);
const errors = runEslint('var React = require(\'react\');\nvar el = <div/>;', config);
t.deepEqual(errors, []);
});

0 comments on commit 261274b

Please sign in to comment.