Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rule react/jsx-curly-spacing #37

Merged
merged 14 commits into from
Jan 17, 2024
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const moduleConfig = {
node: true,
...(hasES2022Support ? { es2022: true } : { es6: true }),
},
plugins: [ "eslint-plugin-n", "import" ],
plugins: [ "eslint-plugin-n", "import", "eslint-plugin-react" ],
linus marked this conversation as resolved.
Show resolved Hide resolved
};

const commonjsConfig = {
Expand All @@ -28,7 +28,7 @@ const commonjsConfig = {
node: true,
es6: true,
},
plugins: [ "eslint-plugin-n" ],
plugins: [ "eslint-plugin-n", "eslint-plugin-react" ],
};

module.exports = {
Expand Down
1 change: 1 addition & 0 deletions rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ module.exports = function getRules(isModuleProject) {
"require-await": "error", // make it explicit
"object-shorthand": [ "error", "properties" ], // make it short
"object-curly-spacing": [ "error", "always" ], // consistency
"react/jsx-curly-spacing": [ "error", { when: "never", children: { when: "always" } } ],
"array-bracket-spacing": [ "error", "always" ], // consistency with above
"new-cap": "error", // consistency with test rules
"switch-colon-spacing": "error",
Expand Down
20 changes: 20 additions & 0 deletions test/data/jsx/curly-braces.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as Preact from "preact";

export function Item() {
return (
<div>
<h1>Data</h1>
<SubItem input={{name: "Anders", role: "Developer"}} />
<SubItem input={ { name: "Anders", role: "Developer" } } />
</div>
);
}

function SubItem({ input }) {
return (
<div>
<h2>{input.name}</h2>
<h3>{input.role}</h3>
</div>
);
}
19 changes: 19 additions & 0 deletions test/data/jsx/noerrors.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as Preact from "preact";

export function Item() {
return (
<div>
<h1>Data</h1>
<SubItem input={{ name: "Anders", role: "Developer" }} />
</div>
);
}

function SubItem({ input }) {
return (
<div>
<h2>{ input.name }</h2>
<h3>{ input.role }</h3>
</div>
);
}
62 changes: 58 additions & 4 deletions test/feature/lint-jsx-feature.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,70 @@ Feature("linting jsx files", () => {
});

let results;
When("we lint a folder", async () => {
results = await eslint.lintFiles([ "test/data/jsx/*.jsx" ]);
When("we lint two valid jsx files", async () => {
results = await eslint.lintFiles([ "test/data/jsx/noerrors.jsx", "test/data/jsx/preact-module.jsx" ]);
});

Then("we should have linted the correct number of files", () => {
expect(results.length).to.eql(1);
expect(results.length).to.eql(2);
});

And("one file should have no messages", () => {
And("the lint result should have no messages", () => {
expect(results[0].messages.length).to.eql(0, JSON.stringify(results[0].messages, null, 2));
expect(results[1].messages.length).to.eql(0, JSON.stringify(results[0].messages, null, 2));
});
});

Scenario("jsx file not passing, invalid curly braces", () => {
let eslint;
Given("we have an eslint instance with the base config", () => {
eslint = new ESLint({
overrideConfigFile: "react.js",
overrideConfig: {
parserOptions: {
ecmaVersion: 2022,
sourceType: "module",
},
settings: {
react: {
pragma: "Preact",
version: "16.0",
},
},
},
useEslintrc: false,
ignore: false,
});
});

let result;
When("we lint a jsx file", async () => {
const results = await eslint.lintFiles([ "test/data/jsx/curly-braces.jsx" ]);
result = results[0];
});

Then("the lint result should have eight messages", () => {
expect(result.messages.length).to.eql(8, JSON.stringify(result.messages, null, 2));
});

And("three errors should be 'A space is required before'", () => {
const errors = result.messages.filter(({ message }) => message.includes("A space is required before"));
expect(errors.length).to.eql(3);
});

And("three errors should be 'A space is required after'", () => {
const errors = result.messages.filter(({ message }) => message.includes("A space is required after"));
expect(errors.length).to.eql(3);
});

And("one error should be 'There should be no space before'", () => {
const errors = result.messages.filter(({ message }) => message.includes("There should be no space before"));
expect(errors.length).to.eql(1);
});

And("one error should be 'There should be no space after'", () => {
const errors = result.messages.filter(({ message }) => message.includes("There should be no space after"));
expect(errors.length).to.eql(1);
});
});
});
Loading