-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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
module: standardize strip shebang behaviour #12202
Conversation
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.
LGTM with a style nit.
lib/internal/module.js
Outdated
var contLen = content.length; | ||
if (contLen >= 2) { | ||
if (content.charCodeAt(0) === 35/*#*/ && | ||
content.charCodeAt(1) === 33/*!*/) { |
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.
Style nit: two more spaces of indent.
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.
Fixed
When loading a module, Node needs to finds the end of a shebang comment by searching for a \r or \n character. This behaviour is now standardized into a dedicated internal module function Refs: nodejs#12180
fef91db
to
355449d
Compare
if (contLen === 2) { | ||
// Exact match | ||
content = ''; | ||
} else { |
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.
You could just drop the if (contLen === 2)
case, it should almost never be hit.
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.
LGTM, but I think we should add a test.
/** | ||
* Find end of shebang line and slice it off | ||
*/ | ||
function stripShebang(content) { |
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.
stripHashBang()
would be clearer
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.
Shebang is more commonly used. For example, the Hashbang article on Wikipedia redirects to the Shebang article.
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.
As a single data point: I've been a UNIX user for 20+ years and this is the first time I've heard someone call it a hashbang.
// Remove shebang | ||
var contLen = content.length; | ||
if (contLen >= 2) { | ||
if (content.charCodeAt(0) === 35/*#*/ && |
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.
does this lint? I would expect a space after 35
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.
Yes it does. This code originates from lib/module.js
(see master branch)
The implementation looks good to me, but I think this should have a regression test for the issue in #12180. |
Ping @seppevs – do you think you could add a test here? or, the other way around: @not-an-aardvark would you be okay with landing this and keeping #12180 open until we have a test? It would seem like an acceptable second or slightly adventurous first contribution for next week’s Code & Learn. |
Sounds good to me. |
Landed in f971566 |
When loading a module, Node needs to finds the end of a shebang comment by searching for a \r or \n character. This behaviour is now standardized into a dedicated internal module function Refs: #12180 PR-URL: #12202 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
Opting to leave to bake until this has tests. |
When loading a module, Node needs to finds the end of a shebang comment by searching for a
\r
or\n
character. This behaviour is now standardized into a dedicated internal module functionRefs: #12180
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
module