Skip to content

Commit e732d78

Browse files
fix: Preserve significant leading whitespace in jsx text nodes.
1 parent 0cc01b2 commit e732d78

File tree

2 files changed

+54
-9
lines changed

2 files changed

+54
-9
lines changed

transforms/__tests__/suppress-eslint-errors.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ test('handles trailing text on the previous line', () => {
357357
}`);
358358
});
359359

360-
test('preserves significant whitespace in jsx text nodes', () => {
360+
test('preserves significant trailing whitespace in jsx text nodes', () => {
361361
const program = `export function Component({ a, b }) {
362362
return (
363363
<div>
@@ -379,6 +379,28 @@ test('preserves significant whitespace in jsx text nodes', () => {
379379
}`);
380380
});
381381

382+
test('preserves significant leading whitespace in jsx text nodes', () => {
383+
const program = `export function Component({ a, b }) {
384+
return (
385+
<div>
386+
<span>A span</span> next to some text
387+
<span onClick={() => a == b}>hi</span>.
388+
</div>
389+
);
390+
}`;
391+
392+
expect(modifySource(program)).toBe(`export function Component({ a, b }) {
393+
return (
394+
(<div>
395+
<span>A span</span> next to some text
396+
{/* TODO: Fix this the next time the file is edited. */}
397+
{/* eslint-disable-next-line eqeqeq */}
398+
<span onClick={() => a == b}>hi</span>.
399+
</div>)
400+
);
401+
}`);
402+
});
403+
382404
const defaultPath = path.resolve(__dirname, 'examples', 'index.js');
383405
function modifySource(source, options) {
384406
const transformOptions = { ...options };

transforms/suppress-eslint-errors.js

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,20 +120,43 @@ function addDisableComment(filePath, api, commentText, targetLine, ruleId, path)
120120
continue;
121121
}
122122

123-
const segments = sibling.value.split('\n').flatMap((line) => {
123+
const lines = sibling.value.split('\n');
124+
const segments = lines.flatMap((line, lineIndex) => {
125+
const result = [];
126+
124127
const trimmedLine = line.trimEnd();
125-
if (trimmedLine.length === 0) {
126-
return ['\n'];
128+
if (trimmedLine.length !== 0) {
129+
if (lineIndex === 0) {
130+
const startTrimmedLine = trimmedLine.trimStart();
131+
if (startTrimmedLine.length === line.length) {
132+
result.push(line);
133+
} else {
134+
if (startTrimmedLine.length < trimmedLine.length) {
135+
result.push(trimmedLine.substr(0, trimmedLine.length - startTrimmedLine.length));
136+
}
137+
138+
result.push(startTrimmedLine);
139+
140+
if (trimmedLine.length < line.length) {
141+
result.push(line.substr(trimmedLine.length));
142+
}
143+
}
144+
} else {
145+
if (trimmedLine.length === line.length) {
146+
result.push(line);
147+
} else {
148+
result.push(trimmedLine, line.substr(trimmedLine.length));
149+
}
150+
}
127151
}
128-
if (trimmedLine.length === line.length) {
129-
return [line, '\n'];
152+
153+
if (lineIndex != lines.length - 1) {
154+
result.push('\n');
130155
}
131156

132-
return [trimmedLine, line.substr(trimmedLine.length), '\n'];
157+
return result;
133158
});
134159

135-
segments.pop();
136-
137160
children.splice(siblingIndex, 1, ...segments.map((segment) => api.j.jsxText(segment)));
138161
}
139162

0 commit comments

Comments
 (0)