Skip to content

Commit

Permalink
fix(jsx): Prevent a crash in multiline prop handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
David Mitchell committed Feb 22, 2019
1 parent a66d8f6 commit 2b12b0f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
15 changes: 15 additions & 0 deletions transforms/__tests__/suppress-eslint-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,21 @@ test("doesn't crash on unusual markup", () => {
expect(modifySource(program)).toBe(program);
});

// This is a somewhat more common case that crashed when I first encountered it.
// Unfortunately, it's not legal to put comments between props, so fixing this one
// will be rather tricky.
test("doesn't crash on violations in multiline props", () => {
const program = `export function Component({ a, b }) {
return (
<div
prop={a == b ? a : b}>
</div>
);
}`;

expect(modifySource(program)).toBe(program);
});

test('supports alternative messages in javascript', () => {
const program = `export function foo(a, b) {
return a == b;
Expand Down
5 changes: 5 additions & 0 deletions transforms/suppress-eslint-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ function addDisableComment(filePath, api, commentText, targetLine, ruleId, path)
}

if (targetPath.parent && targetPath.parent.node.type.substr(0, 3) === 'JSX') {
if (!targetPath.parent.value.children) {
api.report(`Skipping suppression of violation of ${ruleId} on ${targetLine} of ${filePath}`);
return;
}

let siblingIndex = targetPath.parent.value.children.indexOf(targetPath.value) - 1;

while (siblingIndex >= 0) {
Expand Down

0 comments on commit 2b12b0f

Please sign in to comment.