[ruby/prism] Do not warn \r in shebang on windows

https://github.com/ruby/prism/commit/e8c862ca1f
This commit is contained in:
Kevin Newton 2024-09-11 16:47:14 -04:00 committed by git
parent a542479a75
commit 15135030e5
2 changed files with 42 additions and 35 deletions

View File

@ -21772,6 +21772,9 @@ pm_strnstr(const char *big, const char *little, size_t big_length) {
return NULL; return NULL;
} }
#ifdef _WIN32
#define pm_parser_warn_shebang_carriage_return(parser, start, length) ((void) 0)
#else
/** /**
* Potentially warn the user if the shebang that has been found to include * Potentially warn the user if the shebang that has been found to include
* "ruby" has a carriage return at the end, as that can cause problems on some * "ruby" has a carriage return at the end, as that can cause problems on some
@ -21783,6 +21786,7 @@ pm_parser_warn_shebang_carriage_return(pm_parser_t *parser, const uint8_t *start
pm_parser_warn(parser, start, start + length, PM_WARN_SHEBANG_CARRIAGE_RETURN); pm_parser_warn(parser, start, start + length, PM_WARN_SHEBANG_CARRIAGE_RETURN);
} }
} }
#endif
/** /**
* Process the shebang when initializing the parser. This function assumes that * Process the shebang when initializing the parser. This function assumes that
@ -21973,9 +21977,7 @@ pm_parser_init(pm_parser_t *parser, const uint8_t *source, size_t size, const pm
const char *engine; const char *engine;
if ((engine = pm_strnstr((const char *) parser->start, "ruby", length)) != NULL) { if ((engine = pm_strnstr((const char *) parser->start, "ruby", length)) != NULL) {
if (newline != NULL) { if (newline != NULL) {
size_t length_including_newline = length + 1; pm_parser_warn_shebang_carriage_return(parser, parser->start, length + 1);
pm_parser_warn_shebang_carriage_return(parser, parser->start, length_including_newline);
parser->encoding_comment_start = newline + 1; parser->encoding_comment_start = newline + 1;
} }
@ -22015,10 +22017,9 @@ pm_parser_init(pm_parser_t *parser, const uint8_t *source, size_t size, const pm
const char *engine; const char *engine;
if ((engine = pm_strnstr((const char *) cursor, "ruby", length)) != NULL) { if ((engine = pm_strnstr((const char *) cursor, "ruby", length)) != NULL) {
found_shebang = true; found_shebang = true;
if (newline != NULL) {
size_t length_including_newline = length + 1;
pm_parser_warn_shebang_carriage_return(parser, cursor, length_including_newline);
if (newline != NULL) {
pm_parser_warn_shebang_carriage_return(parser, cursor, length + 1);
parser->encoding_comment_start = newline + 1; parser->encoding_comment_start = newline + 1;
} }

View File

@ -321,6 +321,11 @@ module Prism
assert_warning("tap { redo; foo }", "statement not reached") assert_warning("tap { redo; foo }", "statement not reached")
end end
if RbConfig::CONFIG["host_os"].match?(/bccwin|cygwin|djgpp|mingw|mswin|wince/i)
def test_shebang_ending_with_carriage_return
refute_warning("#!ruby\r\np(123)\n", compare: false)
end
else
def test_shebang_ending_with_carriage_return def test_shebang_ending_with_carriage_return
msg = "shebang line ending with \\r may cause problems" msg = "shebang line ending with \\r may cause problems"
@ -358,6 +363,7 @@ module Prism
# https://bugs.ruby-lang.org/issues/20700 # https://bugs.ruby-lang.org/issues/20700
refute_warning("#!ruby\r", compare: false) refute_warning("#!ruby\r", compare: false)
end end
end
def test_warnings_verbosity def test_warnings_verbosity
warning = Prism.parse("def foo; END { }; end").warnings.first warning = Prism.parse("def foo; END { }; end").warnings.first