Skip to content

Commit b239e89

Browse files
committed
Merge pull request #129 from Coacher/master
Allow autoload functions omit an explicit scope
2 parents 621dfcf + ac97657 commit b239e89

File tree

10 files changed

+104
-9
lines changed

10 files changed

+104
-9
lines changed

test/asserting/policy.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from pprint import pprint
44
from compat.itertools import zip_longest
55
from vint.linting.linter import Linter
6+
from vint.linting.config.config_default_source import ConfigDefaultSource
67

78

89
class PolicyAssertion(unittest.TestCase):
@@ -22,11 +23,18 @@ def update_by_config(self, policy_enabling_map):
2223
class StubConfigContainer(object):
2324
def __init__(self, policy_names_to_enable):
2425

25-
policy_enabling_map = dict((policy_name, {'enabled': True})
26-
for policy_name in policy_names_to_enable)
26+
default_config_dict = ConfigDefaultSource(None).get_config_dict()
27+
policy_options = default_config_dict.get('policies', {})
28+
29+
for policy, options in policy_options.items():
30+
options['enabled'] = False
31+
32+
for policy in policy_names_to_enable:
33+
options = policy_options.setdefault(policy, {})
34+
options['enabled'] = True
2735

2836
self._config_dict = {
29-
'policies': policy_enabling_map
37+
'policies': policy_options,
3038
}
3139

3240

@@ -39,17 +47,20 @@ def get_config_dict(self):
3947
return self._config_dict
4048

4149

42-
def assertFoundNoViolations(self, path, Policy):
43-
self.assertFoundViolationsEqual(path, Policy, [])
50+
def assertFoundNoViolations(self, path, Policy, policy_options=None):
51+
self.assertFoundViolationsEqual(path, Policy, [], policy_options)
4452

4553

46-
def assertFoundViolationsEqual(self, path, Policy, expected_violations):
54+
def assertFoundViolationsEqual(self, path, Policy, expected_violations, policy_options=None):
4755
policy_to_test = Policy()
4856
policy_name = Policy.__name__
4957

5058
policy_set = PolicyAssertion.StubPolicySet(policy_to_test)
5159
config = PolicyAssertion.StubConfigContainer(policy_name)
5260

61+
if policy_options is not None:
62+
config.get_config_dict()['policies'][policy_name].update(policy_options)
63+
5364
linter = Linter(policy_set, config.get_config_dict())
5465
violations = linter.lint_file(path)
5566

test/fixture/policy/prohibit_implicit_scope_variable_invalid.vim

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,9 @@ function! ImplicitGlobalFunc(param)
1111
" Make fix missing a: easy
1212
echo param
1313
endfunction
14+
call ImplicitGlobalFunc(0)
15+
16+
function! autoload#ImplicitGlobalAutoloadFunc(param)
17+
echo autoload#AnotherImplicitGlobalAutoloadFunc(param)
18+
endfunction
19+
call autoload#ImplicitGlobalAutoloadFunc(1)

test/fixture/policy/prohibit_implicit_scope_variable_valid.vim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ function! s:ScriptLocalFunc()
3434
endfunction
3535
call s:ScriptLocalFunc()
3636

37+
function! g:autoload#AutoloadFunc()
38+
endfunction
39+
call g:autoload#AutoloadFunc()
40+
3741
" We can call buffer/window/tab local function references
3842
call b:BufferLocalFunc()
3943
call w:WindowLocalFunc()

test/integration/vint/linting/policy/test_prohibit_implicit_scope_variable.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,33 @@ def test_get_violation_if_found_when_file_is_invalid(self):
3333
self.create_violation(8, 5),
3434
self.create_violation(10, 11),
3535
self.create_violation(12, 10),
36+
self.create_violation(14, 6),
37+
self.create_violation(16, 11),
38+
self.create_violation(17, 10),
39+
self.create_violation(17, 53),
40+
self.create_violation(19, 6),
3641
]
3742

3843
self.assertFoundViolationsEqual(PATH_INVALID_VIM_SCRIPT,
3944
ProhibitImplicitScopeVariable,
4045
expected_violations)
4146

47+
48+
def test_get_violation_if_found_when_autoloads_are_suppressed(self):
49+
expected_violations = [
50+
self.create_violation(2, 5),
51+
self.create_violation(4, 10),
52+
self.create_violation(8, 5),
53+
self.create_violation(10, 11),
54+
self.create_violation(12, 10),
55+
self.create_violation(14, 6),
56+
self.create_violation(17, 53),
57+
]
58+
59+
self.assertFoundViolationsEqual(PATH_INVALID_VIM_SCRIPT,
60+
ProhibitImplicitScopeVariable,
61+
expected_violations,
62+
{'suppress_autoload': True})
63+
4264
if __name__ == '__main__':
4365
unittest.main()

