Skip to content

Commit eaaf3ee

Browse files
authored
fix(pacmak): _ is not treated as a keyword in Java (#4094)
Since Java 9, `_` is considered a keyword. Although we are technically still targeting Java 8 with our source, if we want our source to be parseable by more modern tools or are considering raising the threshold at some point, it would be good to have this in place already. This might technically be considered breaking in case someone is using `_` as an identifier in a public API, since in newer releases it will be renamed. But given that most jsii library authors will be targeting users that will be running on recent Java versions, and Java 9 is ~6 years old, the chance of that happening in practice is vanishingly small. --- By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license]. [Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
1 parent 30afa09 commit eaaf3ee

File tree

10 files changed

+384
-13
lines changed

10 files changed

+384
-13
lines changed

packages/@scope/jsii-calc-lib/lib/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,15 @@ export class BaseFor2647 {
127127
}
128128
}
129129

130+
/**
131+
* For Java, test that an identifier that is just a _ is handled
132+
*/
133+
export class FunctionWithUnderscoreArgument {
134+
public foo(_: string) {
135+
return _;
136+
}
137+
}
138+
130139
export * as submodule from './submodule';
131140
export * from './duplicate-inherited-prop';
132141
export * as deprecationRemoval from './deprecation-removal';

packages/@scope/jsii-calc-lib/test/assembly.jsii

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,14 @@
9595
"@scope/jsii-calc-lib.deprecationRemoval": {
9696
"locationInModule": {
9797
"filename": "lib/index.ts",
98-
"line": 132
98+
"line": 141
9999
},
100100
"symbolId": "lib/deprecation-removal:"
101101
},
102102
"@scope/jsii-calc-lib.submodule": {
103103
"locationInModule": {
104104
"filename": "lib/index.ts",
105-
"line": 130
105+
"line": 139
106106
},
107107
"readme": {
108108
"markdown": "# Submodule Readme\n\nThis is a submodule readme.\n"
@@ -333,6 +333,51 @@
333333
"name": "EnumFromScopedModule",
334334
"symbolId": "lib/index:EnumFromScopedModule"
335335
},
336+
"@scope/jsii-calc-lib.FunctionWithUnderscoreArgument": {
337+
"assembly": "@scope/jsii-calc-lib",
338+
"docs": {
339+
"stability": "deprecated",
340+
"summary": "For Java, test that an identifier that is just a _ is handled."
341+
},
342+
"fqn": "@scope/jsii-calc-lib.FunctionWithUnderscoreArgument",
343+
"initializer": {
344+
"docs": {
345+
"stability": "deprecated"
346+
}
347+
},
348+
"kind": "class",
349+
"locationInModule": {
350+
"filename": "lib/index.ts",
351+
"line": 133
352+
},
353+
"methods": [
354+
{
355+
"docs": {
356+
"stability": "deprecated"
357+
},
358+
"locationInModule": {
359+
"filename": "lib/index.ts",
360+
"line": 134
361+
},
362+
"name": "foo",
363+
"parameters": [
364+
{
365+
"name": "_",
366+
"type": {
367+
"primitive": "string"
368+
}
369+
}
370+
],
371+
"returns": {
372+
"type": {
373+
"primitive": "string"
374+
}
375+
}
376+
}
377+
],
378+
"name": "FunctionWithUnderscoreArgument",
379+
"symbolId": "lib/index:FunctionWithUnderscoreArgument"
380+
},
336381
"@scope/jsii-calc-lib.IDoublable": {
337382
"assembly": "@scope/jsii-calc-lib",
338383
"docs": {
@@ -1022,5 +1067,5 @@
10221067
}
10231068
},
10241069
"version": "0.0.0",
1025-
"fingerprint": "XDMAZYhhgc09X8VS8hpn3ch21YxKDn+HB0w82IofsRM="
1070+
"fingerprint": "yrpQ+/ynmcWEtNZgCAeq2yp4JpEpln2V/fRhUc5uVUs="
10261071
}

packages/jsii-pacmak/lib/targets/java.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ class JavaGenerator extends Generator {
576576
'void',
577577
'volatile',
578578
'while',
579+
'_',
579580
];
580581

581582
/**
@@ -587,6 +588,11 @@ class JavaGenerator extends Generator {
587588
return propertyName;
588589
}
589590

591+
if (propertyName === '_') {
592+
// Slightly different pattern for this one
593+
return '__';
594+
}
595+
590596
if (JavaGenerator.RESERVED_KEYWORDS.includes(propertyName)) {
591597
return `${propertyName}Value`;
592598
}
@@ -602,6 +608,11 @@ class JavaGenerator extends Generator {
602608
return methodName;
603609
}
604610

611+
if (methodName === '_') {
612+
// Different pattern for this one. Also this should never happen, who names a function '_' ??
613+
return 'doIt';
614+
}
615+
605616
if (JavaGenerator.RESERVED_KEYWORDS.includes(methodName)) {
606617
return `do${jsiiToPascalCase(methodName)}`;
607618
}

packages/jsii-pacmak/test/generated-code/__snapshots__/target-dotnet.test.js.snap

Lines changed: 60 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/jsii-pacmak/test/generated-code/__snapshots__/target-go.test.js.snap

Lines changed: 133 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)