Skip to content
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

Closed
wants to merge 5 commits into from

Conversation

hns
Copy link
Member

@hns hns commented Mar 29, 2024

Please review an enhancement to detect JavaScript modules when added to javadoc with the --add-script option, which require a different type attribute in the HTML <script> element. The main method of detection is to scan the script content for import and export 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

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8317621: --add-script should support JavaScript modules (Enhancement - P3)

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

@bridgekeeper
Copy link

bridgekeeper bot commented Mar 29, 2024

👋 Welcome back hannesw! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Mar 29, 2024

@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:

8317621: --add-script should support JavaScript modules

Reviewed-by: jjg

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 master branch:

  • 4083255: 8316138: Add GlobalSign 2 TLS root certificates
  • 43b109b: 8330066: HeapDumpPath and HeapDumpGzipLevel VM options do not mention HeapDumpBeforeFullGC and HeapDumpAfterFullGC
  • 7cff04f: 8330276: Console methods with explicit Locale
  • 8a4315f: 8331987: Enhance stacktrace clarity for CompletableFuture CancellationException
  • 491b3b4: 8332256: Shenandoah: Do not visit heap threads during shutdown
  • 9c02c8d: 8332255: Shenandoah: Remove duplicate definition of init mark closure
  • 42ccb74: 8331940: ClassFile API ArrayIndexOutOfBoundsException with certain class files
  • 61aff6d: 8332112: Update nsk.share.Log to don't print summary during VM shutdown hook
  • 30bb066: 8332003: Clarify javadoc for MemoryLayout::offsetHandle
  • c4867c6: 8329273: C2 SuperWord: Some basic MemorySegment IR tests
  • ... and 606 more: https://git.openjdk.org/jdk/compare/df01cc528d3b6e52925b92119e43caee54ec86e8...master

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 master branch, type /integrate in a new comment.

@openjdk
Copy link

openjdk bot commented Mar 29, 2024

@hns The following label will be automatically applied to this pull request:

  • javadoc

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the javadoc javadoc-dev@openjdk.org label Mar 29, 2024
@openjdk openjdk bot changed the title JDK-8317621: --add-script should support JavaScript modules 8317621: --add-script should support JavaScript modules Apr 10, 2024
@hns
Copy link
Member Author

hns commented Apr 22, 2024

I tested module detection on the Angular project which contains roughly 1100 .js files and 50 .mjs files.

  • All but one .mjs files were recognized as modules, with one file requiring the pattern to be updated to match export async function....
  • Of the .js files, 33 were recognized as modules, of which 3 were false positives caused by module source contained in a string literal such as this file.
  • I did not check for false negatives in .js files because of the number of files to look at, but it matches my expectation that most of the source and test code would be non-modular.

In the meantime I also researched usage of the .mjs extension and it turns out that .mjs is mosty used in Node.js, with .js being preferred in browser because of problems with web server MIME type configuration.

@hns hns marked this pull request as ready for review May 7, 2024 15:20
@openjdk openjdk bot added the rfr Pull request is ready for review label May 7, 2024
@mlbridge
Copy link

mlbridge bot commented May 7, 2024

Webrevs

Copy link
Contributor

@jonathan-gibbons jonathan-gibbons left a 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

Comment on lines 336 to 354
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));
Copy link
Contributor

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.

Comment on lines 336 to 354
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));
Copy link
Contributor

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()

Copy link
Contributor

@jonathan-gibbons jonathan-gibbons left a 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.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label May 15, 2024
@hns
Copy link
Member Author

hns commented May 17, 2024

/integrate

@openjdk
Copy link

openjdk bot commented May 17, 2024

Going to push as commit 9bb6169.
Since your change was applied there have been 644 commits pushed to the master branch:

  • 4eb1eaf: 8329617: Update stylesheet for specs and tool documentation
  • d4c2edf: 8331855: Convert jdk.jdeps jdeprscan and jdeps to use the Classfile API
  • beeffd4: 8332109: Convert remaining tests using com.sun.tools.classfile to ClassFile API
  • e0d1c4b: 8321428: Deprecate for removal the package java.beans.beancontext
  • 0b0445b: 8331724: Refactor j.l.constant implementation to internal package
  • d84a8fd: 8332327: Return _methods_jmethod_ids field back in VMStructs
  • f1ce9b0: 8331557: Serial: Refactor SerialHeap::do_collection
  • 14198f5: 8329653: JLILaunchTest fails on AIX after JDK-8329131
  • ae999ea: 8129418: JShell: better highlighting of errors in imports on demand
  • 6422efa: 8332394: Add friendly output when @ir rule missing value
  • ... and 634 more: https://git.openjdk.org/jdk/compare/df01cc528d3b6e52925b92119e43caee54ec86e8...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label May 17, 2024
@openjdk openjdk bot closed this May 17, 2024
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels May 17, 2024
@openjdk
Copy link

openjdk bot commented May 17, 2024

@hns Pushed as commit 9bb6169.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integrated Pull request has been integrated javadoc javadoc-dev@openjdk.org
Development

Successfully merging this pull request may close these issues.

2 participants