Class: StandardNaming

Inherits:
GrammarLinter show all
Defined in:
lib/ruby_grammar_builder/linters/standard_naming.rb

Overview

Checks for names to match expected naming format see www.sublimetext.com/docs/3/scope_naming.html

Constant Summary collapse

EXPECTED_NAMES =

Returns a summarization of expected names.

Returns:

  • (Hash)

    a summarization of expected names

{
    "comment" => {
        "line" => true,
        "block" => true,
    }.freeze,
    "constant" => {
        "numeric" => true,
        "language" => true,
        "character" => {"escape" => true}.freeze,
        "other" => true,
    }.freeze,
    "entity" => {
        "name" => {
            "class" => true,
            "struct" => true,
            "enum" => true,
            "union" => true,
            "trait" => true,
            "interface" => true,
            "impl" => true,
            "type" => true,
            "function" => true,
            "namespace" => true,
            "constant" => true,
            "label" => true,
            "section" => true,
            "scope-resolution" => true,
            "tag" => true,
            "attribute-name" => true,
            "other" => true,
        }.freeze,
        "other" => true,
    }.freeze,
    "invalid" => {
        "illegal" => true,
        "deprecated" => true,
        "unknown" => true,
    }.freeze,
    "keyword" => {
        "control" => true,
        "operator" => true,
        "other" => true,
        "declaration" => true,
    }.freeze,
    "markup" => {
        "heading" => true,
        "list" => {
            "numbered" => true,
            "unnumbered" => true,
        }.freeze,
        "bold" => true,
        "italic" => true,
        "inline" => true,
        "underline" => true,
        "inserted" => true,
        "deleted" => true,
        "quote" => true,
        "raw" => {
            "inline" => true,
            "block" => true,
        }.freeze,
        "other" => true,
    }.freeze,
    "meta" => {
        "asm" => true,
        "class" => true,
        "struct" => true,
        "enum" => true,
        "union" => true,
        "trait" => true,
        "interface" => true,
        "declaration" => true,
        "impl" => true,
        "initialization" => true,
        "type" => true,
        "qualified_type" => true,
        "function" => true,
        "parameter" => true,
        "namespace" => true,
        "using-namespace" => true,
        "preprocessor" => true,
        "conditional" => true,
        "annotation" => true,
        "path" => true,
        "function-call" => true,
        "block" => true,
        "group" => true,
        "braces" => true,
        "parens" => true,
        "brackets" => true,
        "generic" => true,
        "template" => true,
        "tag" => true,
        "paragraph" => true,
        "string" => true,
        "interpolation" => true,
        "toc-list" => true,
        "banner" => true,
    }.freeze,
    "punctuation" => {
        "definition" => true,
        "separator" => true,
        "terminator" => true,
        "accessor" => true,
        "section" => true,
        "vararg-ellipses" => true,
    }.freeze,
    "source" => true,
    "storage" => {
        "type" => true,
        "modifier" => true,
    }.freeze,
    "string" => {
        "quoted" => {
            "single" => true,
            "double" => true,
            "other" => true,
        }.freeze,
        "unquoted" => true,
        "regexp" => true,
    }.freeze,
    "support" => {
        "constant" => true,
        "function" => true,
        "module" => true,
        "type" => true,
        "class" => true,
        "other" => true,
    }.freeze,
    "text" => true,
    "variable" => {
        "other" => true,
        "language" => true,
        "function" => true,
        "annotation" => true,
        "parameter" => true,
    }.freeze,
}.freeze

Instance Method Summary collapse

Methods inherited from GrammarLinter

#post_lint

Methods inherited from GrammarPlugin

display_options, options

Instance Method Details

#check_tag(tag) ⇒ void

This method returns an undefined value.

Checks a tag for standard naming scheme

Parameters:

  • tag (String)

    the tag to check



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/ruby_grammar_builder/linters/standard_naming.rb', line 191

def check_tag(tag)
    result, pos, root = recursive_check_tag(tag)
    return if result

    valid_prefix = (pos > 0) ? tag[0..(pos-1)].join(".") + "." : ""

    joined_tag = "#{tag[0..pos].join('.')}"
    if joined_tag.start_with? "meta."
        return
    end
    puts "The prefix `#{joined_tag}' does not follow the standard format"
    puts "The expected prefixes at this level are:"
    root.keys.each do |key|
        if root[:key] == false
            puts "- #{valid_prefix}#{key}"
        else
            puts "  #{valid_prefix}#{key}"
        end
    end
end

#pre_lint(pattern, _options) ⇒ True

Checks for names to match expected naming format

Returns:

  • (True)

    warnings to not return false



217
218
219
220
221
222
223
224
225
226
227
# File 'lib/ruby_grammar_builder/linters/standard_naming.rb', line 217

def pre_lint(pattern, _options)
    return true unless pattern.is_a? PatternBase

    pattern.each(true) do |pat|
        next unless pat.arguments[:tag_as]

        pat.arguments[:tag_as].split(" ").each { |tag| check_tag(tag.split(".")) }
    end

    true
end

#recursive_check_tag(tag, index = 0, root = EXPECTED_NAMES) ⇒ Boolean

Checks the tag keys at this level

Parameters:

  • tag (Array<string>)

    an array of tag components

  • index (Numeric) (defaults to: 0)

    The index into tag to check

  • root (Hash) (defaults to: EXPECTED_NAMES)

    the hash to check against

Returns:

  • (Boolean)

    If this is a valid tag



171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/ruby_grammar_builder/linters/standard_naming.rb', line 171

def recursive_check_tag(tag, index = 0, root = EXPECTED_NAMES)
    if root.has_key?(tag[index])
        next_part = root[tag[index]]
        return recursive_check_tag(tag, index+1, next_part) if next_part.is_a? Hash

        return [next_part, index, root]
    elsif root.has_key? "*"
        return [root["*"], index, root]
    end

    [false, index, root]
end