rubocop-sketchup 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +19 -19
  3. data/assets/output.html.erb +301 -301
  4. data/config/default.yml +355 -355
  5. data/lib/rubocop/sketchup/config.rb +63 -63
  6. data/lib/rubocop/sketchup/cop/deprecations/add_separator_to_menu.rb +25 -25
  7. data/lib/rubocop/sketchup/cop/deprecations/operation_next_transparent.rb +30 -30
  8. data/lib/rubocop/sketchup/cop/deprecations/require_all.rb +27 -27
  9. data/lib/rubocop/sketchup/cop/deprecations/set_texture_projection.rb +26 -26
  10. data/lib/rubocop/sketchup/cop/deprecations/show_ruby_panel.rb +25 -25
  11. data/lib/rubocop/sketchup/cop/deprecations/sketchup_set.rb +30 -30
  12. data/lib/rubocop/sketchup/cop/performance/openssl.rb +41 -41
  13. data/lib/rubocop/sketchup/cop/performance/operation_disable_ui.rb +33 -33
  14. data/lib/rubocop/sketchup/cop/performance/selection_bulk.rb +79 -79
  15. data/lib/rubocop/sketchup/cop/performance/type_check.rb +63 -63
  16. data/lib/rubocop/sketchup/cop/performance/typename.rb +24 -24
  17. data/lib/rubocop/sketchup/cop/requirements/api_namespace.rb +30 -30
  18. data/lib/rubocop/sketchup/cop/requirements/exit.rb +32 -32
  19. data/lib/rubocop/sketchup/cop/requirements/extension_namespace.rb +108 -108
  20. data/lib/rubocop/sketchup/cop/requirements/file_structure.rb +97 -97
  21. data/lib/rubocop/sketchup/cop/requirements/gem_install.rb +45 -45
  22. data/lib/rubocop/sketchup/cop/requirements/get_extension_license.rb +95 -95
  23. data/lib/rubocop/sketchup/cop/requirements/global_constants.rb +38 -38
  24. data/lib/rubocop/sketchup/cop/requirements/global_include.rb +42 -42
  25. data/lib/rubocop/sketchup/cop/requirements/global_methods.rb +65 -65
  26. data/lib/rubocop/sketchup/cop/requirements/global_variables.rb +95 -95
  27. data/lib/rubocop/sketchup/cop/requirements/language_handler_globals.rb +46 -46
  28. data/lib/rubocop/sketchup/cop/requirements/load_path.rb +83 -83
  29. data/lib/rubocop/sketchup/cop/requirements/minimal_registration.rb +73 -73
  30. data/lib/rubocop/sketchup/cop/requirements/observers_start_operation.rb +161 -161
  31. data/lib/rubocop/sketchup/cop/requirements/register_extension.rb +45 -45
  32. data/lib/rubocop/sketchup/cop/requirements/ruby_core_namespace.rb +291 -291
  33. data/lib/rubocop/sketchup/cop/requirements/ruby_stdlib_namespace.rb +634 -634
  34. data/lib/rubocop/sketchup/cop/requirements/shipped_extensions_namespace.rb +61 -61
  35. data/lib/rubocop/sketchup/cop/requirements/sketchup_extension.rb +119 -119
  36. data/lib/rubocop/sketchup/cop/requirements/sketchup_require.rb +163 -163
  37. data/lib/rubocop/sketchup/cop/suggestions/add_group.rb +49 -49
  38. data/lib/rubocop/sketchup/cop/suggestions/compatibility.rb +117 -117
  39. data/lib/rubocop/sketchup/cop/suggestions/dc_internals.rb +34 -34
  40. data/lib/rubocop/sketchup/cop/suggestions/file_encoding.rb +78 -78
  41. data/lib/rubocop/sketchup/cop/suggestions/model_entities.rb +58 -58
  42. data/lib/rubocop/sketchup/cop/suggestions/monkey_patched_api.rb +45 -45
  43. data/lib/rubocop/sketchup/cop/suggestions/operation_name.rb +103 -103
  44. data/lib/rubocop/sketchup/cop/suggestions/sketchup_find_support_file.rb +39 -39
  45. data/lib/rubocop/sketchup/cop/suggestions/tool_drawing_bounds.rb +44 -44
  46. data/lib/rubocop/sketchup/cop/suggestions/tool_invalidate.rb +66 -66
  47. data/lib/rubocop/sketchup/cop/suggestions/tool_user_input.rb +41 -41
  48. data/lib/rubocop/sketchup/cop/suggestions/toolbar_timer.rb +65 -65
  49. data/lib/rubocop/sketchup/cop.rb +111 -111
  50. data/lib/rubocop/sketchup/dc_globals.rb +24 -24
  51. data/lib/rubocop/sketchup/dc_methods.rb +130 -130
  52. data/lib/rubocop/sketchup/extension_project.rb +65 -65
  53. data/lib/rubocop/sketchup/features.rb +738 -738
  54. data/lib/rubocop/sketchup/formatter/extension_review.rb +259 -259
  55. data/lib/rubocop/sketchup/inject.rb +19 -19
  56. data/lib/rubocop/sketchup/namespace.rb +47 -47
  57. data/lib/rubocop/sketchup/namespace_checker.rb +46 -46
  58. data/lib/rubocop/sketchup/no_comment_disable.rb +17 -17
  59. data/lib/rubocop/sketchup/range_help.rb +52 -52
  60. data/lib/rubocop/sketchup/sketchup_version.rb +87 -87
  61. data/lib/rubocop/sketchup/tool_checker.rb +43 -43
  62. data/lib/rubocop/sketchup/version.rb +5 -5
  63. data/lib/rubocop/sketchup.rb +12 -12
  64. data/lib/rubocop-sketchup.rb +48 -48
  65. data/rubocop-sketchup.gemspec +27 -27
  66. metadata +4 -4
