Skip to content

Commit

Permalink
fix(pacmak): _ is not treated as a keyword in Java (#4094)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rix0rrr authored May 16, 2023
1 parent 30afa09 commit eaaf3ee
Show file tree
Hide file tree
Showing 10 changed files with 384 additions and 13 deletions.
9 changes: 9 additions & 0 deletions packages/@scope/jsii-calc-lib/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,15 @@ export class BaseFor2647 {
}
}

/**
* For Java, test that an identifier that is just a _ is handled
*/
export class FunctionWithUnderscoreArgument {
public foo(_: string) {
return _;
}
}

export * as submodule from './submodule';
export * from './duplicate-inherited-prop';
export * as deprecationRemoval from './deprecation-removal';
51 changes: 48 additions & 3 deletions packages/@scope/jsii-calc-lib/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@
"@scope/jsii-calc-lib.deprecationRemoval": {
"locationInModule": {
"filename": "lib/index.ts",
"line": 132
"line": 141
},
"symbolId": "lib/deprecation-removal:"
},
"@scope/jsii-calc-lib.submodule": {
"locationInModule": {
"filename": "lib/index.ts",
"line": 130
"line": 139
},
"readme": {
"markdown": "# Submodule Readme\n\nThis is a submodule readme.\n"
Expand Down Expand Up @@ -333,6 +333,51 @@
"name": "EnumFromScopedModule",
"symbolId": "lib/index:EnumFromScopedModule"
},
"@scope/jsii-calc-lib.FunctionWithUnderscoreArgument": {
"assembly": "@scope/jsii-calc-lib",
"docs": {
"stability": "deprecated",
"summary": "For Java, test that an identifier that is just a _ is handled."
},
"fqn": "@scope/jsii-calc-lib.FunctionWithUnderscoreArgument",
"initializer": {
"docs": {
"stability": "deprecated"
}
},
"kind": "class",
"locationInModule": {
"filename": "lib/index.ts",
"line": 133
},
"methods": [
{
"docs": {
"stability": "deprecated"
},
"locationInModule": {
"filename": "lib/index.ts",
"line": 134
},
"name": "foo",
"parameters": [
{
"name": "_",
"type": {
"primitive": "string"
}
}
],
"returns": {
"type": {
"primitive": "string"
}
}
}
],
"name": "FunctionWithUnderscoreArgument",
"symbolId": "lib/index:FunctionWithUnderscoreArgument"
},
"@scope/jsii-calc-lib.IDoublable": {
"assembly": "@scope/jsii-calc-lib",
"docs": {
Expand Down Expand Up @@ -1022,5 +1067,5 @@
}
},
"version": "0.0.0",
"fingerprint": "XDMAZYhhgc09X8VS8hpn3ch21YxKDn+HB0w82IofsRM="
"fingerprint": "yrpQ+/ynmcWEtNZgCAeq2yp4JpEpln2V/fRhUc5uVUs="
}
11 changes: 11 additions & 0 deletions packages/jsii-pacmak/lib/targets/java.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ class JavaGenerator extends Generator {
'void',
'volatile',
'while',
'_',
];

/**
Expand All @@ -587,6 +588,11 @@ class JavaGenerator extends Generator {
return propertyName;
}

if (propertyName === '_') {
// Slightly different pattern for this one
return '__';
}

if (JavaGenerator.RESERVED_KEYWORDS.includes(propertyName)) {
return `${propertyName}Value`;
}
Expand All @@ -602,6 +608,11 @@ class JavaGenerator extends Generator {
return methodName;
}

if (methodName === '_') {
// Different pattern for this one. Also this should never happen, who names a function '_' ??
return 'doIt';
}

if (JavaGenerator.RESERVED_KEYWORDS.includes(methodName)) {
return `do${jsiiToPascalCase(methodName)}`;
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit eaaf3ee

Please sign in to comment.