Skip to content

Commit 80b9ed9

Browse files
refactor: remove classic switch label rule
1 parent 09ea2d3 commit 80b9ed9

File tree

4 files changed

+37
-28
lines changed

4 files changed

+37
-28
lines changed

packages/java-parser/api.d.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ export abstract class JavaCstVisitor<IN, OUT> implements ICstVisitor<IN, OUT> {
219219
switchBlock(ctx: SwitchBlockCtx, param?: IN): OUT;
220220
switchBlockStatementGroup(ctx: SwitchBlockStatementGroupCtx, param?: IN): OUT;
221221
switchLabel(ctx: SwitchLabelCtx, param?: IN): OUT;
222-
classicSwitchLabel(ctx: ClassicSwitchLabelCtx, param?: IN): OUT;
223222
switchRule(ctx: SwitchRuleCtx, param?: IN): OUT;
224223
caseConstant(ctx: CaseConstantCtx, param?: IN): OUT;
225224
whileStatement(ctx: WhileStatementCtx, param?: IN): OUT;
@@ -253,6 +252,7 @@ export abstract class JavaCstVisitor<IN, OUT> implements ICstVisitor<IN, OUT> {
253252
ctx: IsLocalVariableDeclarationCtx,
254253
param?: IN
255254
): OUT;
255+
isClassicSwitchLabel(ctx: IsClassicSwitchLabelCtx, param?: IN): OUT;
256256
expression(ctx: ExpressionCtx, param?: IN): OUT;
257257
lambdaExpression(ctx: LambdaExpressionCtx, param?: IN): OUT;
258258
lambdaParameters(ctx: LambdaParametersCtx, param?: IN): OUT;
@@ -548,7 +548,6 @@ export abstract class JavaCstVisitorWithDefaults<IN, OUT>
548548
switchBlock(ctx: SwitchBlockCtx, param?: IN): OUT;
549549
switchBlockStatementGroup(ctx: SwitchBlockStatementGroupCtx, param?: IN): OUT;
550550
switchLabel(ctx: SwitchLabelCtx, param?: IN): OUT;
551-
classicSwitchLabel(ctx: ClassicSwitchLabelCtx, param?: IN): OUT;
552551
switchRule(ctx: SwitchRuleCtx, param?: IN): OUT;
553552
caseConstant(ctx: CaseConstantCtx, param?: IN): OUT;
554553
whileStatement(ctx: WhileStatementCtx, param?: IN): OUT;
@@ -582,6 +581,7 @@ export abstract class JavaCstVisitorWithDefaults<IN, OUT>
582581
ctx: IsLocalVariableDeclarationCtx,
583582
param?: IN
584583
): OUT;
584+
isClassicSwitchLabel(ctx: IsClassicSwitchLabelCtx, param?: IN): OUT;
585585
expression(ctx: ExpressionCtx, param?: IN): OUT;
586586
lambdaExpression(ctx: LambdaExpressionCtx, param?: IN): OUT;
587587
lambdaParameters(ctx: LambdaParametersCtx, param?: IN): OUT;
@@ -2367,7 +2367,8 @@ export interface SwitchBlockStatementGroupCstNode extends CstNode {
23672367
}
23682368

23692369
export type SwitchBlockStatementGroupCtx = {
2370-
classicSwitchLabel: ClassicSwitchLabelCstNode[];
2370+
switchLabel: SwitchLabelCstNode[];
2371+
Colon: IToken[];
23712372
blockStatements?: BlockStatementsCstNode[];
23722373
};
23732374

@@ -2381,16 +2382,6 @@ export type SwitchLabelCtx = {
23812382
Default?: IToken[];
23822383
};
23832384

