Skip to content

Commit

Permalink
feat: adds disallowInAssigment to no-extra-spacing-attrs (#216)
Browse files Browse the repository at this point in the history
* Add disallowInAssignment

* prettier
  • Loading branch information
RobertAKARobin authored Sep 3, 2024
1 parent 8046b6a commit 4ec7fb1
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 2 deletions.
24 changes: 24 additions & 0 deletions docs/rules/no-extra-spacing-attrs.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,27 @@ Example(s) of **correct** code for this rule with the `{ "disallowTabs": true }`
```

<!-- prettier-ignore-end -->

- `disallowInAssignment` (default: false): Disallows spaces around the attribute assignment operator `=`

Example(s) of **incorrect** code for this rule with the `{ "disallowInAssignment": true }` option:

<!-- prettier-ignore-start -->

```html
<div id = "foo" class = "bar">
</div>
```

<!-- prettier-ignore-end -->

Example(s) of **correct** code for this rule with the `{ "disallowInAssignment": true }` option:

<!-- prettier-ignore-start -->

```html
<div id="foo" class="bar">
</div>
```

<!-- prettier-ignore-end -->
34 changes: 32 additions & 2 deletions packages/eslint-plugin/lib/rules/no-extra-spacing-attrs.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const MESSAGE_IDS = {
EXTRA_AFTER: "unexpectedAfter",
EXTRA_BEFORE: "unexpectedBefore",
EXTRA_BEFORE_CLOSE: "unexpectedBeforeClose",
EXTRA_IN_ASSIGNMENT: "unexpectedInAssignment",
MISSING_BEFORE: "missingBefore",
MISSING_BEFORE_SELF_CLOSE: "missingBeforeSelfClose",
EXTRA_BEFORE_SELF_CLOSE: "unexpectedBeforeSelfClose",
Expand All @@ -47,6 +48,9 @@ module.exports = {
{
type: "object",
properties: {
disallowInAssignment: {
type: "boolean",
},
disallowMissing: {
type: "boolean",
},
Expand All @@ -64,6 +68,8 @@ module.exports = {
[MESSAGE_IDS.EXTRA_AFTER]: "Unexpected space after attribute",
[MESSAGE_IDS.EXTRA_BEFORE]: "Unexpected space before attribute",
[MESSAGE_IDS.EXTRA_BEFORE_CLOSE]: "Unexpected space before closing",
[MESSAGE_IDS.EXTRA_IN_ASSIGNMENT]:
"Unexpected space in attribute assignment",
[MESSAGE_IDS.MISSING_BEFORE_SELF_CLOSE]:
"Missing space before self closing",
[MESSAGE_IDS.EXTRA_BEFORE_SELF_CLOSE]:
Expand All @@ -82,6 +88,8 @@ module.exports = {
.enforceBeforeSelfClose;
const disallowMissing = !!(context.options[0] || {}).disallowMissing;
const disallowTabs = !!(context.options[0] || {}).disallowTabs;
const disallowInAssignment = !!(context.options[0] || [])
.disallowInAssignment;

const sourceCode = context.getSourceCode().text;

Expand All @@ -104,7 +112,10 @@ module.exports = {
loc: getLocBetween(current, after),
messageId: MESSAGE_IDS.EXTRA_BETWEEN,
fix(fixer) {
return fixer.removeRange([current.range[1] + 1, after.range[0]]);
return fixer.replaceTextRange(
[current.range[1], after.range[0]],
` `
);
},
});
} else if (disallowMissing && spacesBetween < 1) {
Expand All @@ -122,7 +133,7 @@ module.exports = {
messageId: MESSAGE_IDS.EXTRA_TAB_BETWEEN,
fix(fixer) {
return fixer.replaceTextRange(
[current.range[1], current.range[1] + 1],
[current.range[1], after.range[0]],
` `
);
},
Expand Down Expand Up @@ -184,6 +195,25 @@ module.exports = {

if (node.attributes.length) {
checkExtraSpaceBefore(node.openStart, node.attributes[0]);

for (const attr of node.attributes) {
if (attr.startWrapper && attr.value) {
if (
disallowInAssignment &&
attr.startWrapper.loc.start.column - attr.key.loc.end.column > 1
) {
const start = attr.key.range[1];
const end = attr.startWrapper.range[0];
context.report({
node: attr,
messageId: MESSAGE_IDS.EXTRA_IN_ASSIGNMENT,
fix(fixer) {
return fixer.replaceTextRange([start, end], `=`);
},
});
}
}
}
}

if (node.openEnd) {
Expand Down
57 changes: 57 additions & 0 deletions packages/eslint-plugin/tests/rules/no-extra-spacing-attrs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ ruleTester.run("no-extra-spacing-attrs", rule, {
code: `
<html>
<body>
<div foo = "foo" bar = "bar" class\t=\t"baz"></div>
</body>
</html>
`,
},
{
code: `
<html>
<body>
<div foo="foo" bar="bar"></div>
</body>
</html>
Expand Down Expand Up @@ -515,5 +524,53 @@ ruleTester.run("no-extra-spacing-attrs", rule, {
},
],
},

{
code: `
\t<div class\t= "foo"\t\tdata-custom\t\tid\t\t=\t\t"boo">
\t\t<img\tsrc =\t"foo"\talt= "name"/>
\t</div>
`,
output: `
\t<div class="foo" data-custom id="boo">
\t\t<img src="foo" alt="name"/>
\t</div>
`,
options: [
{
disallowInAssignment: true,
disallowTabs: true,
},
],
errors: [
{
messageId: "unexpectedBefore",
},
{
messageId: "unexpectedInAssignment",
},
{
messageId: "unexpectedBetween",
},
{
messageId: "unexpectedBetween",
},
{
messageId: "unexpectedInAssignment",
},
{
messageId: "unexpectedTabBefore",
},
{
messageId: "unexpectedInAssignment",
},
{
messageId: "unexpectedTabBetween",
},
{
messageId: "unexpectedInAssignment",
},
],
},
],
});

0 comments on commit 4ec7fb1

Please sign in to comment.