Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assertNoDuplicates throw with more context #10419

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/babel-core/src/config/config-descriptors.js
Expand Up @@ -345,6 +345,7 @@ function assertNoDuplicates(items: Array<UnloadedDescriptor>): void {
}

if (nameMap.has(item.name)) {
const conflicts = items.filter(i => i.value === item.value);
throw new Error(
[
`Duplicate plugin/preset detected.`,
Expand All @@ -355,6 +356,9 @@ function assertNoDuplicates(items: Array<UnloadedDescriptor>): void {
` ['some-plugin', {}],`,
` ['some-plugin', {}, 'some unique name'],`,
` ]`,
``,
`Duplicates detected are:`,
`${JSON.stringify(conflicts, null, 2)}`,
].join("\n"),
);
}
Expand Down
13 changes: 9 additions & 4 deletions packages/babel-core/test/option-manager.js
Expand Up @@ -27,14 +27,19 @@ describe("option-manager", () => {
return { plugin, calls };
}

it("should throw if a plugin is repeated", () => {
const { calls, plugin } = makePlugin();
it("should throw if a plugin is repeated, with information about the repeated plugin", () => {
const { calls, plugin } = makePlugin("my-plugin");

expect(() => {
loadOptions({
plugins: [plugin, plugin],
plugins: [
[plugin, undefined, "my-plugin"],
[plugin, undefined, "my-plugin"],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does it throw if you don't specify the my-plugin name here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolo-ribaudo the example output in the pr is a case where no name is specified. Because I'm serializing it with JSON.stringify you don't see the name key in the output at all.

The output for this test is:

      Duplicate plugin/preset detected.
      If you'd like to use two separate instances of a plugin,
      they need separate names, e.g.

        plugins: [
          ['some-plugin', {}],
          ['some-plugin', {}, 'some unique name'],
        ]

      Duplicates detected are:
      [
        {
          "name": "my-plugin",
          "alias": "base$0",
          "dirname": "/Users/dhamilto/src/babel/babel/packages/babel-core/test",
          "ownPass": false
        },
        {
          "name": "my-plugin",
          "alias": "base$1",
          "dirname": "/Users/dhamilto/src/babel/babel/packages/babel-core/test",
          "ownPass": false
        }
      ]

With the name omitted, the output becomes:

      Duplicate plugin/preset detected.
      If you'd like to use two separate instances of a plugin,
      they need separate names, e.g.

        plugins: [
          ['some-plugin', {}],
          ['some-plugin', {}, 'some unique name'],
        ]

      Duplicates detected are:
      [
        {
          "alias": "base$0",
          "dirname": "/Users/dhamilto/src/babel/babel/packages/babel-core/test",
          "ownPass": false
        },
        {
          "alias": "base$1",
          "dirname": "/Users/dhamilto/src/babel/babel/packages/babel-core/test",
          "ownPass": false
        }
      ]

],
});
}).toThrow(/Duplicate plugin\/preset detected/);
}).toThrow(
/Duplicate plugin\/preset detected.*Duplicates detected are.*my-plugin.*my-plugin/ms,
);
expect(calls).toEqual([]);
});

Expand Down