From 4ec7fb15d41ee14baca866a5a7120fefcac7530e Mon Sep 17 00:00:00 2001 From: "Robin (Robert) Thomas" Date: Tue, 3 Sep 2024 10:53:44 -0500 Subject: [PATCH] feat: adds disallowInAssigment to no-extra-spacing-attrs (#216) * Add disallowInAssignment * prettier --- docs/rules/no-extra-spacing-attrs.md | 24 ++++++++ .../lib/rules/no-extra-spacing-attrs.js | 34 ++++++++++- .../rules/no-extra-spacing-attrs.test.js | 57 +++++++++++++++++++ 3 files changed, 113 insertions(+), 2 deletions(-) diff --git a/docs/rules/no-extra-spacing-attrs.md b/docs/rules/no-extra-spacing-attrs.md index 333fa91d..ea3fb1f7 100644 --- a/docs/rules/no-extra-spacing-attrs.md +++ b/docs/rules/no-extra-spacing-attrs.md @@ -112,3 +112,27 @@ Example(s) of **correct** code for this rule with the `{ "disallowTabs": true }` ``` + +- `disallowInAssignment` (default: false): Disallows spaces around the attribute assignment operator `=` + +Example(s) of **incorrect** code for this rule with the `{ "disallowInAssignment": true }` option: + + + +```html +
+
+``` + + + +Example(s) of **correct** code for this rule with the `{ "disallowInAssignment": true }` option: + + + +```html +
+
+``` + + diff --git a/packages/eslint-plugin/lib/rules/no-extra-spacing-attrs.js b/packages/eslint-plugin/lib/rules/no-extra-spacing-attrs.js index e98f89ed..de70773c 100644 --- a/packages/eslint-plugin/lib/rules/no-extra-spacing-attrs.js +++ b/packages/eslint-plugin/lib/rules/no-extra-spacing-attrs.js @@ -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", @@ -47,6 +48,9 @@ module.exports = { { type: "object", properties: { + disallowInAssignment: { + type: "boolean", + }, disallowMissing: { type: "boolean", }, @@ -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]: @@ -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; @@ -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) { @@ -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]], ` ` ); }, @@ -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) { diff --git a/packages/eslint-plugin/tests/rules/no-extra-spacing-attrs.test.js b/packages/eslint-plugin/tests/rules/no-extra-spacing-attrs.test.js index 31ca1545..252b00e0 100644 --- a/packages/eslint-plugin/tests/rules/no-extra-spacing-attrs.test.js +++ b/packages/eslint-plugin/tests/rules/no-extra-spacing-attrs.test.js @@ -18,6 +18,15 @@ ruleTester.run("no-extra-spacing-attrs", rule, { code: ` +
+ + + `, + }, + { + code: ` + +
@@ -515,5 +524,53 @@ ruleTester.run("no-extra-spacing-attrs", rule, { }, ], }, + + { + code: ` +\t
+\t\t +\t
+`, + output: ` +\t
+\t\tname +\t
+`, + options: [ + { + disallowInAssignment: true, + disallowTabs: true, + }, + ], + errors: [ + { + messageId: "unexpectedBefore", + }, + { + messageId: "unexpectedInAssignment", + }, + { + messageId: "unexpectedBetween", + }, + { + messageId: "unexpectedBetween", + }, + { + messageId: "unexpectedInAssignment", + }, + { + messageId: "unexpectedTabBefore", + }, + { + messageId: "unexpectedInAssignment", + }, + { + messageId: "unexpectedTabBetween", + }, + { + messageId: "unexpectedInAssignment", + }, + ], + }, ], });