test/unit/vint/linting/policy/test_abstract_policy.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,33 @@ def test_create_violation_report(self):
4242
policy.create_violation_report(node, env),
4343
expected_violation)
4444

45+
def test_get_policy_options(self):
46+
policy = ConcretePolicy()
47+
48+
expected_options = {}
49+
lint_context = {
50+
'config': {},
51+
}
52+
self.assertEqual(
53+
policy.get_policy_options(lint_context),
54+
expected_options)
55+
56+
expected_options = {}
57+
lint_context = {
58+
'config': {'policies': {}},
59+
}
60+
self.assertEqual(
61+
policy.get_policy_options(lint_context),
62+
expected_options)
63+
64+
expected_options = {'someoption': True}
65+
lint_context = {
66+
'config': {'policies': {'ConcretePolicy': expected_options}},
67+
}
68+
self.assertEqual(
69+
policy.get_policy_options(lint_context),
70+
expected_options)
71+
4572

4673
if __name__ == '__main__':
4774
unittest.main()

vint/asset/default_config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ cmdargs:
44
error-limit: 50
55

66
policies:
7+
ProhibitImplicitScopeVariable:
8+
enabled: yes
9+
suppress_autoload: no
10+
711
# Experimental
812
ProhibitUnusedVariable:
913
enabled: no

vint/ast/plugin/scope_plugin/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
get_explicity_of_scope_visibility as _get_explicity_of_scope_visibility,
1313
normalize_variable_name as _normalize_variable_name,
1414
)
15+
from vint.ast.plugin.scope_plugin.identifier_classifier import (
16+
is_autoload_identifier as _is_autoload_identifier,
17+
)
1518

1619

1720
# Expose to out of ScopePlugin
@@ -45,6 +48,10 @@ def is_unused_declarative_identifier(self, node):
4548
and not _is_referenced_declarative_identifier(node)
4649

4750

51+
def is_autoload_identifier(self, node):
52+
return _is_autoload_identifier(node)
53+
54+
4855
def get_objective_scope_visibility(self, node):
4956
link_registry = self._get_link_registry()
5057
context_scope = link_registry.get_context_scope_by_identifier(node)

vint/linting/linter.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ def lint_file(self, path):
116116
'root_node': root_ast,
117117
'stack_trace': [],
118118
'plugins': self._plugins,
119+
'config': self._config.get_config_dict(),
119120
}
120121

121122
traverse(root_ast,

vint/linting/policy/abstract_policy.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,8 @@ def get_violation_if_found(self, node, lint_context):
3939
return None
4040

4141
return self.create_violation_report(node, lint_context)
42+
43+
44+
def get_policy_options(self, lint_context):
45+
policy_section = lint_context['config'].get('policies', {})
46+
return policy_section.get(self.name, {})

vint/linting/policy/prohibit_implicit_scope_variable.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class ProhibitImplicitScopeVariable(AbstractPolicy):
1010
def __init__(self):
1111
super(ProhibitImplicitScopeVariable, self).__init__()
12-
self.reference = 'Anti-pattern of vimrc (Scope of variable)'
12+
self.reference = 'Anti-pattern of vimrc (Scope of identifier)'
1313
self.level = Level.STYLE_PROBLEM
1414

1515

@@ -22,10 +22,18 @@ def is_valid(self, identifier, lint_context):
2222

2323
scope_plugin = lint_context['plugins']['scope']
2424
explicity = scope_plugin.get_explicity_of_scope_visibility(identifier)
25+
is_autoload = scope_plugin.is_autoload_identifier(identifier)
2526

26-
self._make_description(identifier, scope_plugin)
27+
policy_options = self.get_policy_options(lint_context)
28+
suppress_autoload = policy_options['suppress_autoload']
2729

28-
return explicity is not ExplicityOfScopeVisibility.IMPLICIT
30+
is_valid = (explicity is not ExplicityOfScopeVisibility.IMPLICIT or
31+
is_autoload and suppress_autoload)
32+
33+
if not is_valid:
34+
self._make_description(identifier, scope_plugin)
35+
36+
return is_valid
2937

3038

3139
def _make_description(self, identifier, scope_plugin):

0 commit comments

Comments
 (0)