-
Notifications
You must be signed in to change notification settings - Fork 30k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
exports = module.exports = { | ||
makeRequireFunction, | ||
stripBOM, | ||
stripShebang, | ||
addBuiltinLibsToObject | ||
}; | ||
|
||
|
@@ -50,6 +51,40 @@ function stripBOM(content) { | |
return content; | ||
} | ||
|
||
/** | ||
* Find end of shebang line and slice it off | ||
*/ | ||
function stripShebang(content) { | ||
// 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 commentThe reason will be displayed to describe this comment to others. Learn more. does this lint? I would expect a space after There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes it does. This code originates from |
||
content.charCodeAt(1) === 33/*!*/) { | ||
if (contLen === 2) { | ||
// Exact match | ||
content = ''; | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could just drop the |
||
// Find end of shebang line and slice it off | ||
var i = 2; | ||
for (; i < contLen; ++i) { | ||
var code = content.charCodeAt(i); | ||
if (code === 10/*\n*/ || code === 13/*\r*/) | ||
break; | ||
} | ||
if (i === contLen) | ||
content = ''; | ||
else { | ||
// Note that this actually includes the newline character(s) in the | ||
// new output. This duplicates the behavior of the regular expression | ||
// that was previously used to replace the shebang line | ||
content = content.slice(i); | ||
} | ||
} | ||
} | ||
} | ||
return content; | ||
} | ||
|
||
exports.builtinLibs = [ | ||
'assert', 'buffer', 'child_process', 'cluster', 'crypto', 'dgram', 'dns', | ||
'domain', 'events', 'fs', 'http', 'https', 'net', 'os', 'path', 'punycode', | ||
|
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 clearerThere 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.