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

[parser] Make decoratorsBeforeExport default to false #14695

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
35 changes: 19 additions & 16 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 All @@ -85,14 +91,10 @@ export function validatePlugins(plugins: PluginList) {
"decorators",
"decoratorsBeforeExport",
);
if (decoratorsBeforeExport == null) {
throw new Error(
"The 'decorators' plugin requires a 'decoratorsBeforeExport' option," +
" whose value must be a boolean. If you are migrating from" +
" Babylon/Babel 6 or want to use the old decorators proposal, you" +
" should use the 'decorators-legacy' plugin instead of 'decorators'.",
);
} else if (typeof decoratorsBeforeExport !== "boolean") {
if (
decoratorsBeforeExport != null &&
typeof decoratorsBeforeExport !== "boolean"
) {
throw new Error("'decoratorsBeforeExport' must be a boolean.");
}
}
Expand Down Expand Up @@ -172,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
@@ -1,3 +1,3 @@
{
"plugins": [["decorators", { "decoratorsBeforeExport": false }]]
"plugins": ["decorators"]
}
@@ -1,3 +1,3 @@
{
"plugins": [["decorators", { "decoratorsBeforeExport": false }]]
"plugins": ["decorators"]
}
@@ -1,3 +1,3 @@
{
"plugins": [["decorators", { "decoratorsBeforeExport": false }]]
"plugins": ["decorators"]
}

This file was deleted.

@@ -1,10 +1,3 @@
{
"plugins": [
[
"decorators",
{
"decoratorsBeforeExport": false
}
]
]
"plugins": ["decorators"]
}
@@ -1,11 +1,4 @@
{
"plugins": [
[
"decorators",
{
"decoratorsBeforeExport": false
}
]
],
"plugins": ["decorators"],
"throws": "Decorators must not be followed by a semicolon. (2:5)"
}
}
@@ -1,3 +1,3 @@
{
"plugins": [["decorators", { "decoratorsBeforeExport": false }]]
"plugins": ["decorators"]
}
@@ -1,4 +1,4 @@
{
"plugins": [["decorators", { "decoratorsBeforeExport": false }]],
"plugins": ["decorators"],
"createParenthesizedExpressions": true
}
@@ -1,3 +1,3 @@
{
"plugins": [["decorators", { "decoratorsBeforeExport": false }]]
"plugins": ["decorators"]
}
@@ -1,11 +1,4 @@
{
"plugins": [
[
"decorators",
{
"decoratorsBeforeExport": false
}
]
],
"plugins": ["decorators"],
"throws": "Leading decorators must be attached to a class declaration. (1:6)"
}
}
@@ -1,11 +1,4 @@
{
"plugins": [
[
"decorators",
{
"decoratorsBeforeExport": false
}
]
],
"plugins": ["decorators"],
"throws": "Leading decorators must be attached to a class declaration. (1:6)"
}
}
@@ -1,6 +1,6 @@
{
"plugins": [
["pipelineOperator", { "proposal": "hack", "topicToken": "@@" }],
["decorators", { "decoratorsBeforeExport": false }]
"decorators"
]
}
@@ -1,6 +1,6 @@
{
"plugins": [
["pipelineOperator", { "proposal": "hack", "topicToken": "@@" }],
["decorators", { "decoratorsBeforeExport": false }]
"decorators"
]
}
@@ -1,6 +1,6 @@
{
"plugins": [
["pipelineOperator", { "proposal": "hack", "topicToken": "@@" }],
["decorators", { "decoratorsBeforeExport": false }]
"decorators"
]
}
@@ -1,6 +1,6 @@
{
"plugins": [
["pipelineOperator", { "proposal": "hack", "topicToken": "^^" }],
["decorators", { "decoratorsBeforeExport": false }]
"decorators"
]
}
@@ -1,6 +1,6 @@
{
"plugins": [
["pipelineOperator", { "proposal": "hack", "topicToken": "^^" }],
["decorators", { "decoratorsBeforeExport": false }]
"decorators"
]
}
@@ -1,6 +1,6 @@
{
"plugins": [
["pipelineOperator", { "proposal": "hack", "topicToken": "^^" }],
["decorators", { "decoratorsBeforeExport": false }]
"decorators"
]
}
@@ -1,4 +1,4 @@
{
"sourceType": "module",
"plugins": ["flow", ["decorators", { "decoratorsBeforeExport": false }]]
"plugins": ["flow", "decorators"]
}
@@ -1,5 +1,5 @@
{
"sourceType": "module",
"plugins": ["flow", ["decorators", { "decoratorsBeforeExport": false }]],
"plugins": ["flow", "decorators"],
"throws": "Unexpected token (2:10)"
}