2384-
export interface ClassicSwitchLabelCstNode extends CstNode {
2385-
name: "classicSwitchLabel";
2386-
children: ClassicSwitchLabelCtx;
2387-
}
2388-
2389-
export type ClassicSwitchLabelCtx = {
2390-
switchLabel: SwitchLabelCstNode[];
2391-
Colon: IToken[];
2392-
};
2393-
23942385
export interface SwitchRuleCstNode extends CstNode {
23952386
name: "switchRule";
23962387
children: SwitchRuleCtx;
@@ -2735,6 +2726,16 @@ export type IsLocalVariableDeclarationCtx = {
27352726
variableDeclaratorId: VariableDeclaratorIdCstNode[];
27362727
};
27372728

2729+
export interface IsClassicSwitchLabelCstNode extends CstNode {
2730+
name: "isClassicSwitchLabel";
2731+
children: IsClassicSwitchLabelCtx;
2732+
}
2733+
2734+
export type IsClassicSwitchLabelCtx = {
2735+
switchLabel: SwitchLabelCstNode[];
2736+
Colon: IToken[];
2737+
};
2738+
27382739
export interface ExpressionCstNode extends CstNode {
27392740
name: "expression";
27402741
children: ExpressionCtx;

packages/java-parser/src/productions/blocks-and-statements.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ function defineRules($, t) {
180180
$.CONSUME(t.LCurly);
181181
$.OR([
182182
{
183-
GATE: () => this.BACKTRACK_LOOKAHEAD($.classicSwitchLabel),
183+
GATE: () => this.BACKTRACK_LOOKAHEAD($.isClassicSwitchLabel),
184184
ALT: () => $.MANY(() => $.SUBRULE($.switchBlockStatementGroup))
185185
},
186186
{
@@ -191,7 +191,10 @@ function defineRules($, t) {
191191
});
192192

193193
$.RULE("switchBlockStatementGroup", () => {
194-
$.AT_LEAST_ONE(() => $.SUBRULE($.classicSwitchLabel));
194+
$.AT_LEAST_ONE(() => {
195+
$.SUBRULE($.switchLabel);
196+
$.CONSUME(t.Colon);
197+
});
195198
$.OPTION(() => {
196199
$.SUBRULE($.blockStatements);
197200
});
@@ -219,12 +222,6 @@ function defineRules($, t) {
219222
]);
220223
});
221224

222-
// https://docs.oracle.com/javase/specs/jls/se11/html/jls-14.html#jls-SwitchLabel
223-
$.RULE("classicSwitchLabel", () => {
224-
$.SUBRULE($.switchLabel);
225-
$.CONSUME(t.Colon);
226-
});
227-
228225
// https://docs.oracle.com/javase/specs/jls/se15/html/jls-14.html#jls-SwitchRule
229226
$.RULE("switchRule", () => {
230227
$.SUBRULE($.switchLabel);
@@ -555,6 +552,11 @@ function defineRules($, t) {
555552
return false;
556553
}
557554
});
555+
556+
$.RULE("isClassicSwitchLabel", () => {
557+
$.SUBRULE($.switchLabel);
558+
$.CONSUME(t.Colon);
559+
});
558560
}
559561

560562
module.exports = {

packages/prettier-plugin-java/src/options.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ module.exports = {
2929
{ value: "switchLabel" },
3030
{ value: "switchRule" },
3131
{ value: "switchRuleLabel" },
32-
{ value: "classicSwitchLabel" },
3332
{ value: "caseConstant" },
3433
{ value: "whileStatement" },
3534
{ value: "doStatement" },
@@ -59,6 +58,7 @@ module.exports = {
5958
{ value: "variableAccess" },
6059
{ value: "isBasicForStatement" },
6160
{ value: "isLocalVariableDeclaration" },
61+
{ value: "isClassicSwitchLabel" },
6262
{ value: "classDeclaration" },
6363
{ value: "normalClassDeclaration" },
6464
{ value: "classModifier" },

packages/prettier-plugin-java/src/printers/blocks-and-statements.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,18 @@ class BlocksAndStatementPrettierVisitor {
209209
}
210210

211211
switchBlockStatementGroup(ctx) {
212-
const switchLabels = this.mapVisit(ctx.classicSwitchLabel);
212+
const switchLabels = this.mapVisit(ctx.switchLabel);
213+
214+
const labels = [];
215+
for (let i = 0; i < switchLabels.length; i++) {
216+
labels.push(concat([switchLabels[i], ctx.Colon[i]]));
217+
}
218+
213219
const blockStatements = this.visit(ctx.blockStatements);
214220

215221
return indent(
216222
rejectAndJoin(hardline, [
217-
rejectAndJoin(hardline, switchLabels),
223+
rejectAndJoin(hardline, labels),
218224
blockStatements
219225
])
220226
);
@@ -232,10 +238,6 @@ class BlocksAndStatementPrettierVisitor {
232238
return ctx.Default[0];
233239
}
234240

235-
classicSwitchLabel(ctx) {
236-
return concat([this.visit(ctx.switchLabel), ctx.Colon[0]]);
237-
}
238-
239241
switchRule(ctx) {
240242
const switchLabel = this.visit(ctx.switchLabel);
241243

@@ -564,6 +566,10 @@ class BlocksAndStatementPrettierVisitor {
564566
isLocalVariableDeclaration() {
565567
return "isLocalVariableDeclaration";
566568
}
569+
570+
isClassicSwitchLabel() {
571+
return "isClassicSwitchLabel";
572+
}
567573
}
568574

569575
module.exports = {

0 commit comments

Comments
 (0)