Skip to content

Commit

Permalink
Handle whitespace in shebangs
Browse files Browse the repository at this point in the history
  • Loading branch information
Wilfred committed Mar 5, 2024
1 parent 6ff603c commit 2477270
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ Updated JavaScript, TypeScript, QML and Perl parsers.

`Makefile*.in` is now detected as Makefile syntax.

Improved shebang parsing with whitespace, e.g. `#! /bin/bash` is now
detected as a shell script.

Added support for Scheme and Smali.

### Diffing
Expand Down
8 changes: 7 additions & 1 deletion src/parse/guess_language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ fn from_emacs_mode_header(src: &str) -> Option<Language> {
/// Try to guess the language based on a shebang present in the source.
fn from_shebang(src: &str) -> Option<Language> {
lazy_static! {
static ref RE: Regex = Regex::new(r"#!(?:/usr/bin/env )?([^ ]+)").unwrap();
static ref RE: Regex = Regex::new(r"#! *(?:/usr/bin/env )?([^ ]+)").unwrap();
}
if let Some(first_line) = src.lines().next() {
if let Some(cap) = RE.captures(first_line) {
Expand Down Expand Up @@ -635,6 +635,12 @@ mod tests {
assert_eq!(guess(path, "#!/usr/bin/env python", &[]), Some(Python));
}

#[test]
fn test_guess_by_shebang_with_space() {
let path = Path::new("foo");
assert_eq!(guess(path, "#! /bin/sh", &[]), Some(Bash));
}

#[test]
fn test_guess_by_emacs_mode() {
let path = Path::new("foo");
Expand Down

0 comments on commit 2477270

Please sign in to comment.