Skip to content

Commit

Permalink
Java: Fixed record false positives (#3348)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment committed Mar 4, 2022
1 parent 499b1fa commit 3bd8fdb
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 2 deletions.
2 changes: 1 addition & 1 deletion components/prism-java.js
@@ -1,6 +1,6 @@
(function (Prism) {

var keywords = /\b(?:abstract|assert|boolean|break|byte|case|catch|char|class|const|continue|default|do|double|else|enum|exports|extends|final|finally|float|for|goto|if|implements|import|instanceof|int|interface|long|module|native|new|non-sealed|null|open|opens|package|permits|private|protected|provides|public|record|requires|return|sealed|short|static|strictfp|super|switch|synchronized|this|throw|throws|to|transient|transitive|try|uses|var|void|volatile|while|with|yield)\b/;
var keywords = /\b(?:abstract|assert|boolean|break|byte|case|catch|char|class|const|continue|default|do|double|else|enum|exports|extends|final|finally|float|for|goto|if|implements|import|instanceof|int|interface|long|module|native|new|non-sealed|null|open|opens|package|permits|private|protected|provides|public|record(?!\s*[(){}[\]<>=%~.:,;?+\-*/&|^])|requires|return|sealed|short|static|strictfp|super|switch|synchronized|this|throw|throws|to|transient|transitive|try|uses|var|void|volatile|while|with|yield)\b/;

// full package (optional) + parent classes (optional)
var classNamePrefix = /(^|[^\w.])(?:[a-z]\w*\s*\.\s*)*(?:[A-Z]\w*\s*\.\s*)*/.source;
Expand Down
2 changes: 1 addition & 1 deletion components/prism-java.min.js

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

87 changes: 87 additions & 0 deletions tests/languages/java/issue3345.test
@@ -0,0 +1,87 @@
public class Main {
// "record" is used as variable name, but Prism marked it keyword
public static void main(String[] record) {
System.out.println(record);
}
// "record" is used as method name, but Prism marked it keyword
public void record(String aaa){
}
// "record" is keyword since java 14
public static record A() {
}
// it cannot be used as class name in java syntax
public static class record {
}
}

----------------------------------------------------

[
["keyword", "public"],
["keyword", "class"],
["class-name", ["Main"]],
["punctuation", "{"],

["comment", "// \"record\" is used as variable name, but Prism marked it keyword"],

["keyword", "public"],
["keyword", "static"],
["keyword", "void"],
["function", "main"],
["punctuation", "("],
["class-name", ["String"]],
["punctuation", "["],
["punctuation", "]"],
" record",
["punctuation", ")"],
["punctuation", "{"],

["class-name", ["System"]],
["punctuation", "."],
"out",
["punctuation", "."],
["function", "println"],
["punctuation", "("],
"record",
["punctuation", ")"],
["punctuation", ";"],

["punctuation", "}"],

["comment", "// \"record\" is used as method name, but Prism marked it keyword"],

["keyword", "public"],
["keyword", "void"],
["function", "record"],
["punctuation", "("],
["class-name", ["String"]],
" aaa",
["punctuation", ")"],
["punctuation", "{"],

["punctuation", "}"],

["comment", "// \"record\" is keyword since java 14"],

["keyword", "public"],
["keyword", "static"],
["keyword", "record"],
["class-name", ["A"]],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "{"],

["punctuation", "}"],

["comment", "// it cannot be used as class name in java syntax"],

["keyword", "public"],
["keyword", "static"],
["keyword", "class"],
" record ",
["punctuation", "{"],

["punctuation", "}"],

["punctuation", "}"]
]

0 comments on commit 3bd8fdb

Please sign in to comment.