-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
8317621: --add-script should support JavaScript modules #18546
Conversation
👋 Welcome back hannesw! A progress list of the required criteria for merging this PR into |
@hns This change now passes all automated pre-integration checks. ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details. After integration, the commit message for the final commit will be:
You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed. At the time when this comment was updated there had been 616 new commits pushed to the
As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details. ➡️ To integrate this PR with the above commit message to the |
I tested module detection on the Angular project which contains roughly 1100
In the meantime I also researched usage of the |
Webrevs
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
generally great; some minor comments for your consideration
additionalScripts = options.additionalScripts().stream().map(sf -> { | ||
DocFile file = DocFile.createFileForInput(this, sf); | ||
boolean isModule = sf.toLowerCase(Locale.ROOT).endsWith(".mjs"); | ||
if (!isModule) { | ||
// Regex to detect JavaScript modules | ||
Pattern modulePattern = Pattern.compile(""" | ||
(?:^|[;}])\\s*(?:\ | ||
import\\s*["']|\ | ||
import[\\s{*][^()]*from\\s*["']|\ | ||
export(?:\\s+(?:let|const|function|class|var|default|async)|\\s*[{*]))"""); | ||
try (InputStream in = file.openInputStream(); | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(in))) { | ||
isModule = reader.lines().anyMatch(s -> modulePattern.matcher(s).find()); | ||
} catch (DocFileIOException | IOException e) { | ||
// Errors are handled when copying resources | ||
} | ||
} | ||
return new JavaScriptFile(DocPath.create(file.getName()), isModule); | ||
}).collect(Collectors.toCollection(ArrayList::new)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stylistically, big lambdas like this are harder to read.
Consider pulling the body of this code to a separate private local method, and then using a method reference in the lambda calls.
additionalScripts = options.additionalScripts().stream().map(sf -> { | ||
DocFile file = DocFile.createFileForInput(this, sf); | ||
boolean isModule = sf.toLowerCase(Locale.ROOT).endsWith(".mjs"); | ||
if (!isModule) { | ||
// Regex to detect JavaScript modules | ||
Pattern modulePattern = Pattern.compile(""" | ||
(?:^|[;}])\\s*(?:\ | ||
import\\s*["']|\ | ||
import[\\s{*][^()]*from\\s*["']|\ | ||
export(?:\\s+(?:let|const|function|class|var|default|async)|\\s*[{*]))"""); | ||
try (InputStream in = file.openInputStream(); | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(in))) { | ||
isModule = reader.lines().anyMatch(s -> modulePattern.matcher(s).find()); | ||
} catch (DocFileIOException | IOException e) { | ||
// Errors are handled when copying resources | ||
} | ||
} | ||
return new JavaScriptFile(DocPath.create(file.getName()), isModule); | ||
}).collect(Collectors.toCollection(ArrayList::new)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realize it is not a change in this PR, but is there a reason to use Collectors.toCollection(ArrayList::new)
instead of Collectors.toList()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about the name of the new method, but do not as yet have any better suggestion, so OK.
/integrate |
Going to push as commit 9bb6169.
Your commit was automatically rebased without conflicts. |
Please review an enhancement to detect JavaScript modules when added to
javadoc
with the--add-script
option, which require a differenttype
attribute in the HTML<script>
element. The main method of detection is to scan the script content forimport
andexport
statements. The*.mjs
extension is also recognized, although it is rarely used for browser modules.I have tested the recognition against scripts and modules in projects such as Angular and Mermaid and it worked quite well in detecting JavaScript modules.
Progress
Issue
Reviewers
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/18546/head:pull/18546
$ git checkout pull/18546
Update a local copy of the PR:
$ git checkout pull/18546
$ git pull https://git.openjdk.org/jdk.git pull/18546/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 18546
View PR using the GUI difftool:
$ git pr show -t 18546
Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/18546.diff
Webrev
Link to Webrev Comment