Skip to content

Commit

Permalink
prevent to_proc transform when in a hash with key :if or :unless
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-hunter-pc committed Jul 3, 2020
1 parent 354617d commit e13928b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/toProc.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ const toProc = (path, node) => {
return null;
}

if (key.type === "symbol_literal" && ["if", "unless"].includes(key.body[0].body[0].body)) {
if (
key.type === "symbol_literal" &&
["if", "unless"].includes(key.body[0].body[0].body)
) {
return null;
}
}
Expand Down
18 changes: 18 additions & 0 deletions test/js/blocks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,5 +213,23 @@ describe("blocks", () => {
expect("foo[:bar].each { |baz| baz.to_s }").toChangeFormat(
"foo[:bar].each(&:to_s)"
));

describe.each(["if", "unless"])(
"does not transform when used inside hash with %s",
(keyword) => {
test(`hash literal with :${keyword} key`, () =>
expect(`{ ${keyword}: ->(foo) { foo.to_s } }`).toMatchFormat());
test(`hash literal with hashrocket :${keyword} key`, () =>
expect(`{ :${keyword} => ->(foo) { foo.to_s } }`).toMatchFormat({
preferHashLabels: false
}));
test(`method arguments with :${keyword} key`, () =>
expect(`bar ${keyword}: ->(foo) { foo.to_s }`).toMatchFormat());
test(`method arguments with hashrocket :${keyword} key`, () =>
expect(`bar :${keyword} => ->(foo) { foo.to_s }`).toMatchFormat({
preferHashLabels: false
}));
}
);
});
});

0 comments on commit e13928b

Please sign in to comment.