Skip to content

Commit 2b12b0f

Browse files
author
David Mitchell
committed
fix(jsx): Prevent a crash in multiline prop handling.
1 parent a66d8f6 commit 2b12b0f

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

transforms/__tests__/suppress-eslint-errors.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,21 @@ test("doesn't crash on unusual markup", () => {
9797
expect(modifySource(program)).toBe(program);
9898
});
9999

100+
// This is a somewhat more common case that crashed when I first encountered it.
101+
// Unfortunately, it's not legal to put comments between props, so fixing this one
102+
// will be rather tricky.
103+
test("doesn't crash on violations in multiline props", () => {
104+
const program = `export function Component({ a, b }) {
105+
return (
106+
<div
107+
prop={a == b ? a : b}>
108+
</div>
109+
);
110+
}`;
111+
112+
expect(modifySource(program)).toBe(program);
113+
});
114+
100115
test('supports alternative messages in javascript', () => {
101116
const program = `export function foo(a, b) {
102117
return a == b;

transforms/suppress-eslint-errors.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ function addDisableComment(filePath, api, commentText, targetLine, ruleId, path)
7777
}
7878

7979
if (targetPath.parent && targetPath.parent.node.type.substr(0, 3) === 'JSX') {
80+
if (!targetPath.parent.value.children) {
81+
api.report(`Skipping suppression of violation of ${ruleId} on ${targetLine} of ${filePath}`);
82+
return;
83+
}
84+
8085
let siblingIndex = targetPath.parent.value.children.indexOf(targetPath.value) - 1;
8186

8287
while (siblingIndex >= 0) {

0 commit comments

Comments
 (0)