Skip to content

Commit eb8efa4

Browse files
kddnewtonmatzbot
authored andcommitted
[ruby/prism] shareable_constant_value line warnings
ruby/prism@8c984b6922
1 parent e1e6b49 commit eb8efa4

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

prism/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ warnings:
290290
- KEYWORD_EOL
291291
- LITERAL_IN_CONDITION_DEFAULT
292292
- LITERAL_IN_CONDITION_VERBOSE
293+
- SHAREABLE_CONSTANT_VALUE_LINE
293294
- SHEBANG_CARRIAGE_RETURN
294295
- UNEXPECTED_CARRIAGE_RETURN
295296
- UNREACHABLE_STATEMENT

prism/prism.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8394,7 +8394,12 @@ parser_lex_magic_comment(pm_parser_t *parser, bool semantic_token_seen) {
83948394
// If we have hit a ractor pragma, attempt to lex that.
83958395
uint32_t value_length = (uint32_t) (value_end - value_start);
83968396
if (key_length == 24 && pm_strncasecmp(key_source, (const uint8_t *) "shareable_constant_value", 24) == 0) {
8397-
if (value_length == 4 && pm_strncasecmp(value_start, (const uint8_t *) "none", 4) == 0) {
8397+
const uint8_t *cursor = parser->current.start;
8398+
while ((cursor > parser->start) && ((cursor[-1] == ' ') || (cursor[-1] == '\t'))) cursor--;
8399+
8400+
if (!((cursor == parser->start) || (cursor[-1] == '\n'))) {
8401+
pm_parser_warn_token(parser, &parser->current, PM_WARN_SHAREABLE_CONSTANT_VALUE_LINE);
8402+
} else if (value_length == 4 && pm_strncasecmp(value_start, (const uint8_t *) "none", 4) == 0) {
83988403
pm_parser_scope_shareable_constant_set(parser, PM_SCOPE_SHAREABLE_CONSTANT_NONE);
83998404
} else if (value_length == 7 && pm_strncasecmp(value_start, (const uint8_t *) "literal", 7) == 0) {
84008405
pm_parser_scope_shareable_constant_set(parser, PM_SCOPE_SHAREABLE_CONSTANT_LITERAL);

prism/templates/src/diagnostic.c.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ static const pm_diagnostic_data_t diagnostic_messages[PM_DIAGNOSTIC_ID_MAX] = {
373373
[PM_WARN_KEYWORD_EOL] = { "`%.*s` at the end of line without an expression", PM_WARNING_LEVEL_VERBOSE },
374374
[PM_WARN_LITERAL_IN_CONDITION_DEFAULT] = { "%sliteral in %s", PM_WARNING_LEVEL_DEFAULT },
375375
[PM_WARN_LITERAL_IN_CONDITION_VERBOSE] = { "%sliteral in %s", PM_WARNING_LEVEL_VERBOSE },
376+
[PM_WARN_SHAREABLE_CONSTANT_VALUE_LINE] = { "'shareable_constant_value' is ignored unless in comment-only line", PM_WARNING_LEVEL_VERBOSE },
376377
[PM_WARN_SHEBANG_CARRIAGE_RETURN] = { "shebang line ending with \\r may cause problems", PM_WARNING_LEVEL_DEFAULT },
377378
[PM_WARN_UNEXPECTED_CARRIAGE_RETURN] = { "encountered \\r in middle of line, treated as a mere space", PM_WARNING_LEVEL_DEFAULT },
378379
[PM_WARN_UNREACHABLE_STATEMENT] = { "statement not reached", PM_WARNING_LEVEL_VERBOSE },

test/prism/warnings_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ def test_keyword_eol
6464
assert_warning("if true\nelsif\nfalse; end", "end of line")
6565
end
6666

67+
def test_shareable_constant_value
68+
assert_warning("foo # shareable_constant_value: none", "ignored")
69+
assert_warning("\v # shareable_constant_value: none", "ignored")
70+
71+
refute_warning("# shareable_constant_value: none")
72+
refute_warning(" # shareable_constant_value: none")
73+
refute_warning("\t\t# shareable_constant_value: none")
74+
end
75+
6776
def test_string_in_predicate
6877
assert_warning("if 'foo'; end", "string")
6978
assert_warning("if \"\#{foo}\"; end", "string")

0 commit comments

Comments
 (0)