GeezerVison is showing github.com — [javascript] Fix #1756: Support hashbang comments in CPD by toyeshhm · Pull Request #7383 · pmd/pmd
Conversation
Describe the PR
CPD fails on any JavaScript file that starts with a hashbang line such as #!/usr/bin/env node:
Lexical error at line 1, column 1: "#" (35), after : "" (in lexical state DEFAULT)
The JavaCC grammar that CPD uses for JavaScript (Ecmascript5.jj) has no token that starts with #, so the lexer stops at the first character. Hashbang comments have been part of the language since ECMAScript 2023 (12.5 Hashbang Comments), and they are common in Node.js CLI scripts.
This adds a HASHBANG_COMMENT special token: #! followed by everything up to the line terminator. As a special token it is treated like the other comments and produces no CPD tokens. The spec only allows a hashbang comment at the very start of the source text, so the token's lexical action throws a LexException when it starts at any other offset. A #! later in a file is still an error, as before.
Only the JavaCC lexer used by CPD is changed.
Tests in EcmascriptCpdLexerTest:
testHashbangComment tokenizes a new hashbang.js that starts with #!/usr/bin/env node. Before the fix it fails with the lexical error above. Now the hashbang line is skipped and the rest of the file is tokenized as usual (hashbang.txt).
testHashbangOnlyAtStartOfFile checks that #! on a later line is still a lexical error.
Related issues
Ready?
- Added unit tests for fixed bug/feature
- Passing all unit tests
- Complete build
./mvnw clean verify passes (checked automatically by github actions)
- Added (in-code) documentation (if needed)
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
# Conflicts:
# docs/pages/release_notes.md
|
Documentation Preview
Compared to main:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
There are 0 changed duplications, 0 new duplications and 0 removed duplications.
There are 0 changed CPD errors, 0 new CPD errors and 0 removed CPD errors.
Regression Tester Report
(comment created at 2026-11-27 20:44:04+00:00 for 7dbe93d)
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
# Conflicts:
# docs/pages/release_notes.md
This branch has not been deployed
No deployments
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.