[ruby/prism] shareable_constant_value line warnings

8c984b6922
This commit is contained in:
Kevin Newton 2024-05-07 09:53:17 -04:00 committed by git
parent e1e6b4972f
commit eb8efa42f0
4 changed files with 17 additions and 1 deletions

View file

@ -290,6 +290,7 @@ warnings:
- KEYWORD_EOL - KEYWORD_EOL
- LITERAL_IN_CONDITION_DEFAULT - LITERAL_IN_CONDITION_DEFAULT
- LITERAL_IN_CONDITION_VERBOSE - LITERAL_IN_CONDITION_VERBOSE
- SHAREABLE_CONSTANT_VALUE_LINE
- SHEBANG_CARRIAGE_RETURN - SHEBANG_CARRIAGE_RETURN
- UNEXPECTED_CARRIAGE_RETURN - UNEXPECTED_CARRIAGE_RETURN
- UNREACHABLE_STATEMENT - UNREACHABLE_STATEMENT

View file

@ -8394,7 +8394,12 @@ parser_lex_magic_comment(pm_parser_t *parser, bool semantic_token_seen) {
// If we have hit a ractor pragma, attempt to lex that. // If we have hit a ractor pragma, attempt to lex that.
uint32_t value_length = (uint32_t) (value_end - value_start); uint32_t value_length = (uint32_t) (value_end - value_start);
if (key_length == 24 && pm_strncasecmp(key_source, (const uint8_t *) "shareable_constant_value", 24) == 0) { if (key_length == 24 && pm_strncasecmp(key_source, (const uint8_t *) "shareable_constant_value", 24) == 0) {
if (value_length == 4 && pm_strncasecmp(value_start, (const uint8_t *) "none", 4) == 0) { const uint8_t *cursor = parser->current.start;
while ((cursor > parser->start) && ((cursor[-1] == ' ') || (cursor[-1] == '\t'))) cursor--;
if (!((cursor == parser->start) || (cursor[-1] == '\n'))) {
pm_parser_warn_token(parser, &parser->current, PM_WARN_SHAREABLE_CONSTANT_VALUE_LINE);
} else if (value_length == 4 && pm_strncasecmp(value_start, (const uint8_t *) "none", 4) == 0) {
pm_parser_scope_shareable_constant_set(parser, PM_SCOPE_SHAREABLE_CONSTANT_NONE); pm_parser_scope_shareable_constant_set(parser, PM_SCOPE_SHAREABLE_CONSTANT_NONE);
} else if (value_length == 7 && pm_strncasecmp(value_start, (const uint8_t *) "literal", 7) == 0) { } else if (value_length == 7 && pm_strncasecmp(value_start, (const uint8_t *) "literal", 7) == 0) {
pm_parser_scope_shareable_constant_set(parser, PM_SCOPE_SHAREABLE_CONSTANT_LITERAL); pm_parser_scope_shareable_constant_set(parser, PM_SCOPE_SHAREABLE_CONSTANT_LITERAL);

View file

@ -373,6 +373,7 @@ static const pm_diagnostic_data_t diagnostic_messages[PM_DIAGNOSTIC_ID_MAX] = {
[PM_WARN_KEYWORD_EOL] = { "`%.*s` at the end of line without an expression", PM_WARNING_LEVEL_VERBOSE }, [PM_WARN_KEYWORD_EOL] = { "`%.*s` at the end of line without an expression", PM_WARNING_LEVEL_VERBOSE },
[PM_WARN_LITERAL_IN_CONDITION_DEFAULT] = { "%sliteral in %s", PM_WARNING_LEVEL_DEFAULT }, [PM_WARN_LITERAL_IN_CONDITION_DEFAULT] = { "%sliteral in %s", PM_WARNING_LEVEL_DEFAULT },
[PM_WARN_LITERAL_IN_CONDITION_VERBOSE] = { "%sliteral in %s", PM_WARNING_LEVEL_VERBOSE }, [PM_WARN_LITERAL_IN_CONDITION_VERBOSE] = { "%sliteral in %s", PM_WARNING_LEVEL_VERBOSE },
[PM_WARN_SHAREABLE_CONSTANT_VALUE_LINE] = { "'shareable_constant_value' is ignored unless in comment-only line", PM_WARNING_LEVEL_VERBOSE },
[PM_WARN_SHEBANG_CARRIAGE_RETURN] = { "shebang line ending with \\r may cause problems", PM_WARNING_LEVEL_DEFAULT }, [PM_WARN_SHEBANG_CARRIAGE_RETURN] = { "shebang line ending with \\r may cause problems", PM_WARNING_LEVEL_DEFAULT },
[PM_WARN_UNEXPECTED_CARRIAGE_RETURN] = { "encountered \\r in middle of line, treated as a mere space", PM_WARNING_LEVEL_DEFAULT }, [PM_WARN_UNEXPECTED_CARRIAGE_RETURN] = { "encountered \\r in middle of line, treated as a mere space", PM_WARNING_LEVEL_DEFAULT },
[PM_WARN_UNREACHABLE_STATEMENT] = { "statement not reached", PM_WARNING_LEVEL_VERBOSE }, [PM_WARN_UNREACHABLE_STATEMENT] = { "statement not reached", PM_WARNING_LEVEL_VERBOSE },

View file

@ -64,6 +64,15 @@ module Prism
assert_warning("if true\nelsif\nfalse; end", "end of line") assert_warning("if true\nelsif\nfalse; end", "end of line")
end end
def test_shareable_constant_value
assert_warning("foo # shareable_constant_value: none", "ignored")
assert_warning("\v # shareable_constant_value: none", "ignored")
refute_warning("# shareable_constant_value: none")
refute_warning(" # shareable_constant_value: none")
refute_warning("\t\t# shareable_constant_value: none")
end
def test_string_in_predicate def test_string_in_predicate
assert_warning("if 'foo'; end", "string") assert_warning("if 'foo'; end", "string")
assert_warning("if \"\#{foo}\"; end", "string") assert_warning("if \"\#{foo}\"; end", "string")