Skip to content

Commit

Permalink
Fix failures
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Aug 16, 2022
1 parent 5e02340 commit 942c580
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
23 changes: 15 additions & 8 deletions packages/babel-parser/src/plugin-utils.ts
@@ -1,5 +1,9 @@
import type Parser from "./parser";
import type { PluginConfig } from "./typings";
import type {
PluginConfig,
ParserPluginWithOptions,
PluginOptions,
} from "./typings";

export type Plugin = PluginConfig;

Expand Down Expand Up @@ -47,11 +51,14 @@ export function hasPlugin(
});
}

export function getPluginOption(
export function getPluginOption<
PluginName extends ParserPluginWithOptions[0],
OptionName extends keyof PluginOptions<PluginName>,
>(
plugins: PluginList,
name: string,
option: string,
) {
name: PluginName,
option: OptionName,
): PluginOptions<PluginName>[OptionName] | null {
const plugin = plugins.find(plugin => {
if (Array.isArray(plugin)) {
return plugin[0] === name;
Expand All @@ -60,9 +67,8 @@ export function getPluginOption(
}
});

if (plugin && Array.isArray(plugin)) {
// @ts-expect-error Fixme: should check whether option is defined
return plugin[1][option];
if (plugin && Array.isArray(plugin) && plugin.length >= 2) {
return (plugin[1] as PluginOptions<PluginName>)[option];
}

return null;
Expand Down Expand Up @@ -168,6 +174,7 @@ export function validatePlugins(plugins: PluginList) {
}
const moduleAttributesVersionPluginOption = getPluginOption(
plugins,
// @ts-expect-error TODO: moduleAttributes's type definitions don't have options
"moduleAttributes",
"version",
);
Expand Down
3 changes: 3 additions & 0 deletions packages/babel-parser/src/typings.ts
Expand Up @@ -53,6 +53,9 @@ export type ParserPluginWithOptions =
| ["flow", FlowPluginOptions]
| ["typescript", TypeScriptPluginOptions];

export type PluginOptions<PluginName extends ParserPluginWithOptions[0]> =
Extract<ParserPluginWithOptions, [PluginName, any]>[1];

export interface DecoratorsPluginOptions {
decoratorsBeforeExport?: boolean;
}
Expand Down

0 comments on commit 942c580

Please sign in to comment.