Skip to content

Commit

Permalink
fix(resolve-map): fix #97, update to consider trailing comma & whites…
Browse files Browse the repository at this point in the history
…pace

- add more tests
  • Loading branch information
postspectacular committed Jul 8, 2019
1 parent 5c53dcc commit de9532b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/resolve-map/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ const resolveFunction = (
let res;
if (match) {
const args = match[2]
.replace(/\s|,$/g, "")
// remove white space and trailing comma
.replace(/\s|(,\s*$)/g, "")
.split(/,/g)
.map((k) => k.split(":")[0])
.reduce((acc: any, k) => ((acc[k] = resolve(k)), acc), {});
Expand Down
28 changes: 27 additions & 1 deletion packages/resolve-map/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,38 @@ describe("resolve-map", () => {
);
});

it("destructures w/ local renames", () => {
assert.deepEqual(resolve({ a: 1, b: ({ a: aa }: any) => aa }), {
a: 1,
b: 1
});
});

it("destructures w/ trailing comma", () => {
assert.deepEqual(
// since prettier is running over this file
// build function dynamically to force trailing comma
resolve({ a: 1, b: 2, c: new Function("{a,b,}", "return a + b") }),
{ a: 1, b: 2, c: 3 }
{ a: 1, b: 2, c: 3 },
"comma only"
);
assert.deepEqual(
resolve({
a: 1,
b: 2,
c: new Function("{ a, b, }", "return a + b")
}),
{ a: 1, b: 2, c: 3 },
"comma & whitespaces"
);
assert.deepEqual(
resolve({
a: 1,
b: 2,
c: new Function("{ a, b: bb, }", "return a + bb")
}),
{ a: 1, b: 2, c: 3 },
"comma & whitespaces & rename"
);
});
});

0 comments on commit de9532b

Please sign in to comment.