Skip to content

Commit

Permalink
Relax naming rules to allow valid java bean properties as jsproperties
Browse files Browse the repository at this point in the history
The old implementation only allowed a capital letter as the first
character of a bean-like property, which isn't sufficient. Instead, we
need to support cases where the first character is *not* lower case, so
that non-letters that are valid for the first character are permitted.

Fixes #9554
  • Loading branch information
niloc132 committed Dec 12, 2023
1 parent f9028fe commit 829628b
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 16 deletions.
2 changes: 1 addition & 1 deletion dev/core/src/com/google/gwt/dev/jjs/ast/HasJsInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public String computeName(JMember member) {

private static boolean startsWithCamelCase(String string, String prefix) {
return string.length() > prefix.length() && string.startsWith(prefix)
&& Character.isUpperCase(string.charAt(prefix.length()));
&& !Character.isLowerCase(string.charAt(prefix.length()));
}
}

Expand Down
2 changes: 1 addition & 1 deletion dev/core/src/com/google/gwt/dev/js/JsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ public static JsFunction isFunctionDeclaration(JsStatement stmt) {
/**
* A JavaScript identifier contains only letters, numbers, _, $ and does not begin with a number.
* There are actually other valid identifiers, such as ones that contain escaped Unicode
* characters but we disallow those for the time being.
* characters, but we disallow those for the time being.
*/
public static boolean isValidJsIdentifier(String name) {
return JAVASCRIPT_VALID_IDENTIFIER_PATTERN.matcher(name).matches();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,77 @@ public void testJsNameGlobalNamespacesSucceeds() throws Exception {
assertBuggySucceeds();
}

public void testJsMethodWithDollarsign() throws Exception {
addSnippetImport("jsinterop.annotations.JsType");
addSnippetImport("jsinterop.annotations.JsMethod");
addSnippetImport("jsinterop.annotations.JsProperty");
addSnippetImport("jsinterop.annotations.JsPackage");
addSnippetClassDecl(
"@JsType public static class Buggy {",
" public void $() {",
" }",
" public void $method(String l) {",
" }",
" public void method$(String l) {",
" }",
" public void method$name(String l) {",
" }",
"}");
assertBuggySucceeds();
}

public void testJsFieldWithDollarsign() throws Exception {
addSnippetImport("jsinterop.annotations.JsType");
addSnippetImport("jsinterop.annotations.JsMethod");
addSnippetImport("jsinterop.annotations.JsProperty");
addSnippetImport("jsinterop.annotations.JsPackage");
addSnippetClassDecl(
"@JsType public static class Buggy {",
" public String $;",
" public String $field;",
" public String field$;",
" public String field$name;",
"}");
assertBuggySucceeds();
}

public void testJsPropertyWithDollarsign() throws Exception {
addSnippetImport("jsinterop.annotations.JsType");
addSnippetImport("jsinterop.annotations.JsProperty");
addSnippetClassDecl(
"@JsType public static class Buggy {",
" @JsProperty",
" public String get$() {",
" return null;",
" }",
" @JsProperty",
" public void set$(String value) {",
" }",
" @JsProperty",
" public String get$1() {",
" return null;",
" }",
" @JsProperty",
" public void set$1(String value) {",
" }",
" @JsProperty",
" public String getVal$() {",
" return null;",
" }",
" @JsProperty",
" public void setVal$(String value) {",
" }",
" @JsProperty",
" public String getVal$1() {",
" return null;",
" }",
" @JsProperty",
" public void setVal$1(String value) {",
" }",
"}");
assertBuggySucceeds();
}

public void testSingleJsTypeSucceeds() throws Exception {
addSnippetImport("jsinterop.annotations.JsType");
addSnippetClassDecl(
Expand Down Expand Up @@ -2555,20 +2626,6 @@ public void testUnusableByJsSyntheticMembersSucceeds() throws Exception {
assertBuggySucceeds();
}

private static final MockJavaResource jsFunctionInterface = new MockJavaResource(
"test.MyJsFunctionInterface") {
@Override
public CharSequence getContent() {
StringBuilder code = new StringBuilder();
code.append("package test;\n");
code.append("import jsinterop.annotations.JsFunction;\n");
code.append("@JsFunction public interface MyJsFunctionInterface {\n");
code.append("int foo(int x);\n");
code.append("}\n");
return code;
}
};

public final void assertBuggySucceeds(String... expectedWarnings)
throws Exception {
Result result = assertCompileSucceeds("Buggy buggy = null;", expectedWarnings);
Expand Down

0 comments on commit 829628b

Please sign in to comment.