Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3ffc707

Browse files
[babel 8] Remove references to minimal/smart pipelines (#17058)
1 parent 4b2608b commit 3ffc707

11 files changed

Lines changed: 96 additions & 68 deletions

File tree

packages/babel-parser/src/parse-error/pipeline-operator-errors.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ export default {
1919
// This error is only used by the smart-mix proposal
2020
PipeBodyIsTighter:
2121
"Unexpected yield after pipeline body; any yield expression acting as Hack-style pipe body must be parenthesized due to its loose operator precedence.",
22-
PipeTopicRequiresHackPipes:
23-
'Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.',
22+
PipeTopicRequiresHackPipes: process.env.BABEL_8_BREAKING
23+
? 'Topic references are only supported when using the `"proposal": "hack"` version of the pipeline proposal.'
24+
: 'Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.',
2425
PipeTopicUnbound:
2526
"Topic reference is unbound; it must be inside a pipe body.",
2627
PipeTopicUnconfiguredToken: ({ token }: { token: string }) =>
@@ -32,20 +33,24 @@ export default {
3233
type,
3334
})}; please wrap it in parentheses.`,
3435

35-
// Messages whose codes start with “Pipeline” or “PrimaryTopic”
36-
// are retained for backwards compatibility
37-
// with the deprecated smart-mix pipe operator proposal plugin.
38-
// They are subject to removal in a future major version.
39-
PipelineBodyNoArrow:
40-
'Unexpected arrow "=>" after pipeline body; arrow function in pipeline body must be parenthesized.',
41-
PipelineBodySequenceExpression:
42-
"Pipeline body may not be a comma-separated sequence expression.",
43-
PipelineHeadSequenceExpression:
44-
"Pipeline head should not be a comma-separated sequence expression.",
45-
PipelineTopicUnused:
46-
"Pipeline is in topic style but does not use topic reference.",
47-
PrimaryTopicNotAllowed:
48-
"Topic reference was used in a lexical context without topic binding.",
49-
PrimaryTopicRequiresSmartPipeline:
50-
'Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.',
36+
...(process.env.BABEL_8_BREAKING
37+
? {}
38+
: {
39+
// Messages whose codes start with “Pipeline” or “PrimaryTopic”
40+
// are retained for backwards compatibility
41+
// with the deprecated smart-mix pipe operator proposal plugin.
42+
// They are subject to removal in a future major version.
43+
PipelineBodyNoArrow:
44+
'Unexpected arrow "=>" after pipeline body; arrow function in pipeline body must be parenthesized.',
45+
PipelineBodySequenceExpression:
46+
"Pipeline body may not be a comma-separated sequence expression.",
47+
PipelineHeadSequenceExpression:
48+
"Pipeline head should not be a comma-separated sequence expression.",
49+
PipelineTopicUnused:
50+
"Pipeline is in topic style but does not use topic reference.",
51+
PrimaryTopicNotAllowed:
52+
"Topic reference was used in a lexical context without topic binding.",
53+
PrimaryTopicRequiresSmartPipeline:
54+
'Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.',
55+
}),
5156
} satisfies ParseErrorTemplates;

packages/babel-parser/src/parser/expression.ts

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -469,8 +469,9 @@ export default abstract class ExpressionParser extends LValParser {
469469
this.next();
470470

471471
if (
472+
!process.env.BABEL_8_BREAKING &&
472473
op === tt.pipeline &&
473-
// @ts-expect-error Remove this in Babel 8
474+
// @ts-expect-error: Only in Babel 7
474475
this.hasPlugin(["pipelineOperator", { proposal: "minimal" }])
475476
) {
476477
if (this.state.type === tt._await && this.prodParam.hasAwait) {
@@ -526,24 +527,28 @@ export default abstract class ExpressionParser extends LValParser {
526527
return this.parseHackPipeBody();
527528
});
528529

529-
// @ts-expect-error Remove this in Babel 8
530-
case "smart":
531-
return this.withTopicBindingContext(() => {
532-
if (this.prodParam.hasYield && this.isContextual(tt._yield)) {
533-
throw this.raise(Errors.PipeBodyIsTighter, this.state.startLoc);
534-
}
535-
return this.parseSmartPipelineBodyInStyle(
536-
this.parseExprOpBaseRightExpr(op, prec),
537-
startLoc,
538-
);
539-
});
540-
541530
case "fsharp":
542531
return this.withSoloAwaitPermittingContext(() => {
543532
return this.parseFSharpPipelineBody(prec);
544533
});
545534
}
546535

536+
if (
537+
!process.env.BABEL_8_BREAKING &&
538+
// @ts-expect-error: Babel 7 only
539+
this.getPluginOption("pipelineOperator", "proposal") === "smart"
540+
) {
541+
return this.withTopicBindingContext(() => {
542+
if (this.prodParam.hasYield && this.isContextual(tt._yield)) {
543+
throw this.raise(Errors.PipeBodyIsTighter, this.state.startLoc);
544+
}
545+
return this.parseSmartPipelineBodyInStyle(
546+
this.parseExprOpBaseRightExpr(op, prec),
547+
startLoc,
548+
);
549+
});
550+
}
551+
547552
// Falls through.
548553
default:
549554
return this.parseExprOpBaseRightExpr(op, prec);
@@ -1402,33 +1407,25 @@ export default abstract class ExpressionParser extends LValParser {
14021407
// The token matches the plugin’s configuration.
14031408
// The token is therefore a topic reference.
14041409

1405-
// Determine the node type for the topic reference
1406-
// that is appropriate for the active pipe-operator proposal.
1407-
const nodeType =
1408-
pipeProposal === "smart"
1409-
? "PipelinePrimaryTopicReference"
1410-
: // The proposal must otherwise be "hack",
1411-
// as enforced by testTopicReferenceConfiguration.
1412-
"TopicReference";
1410+
if (process.env.BABEL_8_BREAKING || pipeProposal === "hack") {
1411+
if (!this.topicReferenceIsAllowedInCurrentContext()) {
1412+
this.raise(Errors.PipeTopicUnbound, startLoc);
1413+
}
14131414

1414-
if (!this.topicReferenceIsAllowedInCurrentContext()) {
1415-
this.raise(
1416-
// The topic reference is not allowed in the current context:
1417-
// it is outside of a pipe body.
1418-
// Raise recoverable errors.
1419-
pipeProposal === "smart"
1420-
? Errors.PrimaryTopicNotAllowed
1421-
: // In this case, `pipeProposal === "hack"` is true.
1422-
Errors.PipeTopicUnbound,
1423-
startLoc,
1424-
);
1425-
}
1415+
// Register the topic reference so that its pipe body knows
1416+
// that its topic was used at least once.
1417+
this.registerTopicReference();
14261418

1427-
// Register the topic reference so that its pipe body knows
1428-
// that its topic was used at least once.
1429-
this.registerTopicReference();
1419+
return this.finishNode(node, "TopicReference");
1420+
} else {
1421+
// pipeProposal is "smart"
14301422

1431-
return this.finishNode(node, nodeType);
1423+
if (!this.topicReferenceIsAllowedInCurrentContext()) {
1424+
this.raise(Errors.PrimaryTopicNotAllowed, startLoc);
1425+
}
1426+
this.registerTopicReference();
1427+
return this.finishNode(node, "PipelinePrimaryTopicReference");
1428+
}
14321429
} else {
14331430
// The token does not match the plugin’s configuration.
14341431
throw this.raise(Errors.PipeTopicUnconfiguredToken, startLoc, {
@@ -1444,7 +1441,7 @@ export default abstract class ExpressionParser extends LValParser {
14441441
// then this is a valid topic reference.
14451442
// If the active pipe proposal is smart mix,
14461443
// then the topic token must always be `#`.
1447-
// If the active pipe proposal is neither (e.g., "minimal" or "fsharp"),
1444+
// If the active pipe proposal is neither (e.g., "minimal"(Babel 7) or "fsharp"),
14481445
// then an error is thrown.
14491446
testTopicReferenceConfiguration(
14501447
pipeProposal: string,
@@ -3065,8 +3062,13 @@ export default abstract class ExpressionParser extends LValParser {
30653062
// had before the function was called.
30663063

30673064
withSmartMixTopicForbiddingContext<T>(callback: () => T): T {
3068-
// @ts-expect-error Remove this in Babel 8
3069-
if (this.hasPlugin(["pipelineOperator", { proposal: "smart" }])) {
3065+
// TODO(Babel 8): Remove this method
3066+
3067+
if (
3068+
!process.env.BABEL_8_BREAKING &&
3069+
// @ts-expect-error Babel 7 only
3070+
this.hasPlugin(["pipelineOperator", { proposal: "smart" }])
3071+
) {
30703072
// Reset the parser’s topic context only if the smart-mix pipe proposal is active.
30713073
const outerContextTopicState = this.state.topicContext;
30723074
this.state.topicContext = {
@@ -3082,7 +3084,7 @@ export default abstract class ExpressionParser extends LValParser {
30823084
this.state.topicContext = outerContextTopicState;
30833085
}
30843086
} else {
3085-
// If the pipe proposal is "minimal", "fsharp", or "hack",
3087+
// If the pipe proposal is "minimal"(Babel 7), "fsharp", or "hack",
30863088
// or if no pipe proposal is active,
30873089
// then the callback result is returned
30883090
// without touching any extra parser state.

packages/babel-parser/src/plugin-utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,11 @@ export function validatePlugins(pluginsMap: Map<string, any>) {
9393
`Plugin conflict between \`["pipelineOperator", { proposal: "hack", topicToken: "#" }]\` and \`${JSON.stringify(["recordAndTuple", pluginsMap.get("recordAndTuple")])}\`.`,
9494
);
9595
}
96-
} else if (proposal === "smart" && tupleSyntaxIsHash) {
96+
} else if (
97+
!process.env.BABEL_8_BREAKING &&
98+
proposal === "smart" &&
99+
tupleSyntaxIsHash
100+
) {
97101
throw new Error(
98102
`Plugin conflict between \`["pipelineOperator", { proposal: "smart" }]\` and \`${JSON.stringify(["recordAndTuple", pluginsMap.get("recordAndTuple")])}\`.`,
99103
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
x |> # + 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"BABEL_8_BREAKING": false,
3+
"throws": "Topic reference is used, but the pipelineOperator plugin was not passed a \"proposal\": \"hack\" or \"smart\" option. (1:5)"
4+
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"throws": "Topic reference is used, but the pipelineOperator plugin was not passed a \"proposal\": \"hack\" or \"smart\" option. (1:5)"
3-
}
2+
"BABEL_8_BREAKING": true,
3+
"throws": "Topic references are only supported when using the `\"proposal\": \"hack\"` version of the pipeline proposal. (1:5)"
4+
}

packages/babel-plugin-proposal-pipeline-operator/src/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ const visitorsPerProposal = {
1616
export default declare((api, options: Options) => {
1717
api.assertVersion(REQUIRED_VERSION(7));
1818

19-
const { proposal } = options;
20-
21-
if (proposal === "smart") {
19+
if (!process.env.BABEL_8_BREAKING && options.proposal === "smart") {
2220
console.warn(
2321
`The smart-mix pipe operator is deprecated. Use "proposal": "hack" instead.`,
2422
);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
x |> #;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"BABEL_8_BREAKING": false,
3+
"plugins": ["proposal-pipeline-operator"],
4+
"throws": "The pipeline plugin requires a \"proposal\" option. \"proposal\" must be one of: \"minimal\", \"fsharp\", \"hack\", \"smart\". See <https://babeljs.io/docs/en/babel-plugin-proposal-pipeline-operator>."
5+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"BABEL_8_BREAKING": true,
23
"plugins": ["proposal-pipeline-operator"],
3-
"throws": "The pipeline plugin requires a \"proposal\" option. \"proposal\" must be one of: \"minimal\", \"fsharp\", \"hack\", \"smart\". See <https://babeljs.io/docs/en/babel-plugin-proposal-pipeline-operator>."
4+
"throws": "The pipeline plugin requires a \"proposal\" option. \"proposal\" must be one of: \"fsharp\", \"hack\". See <https://babeljs.io/docs/en/babel-plugin-proposal-pipeline-operator>."
45
}

0 commit comments

Comments
 (0)