Skip to content

Commit

Permalink
Make test pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuniwak committed Nov 12, 2017
1 parent 2c453ee commit a0ca3c9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
19 changes: 19 additions & 0 deletions test/unit/vint/ast/plugin/test_autocmd_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,132 +20,151 @@ def test_parse_autocmd(self):
'pat': None,
'nested': False,
'cmd': None,
'bang': False,
}),
(create_autocmd_node('autocmd!'), {
'group': None,
'event': [],
'pat': None,
'nested': False,
'cmd': None,
'bang': True,
}),
(create_autocmd_node('autocmd FileType'), {
'group': None,
'event': [AutocmdEvents.FILE_TYPE],
'pat': None,
'nested': False,
'cmd': None,
'bang': False,
}),
(create_autocmd_node('autocmd BufNew,BufRead'), {
'group': None,
'event': [AutocmdEvents.BUF_NEW, AutocmdEvents.BUF_READ],
'pat': None,
'nested': False,
'cmd': None,
'bang': False,
}),
(create_autocmd_node('autocmd! FileType'), {
'group': None,
'event': [AutocmdEvents.FILE_TYPE],
'pat': None,
'nested': False,
'cmd': None,
'bang': True,
}),
(create_autocmd_node('autocmd FileType *'), {
'group': None,
'event': [AutocmdEvents.FILE_TYPE],
'pat': '*',
'nested': False,
'cmd': None,
'bang': False,
}),
(create_autocmd_node('autocmd! FileType *'), {
'group': None,
'event': [AutocmdEvents.FILE_TYPE],
'pat': '*',
'nested': False,
'cmd': None,
'bang': True,
}),
(create_autocmd_node('autocmd FileType * nested :echo'), {
'group': None,
'event': [AutocmdEvents.FILE_TYPE],
'pat': '*',
'nested': True,
'cmd': create_echo_cmd_node(),
'bang': False,
}),
(create_autocmd_node('autocmd! FileType * nested :echo'), {
'group': None,
'event': [AutocmdEvents.FILE_TYPE],
'pat': '*',
'nested': True,
'cmd': create_echo_cmd_node(),
'bang': True,
}),
(create_autocmd_node('autocmd Group'), {
'group': 'Group',
'event': [],
'pat': None,
'nested': False,
'cmd': None,
'bang': False,
}),
(create_autocmd_node('autocmd! Group'), {
'group': 'Group',
'event': [],
'pat': None,
'nested': False,
'cmd': None,
'bang': True,
}),
(create_autocmd_node('autocmd Group *'), {
'group': 'Group',
'event': [AutocmdEvents.ANY],
'pat': None,
'nested': False,
'cmd': None,
'bang': False,
}),
(create_autocmd_node('autocmd! Group *'), {
'group': 'Group',
'event': [AutocmdEvents.ANY],
'pat': None,
'nested': False,
'cmd': None,
'bang': True,
}),
(create_autocmd_node('autocmd Group FileType'), {
'group': 'Group',
'event': [AutocmdEvents.FILE_TYPE],
'pat': None,
'nested': False,
'cmd': None,
'bang': False,
}),
(create_autocmd_node('autocmd! Group FileType'), {
'group': 'Group',
'event': [AutocmdEvents.FILE_TYPE],
'pat': None,
'nested': False,
'cmd': None,
'bang': True,
}),
(create_autocmd_node('autocmd Group FileType *'), {
'group': 'Group',
'event': [AutocmdEvents.FILE_TYPE],
'pat': '*',
'nested': False,
'cmd': None,
'bang': False,
}),
(create_autocmd_node('autocmd! Group FileType *'), {
'group': 'Group',
'event': [AutocmdEvents.FILE_TYPE],
'pat': '*',
'nested': False,
'cmd': None,
'bang': True,
}),
(create_autocmd_node('autocmd Group FileType * nested :echo'), {
'group': 'Group',
'event': [AutocmdEvents.FILE_TYPE],
'pat': '*',
'nested': True,
'cmd': create_echo_cmd_node(),
'bang': False,
}),
(create_autocmd_node('autocmd! Group FileType * nested :echo'), {
'group': 'Group',
'event': [AutocmdEvents.FILE_TYPE],
'pat': '*',
'nested': True,
'cmd': create_echo_cmd_node(),
'bang': True,
}),
]

Expand Down
16 changes: 7 additions & 9 deletions vint/ast/plugin/autocmd_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,20 @@ def parse_autocmd(autocmd_node):
'pat': None,
'nested': False,
'cmd': None,
'bang': False
}

# type: str
autocmd_str = autocmd_node.get('str')

# This tokens may be broken, because the cmd part can have
# whitespaces.
# type: [str]
tokens = autocmd_str.split(None, 2)

if len(tokens) > 0:
autocmd_info['bang'] = tokens[0].endswith('!')

if len(tokens) == 1:
# Examples:
# :au[tocmd][!]
Expand Down Expand Up @@ -167,12 +174,3 @@ def _parse_events(token):

def is_autocmd_event_like(token):
return all([is_autocmd_event(part) for part in token.split(',')])


@register_traverser_extension
def traverse_autocmd(node, on_enter=None, on_leave=None):
autocmd_content = get_autocmd_content(node)
if autocmd_content is None:
return

traverse(autocmd_content, on_enter=on_enter, on_leave=on_leave)
2 changes: 2 additions & 0 deletions vint/linting/linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from vint.ast.node_type import NodeType
from vint.ast.traversing import traverse
from vint.ast.plugin.scope_plugin import ScopePlugin
from vint.ast.plugin.autocmd_parser import AutocmdParser
from vint.linting.config.config_container import ConfigContainer
from vint.linting.config.config_dict_source import ConfigDictSource
from vint.linting.config.config_comment_source import ConfigCommentSource
Expand All @@ -32,6 +33,7 @@ class Linter(object):
def __init__(self, policy_set, config_dict_global):
self._plugins = {
'scope': ScopePlugin(),
'autocmd': AutocmdParser(),
}
self._policy_set = policy_set

Expand Down
16 changes: 10 additions & 6 deletions vint/linting/policy/prohibit_autocmd_with_no_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from vint.linting.policy.abstract_policy import AbstractPolicy
from vint.linting.policy_registry import register_policy
from vint.ast.dictionary.autocmd_events import AutocmdEvents
from vint.ast.plugin.autocmd_parser import AUTOCMD_CONTENT
from pprint import pprint


@register_policy
Expand All @@ -27,19 +29,21 @@ def is_valid(self, node, lint_context):
autocmd family should be called with any groups.
"""

# noed.ea.cmd is empty when line jump command such as 1
# node.ea.cmd is empty when line jump command such as 1
cmd_name = node['ea']['cmd'].get('name', None)

is_autocmd = cmd_name == 'autocmd'
if is_autocmd and not self.is_inside_of_augroup:
matched = re.match(r'au(?:tocmd)?!?\s+(\S+)', node['str'])

if not matched:
autocmd_attr = node[AUTOCMD_CONTENT]
if autocmd_attr['bang']:
# Looks like autocmd with a bang
return True

has_no_group = matched.group(1) in AutocmdEvents
return not has_no_group
has_group = autocmd_attr.get('group') is not None
if has_group:
return True

return self.is_inside_of_augroup

is_augroup = cmd_name == 'augroup'
if is_augroup:
Expand Down

0 comments on commit a0ca3c9

Please sign in to comment.