Skip to content

Commit 4ce369f

Browse files
committed
fix: @putout/operator-jsx: removeClassName: middle
1 parent 703f1b8 commit 4ce369f

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

packages/operator-jsx/lib/jsx.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,13 @@ function removeAttributeValue(path, name, attributeValue) {
9191

9292
const {value} = classAttribute.value;
9393

94-
if (value.includes(attributeValue))
95-
setLiteralValue(classAttribute.value, value.replace(RegExp(`\\s?${attributeValue}\\s?`), ''));
94+
if (!value.includes(attributeValue))
95+
return;
96+
97+
const newValue = value
98+
.replace(RegExp(`\\s?${attributeValue}`), '')
99+
.trim();
100+
setLiteralValue(classAttribute.value, newValue);
96101
}
97102

98103
module.exports.setAttributeValue = (path, name, value) => {

packages/operator-jsx/lib/jsx.spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,17 @@ test('putout: operator: jsx: removeAttributeValue', (t) => {
187187
t.end();
188188
});
189189

190+
test('putout: operator: jsx: removeAttributeValue: no', (t) => {
191+
const node = template.ast.fresh('<hello className="hello world"/>');
192+
removeAttributeValue(node, 'className', 'abc');
193+
194+
const result = print(node);
195+
const expected = `<hello className="hello world"/>;\n`;
196+
197+
t.equal(result, expected);
198+
t.end();
199+
});
200+
190201
test('putout: operator: jsx: setAttributeValue', (t) => {
191202
const node = template.ast.fresh('<hello className="hello"/>');
192203
setAttributeValue(node, 'className', 'world');
@@ -250,6 +261,17 @@ test('putout: operator: jsx: removeClassName: first', (t) => {
250261
t.end();
251262
});
252263

264+
test('putout: operator: jsx: removeClassName: middle', (t) => {
265+
const node = template.ast.fresh('<hello className="hello world abc"/>');
266+
removeClassName(node, 'world');
267+
268+
const result = print(node);
269+
const expected = `<hello className="hello abc"/>;\n`;
270+
271+
t.equal(result, expected);
272+
t.end();
273+
});
274+
253275
test('putout: operator: jsx: containsClassName', (t) => {
254276
const node = template.ast.fresh('<hello className="hello world"/>');
255277
const result = containsClassName(node, 'world');

0 commit comments

Comments
 (0)