Skip to content

Commit

Permalink
fix: test overrides for filename compulsory
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Jul 9, 2019
1 parent 0a5d100 commit 056e197
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
21 changes: 17 additions & 4 deletions packages/babel-core/src/config/full.js
Expand Up @@ -18,6 +18,7 @@ import { validatePluginObject } from "./validation/plugins";
import makeAPI from "./helpers/config-api";

import loadPrivatePartialConfig from "./partial";
import type { ValidatedOptions } from "./validation/options";

type LoadedDescriptor = {
value: {},
Expand Down Expand Up @@ -278,17 +279,29 @@ const instantiatePlugin = makeWeakCache(
},
);

const validateIfOptionNeedsFilename = (
options: ValidatedOptions,
descriptor: UnloadedDescriptor,
): void => {
if (options.test || options.include || options.exclude) {
throw new Error(
`Preset ${descriptor.name ||
""} requires filename, but it was not passed.`.replace(/\s{2}/, " "),
);
}
};

const validatePreset = (
preset: PresetInstance,
context: ConfigContext,
descriptor: UnloadedDescriptor,
): void => {
if (!context.filename) {
const { options } = preset;
if (options.test || options.include || options.exclude) {
throw new Error(
`Preset ${descriptor.name ||
""} requires filename, but it was not passed.`.replace(/\s{2}/, " "),
validateIfOptionNeedsFilename(options, descriptor);
if (options.overrides) {
options.overrides.forEach(overrideOptions =>
validateIfOptionNeedsFilename(overrideOptions, descriptor),
);
}
}
Expand Down
8 changes: 8 additions & 0 deletions packages/babel-core/test/config-chain.js
Expand Up @@ -1063,5 +1063,13 @@ describe("buildConfigChain", function() {
});
}).toThrow(/Preset requires filename/);
});

it("should throw when `preset.overrides` requires `filename` but it was not passed", () => {
expect(() => {
loadOptions({
presets: [require("./fixtures/config-loading/preset5")],
});
}).toThrow(/Preset requires filename/);
});
});
});
8 changes: 8 additions & 0 deletions packages/babel-core/test/fixtures/config-loading/preset5.js
@@ -0,0 +1,8 @@
module.exports = function() {
return {
overrides: [{
test: /\.ts$/,
plugins: []
}]
}
};

0 comments on commit 056e197

Please sign in to comment.