@@ -1,95 +1,95 @@
1
- # frozen_string_literal: true
2
-
3
- module RuboCop
4
- module Cop
5
- module SketchupRequirements
6
- # Don't attempt to kill the Ruby interpreter by calling `exit` or `exit!`.
7
- # SketchUp will trap `exit` and prevent that, with a message in the
8
- # console. But `exit!` is not trapped and with terminate SketchUp without
9
- # shutting down cleanly.
10
- #
11
- # Use `return`, `next`, `break` or `raise` instead.
12
- class GetExtensionLicense < SketchUp::Cop
13
-
14
- include RangeHelp
15
- include SketchUp::NoCommentDisable
16
-
17
- MSG_INVALID = 'Invalid extension GUID'.freeze
18
-
19
- MSG_WRONG_TYPE = 'Only pass in extension GUID from local string '\
20
- 'literals.'.freeze
21
-
22
- MSG_TRAILING_SPACE = 'Extra space in extension GUID'.freeze
23
-
24
- def_node_matcher :get_extension_license, <<-PATTERN
25
- (send
26
- (const
27
- (const nil? :Sketchup) :Licensing) :get_extension_license
28
- $_)
29
- PATTERN
30
-
31
- # rubocop:disable Metrics/LineLength
32
- EXTENSION_ID_PATTERN = /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/
33
- # rubocop:enable Metrics/LineLength
34
-
35
- def on_send(node)
36
- argument = get_extension_license(node)
37
- return unless argument
38
-
39
- if argument.lvar_type?
40
- variable_name = argument.children.first
41
- assignment_node = find_assignment(node, variable_name)
42
- argument = assignment_node.children.last
43
- end
44
-
45
- if argument.str_type?
46
- validate_extension_id(argument)
47
- else
48
- location = argument.loc.expression
49
- add_offense(node, location: location, message: MSG_WRONG_TYPE)
50
- end
51
- end
52
-
53
- private
54
-
55
- def find_assignment(node, variable_name)
56
- scope = node
57
- until scope.parent.nil?
58
- scope = scope.parent
59
- scope.each_child_node { |child|
60
- # next unless child.is_a?(RuboCop::AST::Node)
61
- next unless child.lvasgn_type?
62
- next unless child.children.first == variable_name
63
-
64
- return child
65
- }
66
- end
67
- nil
68
- end
69
-
70
- def validate_extension_id(node)
71
- extension_id = node.str_content
72
-
73
- # Trailing spaces
74
- if extension_id.rstrip.size < extension_id.size
75
- end_pos = node.loc.end.begin_pos
76
- begin_pos = node.loc.begin.end_pos + extension_id.rstrip.size
77
- range = range_between(begin_pos, end_pos)
78
- add_offense(node, location: range, message: MSG_TRAILING_SPACE)
79
- return false
80
- end
81
-
82
- # Invalid format.
83
- if extension_id !~ EXTENSION_ID_PATTERN
84
- range = string_contents_range(node)
85
- add_offense(node, location: range, message: MSG_INVALID)
86
- return false
87
- end
88
-
89
- true
90
- end
91
-
92
- end
93
- end
94
- end
95
- end
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module SketchupRequirements
6
+ # Don't attempt to kill the Ruby interpreter by calling `exit` or `exit!`.
7
+ # SketchUp will trap `exit` and prevent that, with a message in the
8
+ # console. But `exit!` is not trapped and with terminate SketchUp without
9
+ # shutting down cleanly.
10
+ #
11
+ # Use `return`, `next`, `break` or `raise` instead.
12
+ class GetExtensionLicense < SketchUp::Cop
13
+
14
+ include RangeHelp
15
+ include SketchUp::NoCommentDisable
16
+
17
+ MSG_INVALID = 'Invalid extension GUID'.freeze
18
+
19
+ MSG_WRONG_TYPE = 'Only pass in extension GUID from local string '\
20
+ 'literals.'.freeze
21
+
22
+ MSG_TRAILING_SPACE = 'Extra space in extension GUID'.freeze
23
+
24
+ def_node_matcher :get_extension_license, <<-PATTERN
25
+ (send
26
+ (const
27
+ (const nil? :Sketchup) :Licensing) :get_extension_license
28
+ $_)
29
+ PATTERN
30
+
31
+ # rubocop:disable Metrics/LineLength
32
+ EXTENSION_ID_PATTERN = /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/.freeze
33
+ # rubocop:enable Metrics/LineLength
34
+
35
+ def on_send(node)
36
+ argument = get_extension_license(node)
37
+ return unless argument
38
+
39
+ if argument.lvar_type?
40
+ variable_name = argument.children.first
41
+ assignment_node = find_assignment(node, variable_name)
42
+ argument = assignment_node.children.last
43
+ end
44
+
45
+ if argument.str_type?
46
+ validate_extension_id(argument)
47
+ else
48
+ location = argument.loc.expression
49
+ add_offense(node, location: location, message: MSG_WRONG_TYPE)
50
+ end
51
+ end
52
+
53
+ private
54
+
55
+ def find_assignment(node, variable_name)
56
+ scope = node
57
+ until scope.parent.nil?
58
+ scope = scope.parent
59
+ scope.each_child_node { |child|
60
+ # next unless child.is_a?(RuboCop::AST::Node)
61
+ next unless child.lvasgn_type?
62
+ next unless child.children.first == variable_name
63
+
64
+ return child
65
+ }
66
+ end
67
+ nil
68
+ end
69
+
70
+ def validate_extension_id(node)
71
+ extension_id = node.str_content
72
+
73
+ # Trailing spaces
74
+ if extension_id.rstrip.size < extension_id.size
75
+ end_pos = node.loc.end.begin_pos
76
+ begin_pos = node.loc.begin.end_pos + extension_id.rstrip.size
77
+ range = range_between(begin_pos, end_pos)
78
+ add_offense(node, location: range, message: MSG_TRAILING_SPACE)
79
+ return false
80
+ end
81
+
82
+ # Invalid format.
83
+ if extension_id !~ EXTENSION_ID_PATTERN
84
+ range = string_contents_range(node)
85
+ add_offense(node, location: range, message: MSG_INVALID)
86
+ return false
87
+ end
88
+
89
+ true
90
+ end
91
+
92
+ end
93
+ end
94
+ end
95
+ end
@@ -1,38 +1,38 @@
1
- # frozen_string_literal: true
2
-
3
- module RuboCop
4
- module Cop
5
- module SketchupRequirements
6
- # Extensions in SketchUp all share the same Ruby environment on the user's
7
- # machine. Because of this it's important that each extension isolate
8
- # itself to avoid clashing with other extensions.
9
- #
10
- # Extensions submitted to Extension Warehouse is expected to not define
11
- # global constants.
12
- class GlobalConstants < SketchUp::Cop
13
-
14
- include SketchUp::NoCommentDisable
15
- include SketchUp
16
-
17
- MSG = 'Do not introduce global constants.'.freeze
18
-
19
- def_node_matcher :namespaced_constant?, <<-PATTERN
20
- (casgn
21
- (const _ _) ...
22
- )
23
- PATTERN
24
-
25
- # Constant assignment.
26
- def on_casgn(node)
27
- return if namespaced_constant?(node)
28
-
29
- namespace = Namespace.new(node.parent_module_name)
30
- return unless namespace.top_level?
31
-
32
- add_offense(node, location: :name)
33
- end
34
-
35
- end
36
- end
37
- end
38
- end
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module SketchupRequirements
6
+ # Extensions in SketchUp all share the same Ruby environment on the user's
7
+ # machine. Because of this it's important that each extension isolate
8
+ # itself to avoid clashing with other extensions.
9
+ #
10
+ # Extensions submitted to Extension Warehouse is expected to not define
11
+ # global constants.
12
+ class GlobalConstants < SketchUp::Cop
13
+
14
+ include SketchUp::NoCommentDisable
15
+ include SketchUp
16
+
17
+ MSG = 'Do not introduce global constants.'.freeze
18
+
19
+ def_node_matcher :namespaced_constant?, <<-PATTERN
20
+ (casgn
21
+ (const _ _) ...
22
+ )
23
+ PATTERN
24
+
25
+ # Constant assignment.
26
+ def on_casgn(node)
27
+ return if namespaced_constant?(node)
28
+
29
+ namespace = Namespace.new(node.parent_module_name)
30
+ return unless namespace.top_level?
31
+
32
+ add_offense(node, location: :name)
33
+ end
34
+
35
+ end
36
+ end
37
+ end
38
+ end
@@ -1,42 +1,42 @@
1
- # frozen_string_literal: true
2
-
3
- module RuboCop
4
- module Cop
5
- module SketchupRequirements
6
- # Extensions in SketchUp all share the same Ruby environment on the user's
7
- # machine. Because of this it's important that each extension isolate
8
- # itself to avoid clashing with other extensions.
9
- #
10
- # Extensions submitted to Extension Warehouse is expected to not pollute
11
- # the global namespace by including mix-in modules.
12
- class GlobalInclude < SketchUp::Cop
13
-
14
- include SketchUp::NoCommentDisable
15
- include SketchUp
16
-
17
- MSG = 'Do not include into global namespace.'.freeze
18
-
19
- def_node_matcher :is_include?, <<-PATTERN
20
- (send nil? :include ...)
21
- PATTERN
22
-
23
- def on_send(node)
24
- return unless global_include?(node)
25
-
26
- add_offense(node, location: :selector)
27
- end
28
-
29
- private
30
-
31
- def global_include?(node)
32
- is_include?(node) && global_namespace?(node)
33
- end
34
-
35
- def global_namespace?(node)
36
- %w[Kernel Object].include?(node.parent_module_name)
37
- end
38
-
39
- end
40
- end
41
- end
42
- end
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module SketchupRequirements
6
+ # Extensions in SketchUp all share the same Ruby environment on the user's
7
+ # machine. Because of this it's important that each extension isolate
8
+ # itself to avoid clashing with other extensions.
9
+ #
10
+ # Extensions submitted to Extension Warehouse is expected to not pollute
11
+ # the global namespace by including mix-in modules.
12
+ class GlobalInclude < SketchUp::Cop
13
+
14
+ include SketchUp::NoCommentDisable
15
+ include SketchUp
16
+
17
+ MSG = 'Do not include into global namespace.'.freeze
18
+
19
+ def_node_matcher :is_include?, <<-PATTERN
20
+ (send nil? :include ...)
21
+ PATTERN
22
+
23
+ def on_send(node)
24
+ return unless global_include?(node)
25
+
26
+ add_offense(node, location: :selector)
27
+ end
28
+
29
+ private
30
+
31
+ def global_include?(node)
32
+ is_include?(node) && global_namespace?(node)
33
+ end
34
+
35
+ def global_namespace?(node)
36
+ %w[Kernel Object].include?(node.parent_module_name)
37
+ end
38
+
39
+ end
40
+ end
41
+ end
42
+ end
@@ -1,65 +1,65 @@
1
- # frozen_string_literal: true
2
-
3
- module RuboCop
4
- module Cop
5
- module SketchupRequirements
6
- # Extensions in SketchUp all share the same Ruby environment on the user's
7
- # machine. Because of this it's important that each extension isolate
8
- # itself to avoid clashing with other extensions.
9
- #
10
- # Extensions submitted to Extension Warehouse is expected to not define
11
- # global methods.
12
- class GlobalMethods < SketchUp::Cop
13
-
14
- include SketchUp::NoCommentDisable
15
- include SketchUp
16
-
17
- MSG = 'Do not introduce global methods.'.freeze
18
-
19
- # Reference: http://www.rubydoc.info/gems/rubocop/RuboCop/NodePattern
20
- #
21
- # Matchers for methods defined with this syntax:
22
- #
23
- # def Example.foo
24
- # end
25
- #
26
- # def (Example::Foo).bar
27
- # end
28
-
29
- def_node_matcher :class_method?, <<-PATTERN
30
- (defs
31
- (const _ _) ...
32
- )
33
- PATTERN
34
-
35
- def_node_matcher :class_method, <<-PATTERN
36
- (defs
37
- {
38
- (const nil? $_)
39
- (const (const nil? $_) ...)
40
- }
41
- ...
42
- )
43
- PATTERN
44
-
45
- def on_def(node)
46
- if class_method?(node)
47
- class_method_parent = class_method(node)
48
- namespace = Namespace.new(class_method_parent.to_s)
49
- else
50
- # If a method is defined inside a block then parent_module_name
51
- # will return nil.
52
- return if node.parent_module_name.nil?
53
-
54
- namespace = Namespace.new(node.parent_module_name)
55
- end
56
- return unless namespace.top_level?
57
-
58
- add_offense(node, location: :name)
59
- end
60
- alias on_defs on_def
61
-
62
- end
63
- end
64
- end
65
- end
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module SketchupRequirements
6
+ # Extensions in SketchUp all share the same Ruby environment on the user's
7
+ # machine. Because of this it's important that each extension isolate
8
+ # itself to avoid clashing with other extensions.
9
+ #
10
+ # Extensions submitted to Extension Warehouse is expected to not define
11
+ # global methods.
12
+ class GlobalMethods < SketchUp::Cop
13
+
14
+ include SketchUp::NoCommentDisable
15
+ include SketchUp
16
+
17
+ MSG = 'Do not introduce global methods.'.freeze
18
+
19
+ # Reference: http://www.rubydoc.info/gems/rubocop/RuboCop/NodePattern
20
+ #
21
+ # Matchers for methods defined with this syntax:
22
+ #
23
+ # def Example.foo
24
+ # end
25
+ #
26
+ # def (Example::Foo).bar
27
+ # end
28
+
29
+ def_node_matcher :class_method?, <<-PATTERN
30
+ (defs
31
+ (const _ _) ...
32
+ )
33
+ PATTERN
34
+
35
+ def_node_matcher :class_method, <<-PATTERN
36
+ (defs
37
+ {
38
+ (const nil? $_)
39
+ (const (const nil? $_) ...)
40
+ }
41
+ ...
42
+ )
43
+ PATTERN
44
+
45
+ def on_def(node)
46
+ if class_method?(node)
47
+ class_method_parent = class_method(node)
48
+ namespace = Namespace.new(class_method_parent.to_s)
49
+ else
50
+ # If a method is defined inside a block then parent_module_name
51
+ # will return nil.
52
+ return if node.parent_module_name.nil?
53
+
54
+ namespace = Namespace.new(node.parent_module_name)
55
+ end
56
+ return unless namespace.top_level?
57
+
58
+ add_offense(node, location: :name)
59
+ end
60
+ alias on_defs on_def
61
+
62
+ end
63
+ end
64
+ end
65
+ end
@@ -1,95 +1,95 @@
1
- # frozen_string_literal: true
2
-
3
- module RuboCop
4
- module Cop
5
- module SketchupRequirements
6
- # Extensions in SketchUp all share the same Ruby environment on the user's
7
- # machine. Because of this it's important that each extension isolate
8
- # itself to avoid clashing with other extensions.
9
- #
10
- # Extensions submitted to Extension Warehouse is expected to not define
11
- # global variables.
12
- #
13
- # This cops looks for uses of global variables.
14
- # It does not report offenses for built-in global variables.
15
- # Built-in global variables are allowed by default. Additionally
16
- # users can allow additional variables via the AllowedVariables option.
17
- #
18
- # Note that backreferences like `$1`, `$2`, etc are not global variables.
19
- class GlobalVariables < SketchUp::Cop
20
-
21
- include SketchUp::NoCommentDisable
22
- include SketchUp::DynamicComponentGlobals
23
-
24
- MSG = 'Do not introduce global variables.'.freeze
25
-
26
- # predefined global variables their English aliases
27
- # http://www.zenspider.com/Languages/Ruby/QuickRef.html
28
- BUILT_IN_VARS = %i[
29
- $: $LOAD_PATH
30
- $" $LOADED_FEATURES
31
- $0 $PROGRAM_NAME
32
- $! $ERROR_INFO
33
- $@ $ERROR_POSITION
34
- $; $FS $FIELD_SEPARATOR
35
- $, $OFS $OUTPUT_FIELD_SEPARATOR
36
- $/ $RS $INPUT_RECORD_SEPARATOR
37
- $\\ $ORS $OUTPUT_RECORD_SEPARATOR
38
- $. $NR $INPUT_LINE_NUMBER
39
- $_ $LAST_READ_LINE
40
- $> $DEFAULT_OUTPUT
41
- $< $DEFAULT_INPUT
42
- $$ $PID $PROCESS_ID
43
- $? $CHILD_STATUS
44
- $~ $LAST_MATCH_INFO
45
- $= $IGNORECASE
46
- $* $ARGV
47
- $& $MATCH
48
- $` $PREMATCH
49
- $' $POSTMATCH
50
- $+ $LAST_PAREN_MATCH
51
- $stdin $stdout $stderr
52
- $DEBUG $FILENAME $VERBOSE $SAFE
53
- $-0 $-a $-d $-F $-i $-I $-l $-p $-v $-w
54
- $CLASSPATH $JRUBY_VERSION $JRUBY_REVISION $ENV_JAVA
55
- ].freeze
56
-
57
- SKETCHUP_VARS = %i[
58
- $loaded_files
59
- ].freeze
60
-
61
- # Some globals, like DC's, are being read from so often that it's better
62
- # to ignore these to reduce noise.
63
- READ_ONLY_VARS = DC_GLOBALS
64
-
65
- ALLOWED_VARS = BUILT_IN_VARS | SKETCHUP_VARS
66
-
67
-
68
- def allowed_var?(global_var)
69
- ALLOWED_VARS.include?(global_var)
70
- end
71
-
72
- def read_allowed?(global_var)
73
- READ_ONLY_VARS.include?(global_var)
74
- end
75
-
76
- def on_gvar(node)
77
- global_var, = *node
78
- check(node) unless read_allowed?(global_var)
79
- end
80
-
81
- def on_gvasgn(node)
82
- check(node)
83
- end
84
-
85
- def check(node)
86
- global_var, = *node
87
-
88
- return if allowed_var?(global_var)
89
-
90
- add_offense(node, location: :name)
91
- end
92
- end
93
- end
94
- end
95
- end
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module SketchupRequirements
6
+ # Extensions in SketchUp all share the same Ruby environment on the user's
7
+ # machine. Because of this it's important that each extension isolate
8
+ # itself to avoid clashing with other extensions.
9
+ #
10
+ # Extensions submitted to Extension Warehouse is expected to not define
11
+ # global variables.
12
+ #
13
+ # This cops looks for uses of global variables.
14
+ # It does not report offenses for built-in global variables.
15
+ # Built-in global variables are allowed by default. Additionally
16
+ # users can allow additional variables via the AllowedVariables option.
17
+ #
18
+ # Note that backreferences like `$1`, `$2`, etc are not global variables.
19
+ class GlobalVariables < SketchUp::Cop
20
+
21
+ include SketchUp::NoCommentDisable
22
+ include SketchUp::DynamicComponentGlobals
23
+
24
+ MSG = 'Do not introduce global variables.'.freeze
25
+
26
+ # predefined global variables their English aliases
27
+ # http://www.zenspider.com/Languages/Ruby/QuickRef.html
28
+ BUILT_IN_VARS = %i[
29
+ $: $LOAD_PATH
30
+ $" $LOADED_FEATURES
31
+ $0 $PROGRAM_NAME
32
+ $! $ERROR_INFO
33
+ $@ $ERROR_POSITION
34
+ $; $FS $FIELD_SEPARATOR
35
+ $, $OFS $OUTPUT_FIELD_SEPARATOR
36
+ $/ $RS $INPUT_RECORD_SEPARATOR
37
+ $\\ $ORS $OUTPUT_RECORD_SEPARATOR
38
+ $. $NR $INPUT_LINE_NUMBER
39
+ $_ $LAST_READ_LINE
40
+ $> $DEFAULT_OUTPUT
41
+ $< $DEFAULT_INPUT
42
+ $$ $PID $PROCESS_ID
43
+ $? $CHILD_STATUS
44
+ $~ $LAST_MATCH_INFO
45
+ $= $IGNORECASE
46
+ $* $ARGV
47
+ $& $MATCH
48
+ $` $PREMATCH
49
+ $' $POSTMATCH
50
+ $+ $LAST_PAREN_MATCH
51
+ $stdin $stdout $stderr
52
+ $DEBUG $FILENAME $VERBOSE $SAFE
53
+ $-0 $-a $-d $-F $-i $-I $-l $-p $-v $-w
54
+ $CLASSPATH $JRUBY_VERSION $JRUBY_REVISION $ENV_JAVA
55
+ ].freeze
56
+
57
+ SKETCHUP_VARS = %i[
58
+ $loaded_files
59
+ ].freeze
60
+
61
+ # Some globals, like DC's, are being read from so often that it's better
62
+ # to ignore these to reduce noise.
63
+ READ_ONLY_VARS = DC_GLOBALS
64
+
65
+ ALLOWED_VARS = BUILT_IN_VARS | SKETCHUP_VARS
66
+
67
+
68
+ def allowed_var?(global_var)
69
+ ALLOWED_VARS.include?(global_var)
70
+ end
71
+
72
+ def read_allowed?(global_var)
73
+ READ_ONLY_VARS.include?(global_var)
74
+ end
75
+
76
+ def on_gvar(node)
77
+ global_var, = *node
78
+ check(node) unless read_allowed?(global_var)
79
+ end
80
+
81
+ def on_gvasgn(node)
82
+ check(node)
83
+ end
84
+
85
+ def check(node)
86
+ global_var, = *node
87
+
88
+ return if allowed_var?(global_var)
89
+
90
+ add_offense(node, location: :name)
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end