rubocop-sketchup 1.2.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +20 -19
  3. data/assets/output.html.erb +301 -301
  4. data/config/default.yml +379 -379
  5. data/lib/rubocop/sketchup/config.rb +63 -63
  6. data/lib/rubocop/sketchup/cop/bugs/material_name.rb +108 -108
  7. data/lib/rubocop/sketchup/cop/bugs/render_mode.rb +72 -72
  8. data/lib/rubocop/sketchup/cop/bugs/uniform_scaling.rb +36 -36
  9. data/lib/rubocop/sketchup/cop/deprecations/add_separator_to_menu.rb +25 -25
  10. data/lib/rubocop/sketchup/cop/deprecations/operation_next_transparent.rb +30 -30
  11. data/lib/rubocop/sketchup/cop/deprecations/require_all.rb +27 -27
  12. data/lib/rubocop/sketchup/cop/deprecations/set_texture_projection.rb +26 -26
  13. data/lib/rubocop/sketchup/cop/deprecations/show_ruby_panel.rb +25 -25
  14. data/lib/rubocop/sketchup/cop/deprecations/sketchup_set.rb +30 -30
  15. data/lib/rubocop/sketchup/cop/performance/openssl.rb +41 -41
  16. data/lib/rubocop/sketchup/cop/performance/operation_disable_ui.rb +33 -33
  17. data/lib/rubocop/sketchup/cop/performance/selection_bulk.rb +79 -79
  18. data/lib/rubocop/sketchup/cop/performance/type_check.rb +63 -63
  19. data/lib/rubocop/sketchup/cop/performance/typename.rb +24 -24
  20. data/lib/rubocop/sketchup/cop/requirements/api_namespace.rb +30 -30
  21. data/lib/rubocop/sketchup/cop/requirements/exit.rb +33 -33
  22. data/lib/rubocop/sketchup/cop/requirements/extension_namespace.rb +125 -125
  23. data/lib/rubocop/sketchup/cop/requirements/file_structure.rb +97 -97
  24. data/lib/rubocop/sketchup/cop/requirements/gem_install.rb +45 -45
  25. data/lib/rubocop/sketchup/cop/requirements/get_extension_license.rb +95 -95
  26. data/lib/rubocop/sketchup/cop/requirements/global_constants.rb +38 -38
  27. data/lib/rubocop/sketchup/cop/requirements/global_include.rb +42 -42
  28. data/lib/rubocop/sketchup/cop/requirements/global_methods.rb +65 -65
  29. data/lib/rubocop/sketchup/cop/requirements/global_variables.rb +95 -95
  30. data/lib/rubocop/sketchup/cop/requirements/language_handler_globals.rb +46 -46
  31. data/lib/rubocop/sketchup/cop/requirements/load_path.rb +83 -83
  32. data/lib/rubocop/sketchup/cop/requirements/minimal_registration.rb +73 -73
  33. data/lib/rubocop/sketchup/cop/requirements/observers_start_operation.rb +161 -161
  34. data/lib/rubocop/sketchup/cop/requirements/register_extension.rb +45 -45
  35. data/lib/rubocop/sketchup/cop/requirements/ruby_core_namespace.rb +291 -291
  36. data/lib/rubocop/sketchup/cop/requirements/ruby_stdlib_namespace.rb +634 -634
  37. data/lib/rubocop/sketchup/cop/requirements/shipped_extensions_namespace.rb +61 -61
  38. data/lib/rubocop/sketchup/cop/requirements/sketchup_extension.rb +119 -119
  39. data/lib/rubocop/sketchup/cop/requirements/sketchup_require.rb +150 -149
  40. data/lib/rubocop/sketchup/cop/suggestions/add_group.rb +49 -49
  41. data/lib/rubocop/sketchup/cop/suggestions/compatibility.rb +128 -128
  42. data/lib/rubocop/sketchup/cop/suggestions/dc_internals.rb +34 -34
  43. data/lib/rubocop/sketchup/cop/suggestions/file_encoding.rb +78 -78
  44. data/lib/rubocop/sketchup/cop/suggestions/model_entities.rb +58 -58
  45. data/lib/rubocop/sketchup/cop/suggestions/monkey_patched_api.rb +45 -45
  46. data/lib/rubocop/sketchup/cop/suggestions/operation_name.rb +137 -137
  47. data/lib/rubocop/sketchup/cop/suggestions/sketchup_find_support_file.rb +39 -39
  48. data/lib/rubocop/sketchup/cop/suggestions/tool_drawing_bounds.rb +45 -45
  49. data/lib/rubocop/sketchup/cop/suggestions/tool_invalidate.rb +68 -68
  50. data/lib/rubocop/sketchup/cop/suggestions/tool_user_input.rb +41 -41
  51. data/lib/rubocop/sketchup/cop/suggestions/toolbar_timer.rb +65 -65
  52. data/lib/rubocop/sketchup/cop.rb +110 -110
  53. data/lib/rubocop/sketchup/dc_globals.rb +24 -24
  54. data/lib/rubocop/sketchup/dc_methods.rb +130 -130
  55. data/lib/rubocop/sketchup/extension_project.rb +65 -65
  56. data/lib/rubocop/sketchup/features.rb +1477 -1420
  57. data/lib/rubocop/sketchup/formatter/extension_review.rb +269 -269
  58. data/lib/rubocop/sketchup/inject.rb +19 -19
  59. data/lib/rubocop/sketchup/namespace.rb +49 -49
  60. data/lib/rubocop/sketchup/namespace_checker.rb +103 -46
  61. data/lib/rubocop/sketchup/no_comment_disable.rb +17 -17
  62. data/lib/rubocop/sketchup/range_help.rb +52 -52
  63. data/lib/rubocop/sketchup/sketchup_target_range.rb +75 -75
  64. data/lib/rubocop/sketchup/sketchup_version.rb +129 -128
  65. data/lib/rubocop/sketchup/tool_checker.rb +41 -43
  66. data/lib/rubocop/sketchup/version.rb +7 -7
  67. data/lib/rubocop/sketchup.rb +14 -14
  68. data/lib/rubocop-sketchup.rb +53 -53
  69. data/rubocop-sketchup.gemspec +29 -29
  70. metadata +8 -8
@@ -1,61 +1,61 @@
1
- # frozen_string_literal: true
2
-
3
- module RuboCop
4
- module Cop
5
- module SketchupRequirements
6
- # Don't modify SketchUp's shipped extensions.
7
- class ShippedExtensionsNamespace < SketchUp::Cop
8
-
9
- include SketchUp::NoCommentDisable
10
- include SketchUp::NamespaceChecker
11
-
12
- MSG = 'Do not modify shipped extensions.'
13
-
14
- # We check only against the top level namespaces. The core define more
15
- # objects, but they are under one of the top level namespaces listed.
16
-
17
- NAMESPACES_ADVANCED_CAMERA_TOOLS = %w[
18
- ACTUtil
19
- CameraAppObserver
20
- CameraEntityObserver
21
- CameraFrameChangeObserver
22
- CameraRep
23
- CameraToolModelObserver
24
- CameraToolPagesObserver
25
- CameraToolViewObserver
26
- FilmCameraTool
27
- FSCameraData
28
- FSGeomUtils
29
- FSValidate
30
- PageNameChangeObserver
31
- ].freeze
32
-
33
- NAMESPACES_DYNAMIC_COMPONENTS = %w[
34
- DCConverter
35
- DCDownloader
36
- DCFunctionsV1
37
- DCInteractTool
38
- DCObservers
39
- DCProgressBar
40
- DynamicComponents
41
- DynamicComponentsV1
42
- ].freeze
43
-
44
- NAMESPACES_TRIMBLE_CONNECT = %w[
45
- Trimble
46
- ].freeze
47
-
48
- NAMESPACES = (
49
- NAMESPACES_ADVANCED_CAMERA_TOOLS |
50
- NAMESPACES_DYNAMIC_COMPONENTS |
51
- NAMESPACES_TRIMBLE_CONNECT
52
- ).freeze
53
-
54
- def namespaces
55
- NAMESPACES
56
- end
57
-
58
- end
59
- end
60
- end
61
- end
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module SketchupRequirements
6
+ # Don't modify SketchUp's shipped extensions.
7
+ class ShippedExtensionsNamespace < SketchUp::Cop
8
+
9
+ include SketchUp::NoCommentDisable
10
+ include SketchUp::NamespaceChecker
11
+
12
+ MSG = 'Do not modify shipped extensions.'
13
+
14
+ # We check only against the top level namespaces. The core define more
15
+ # objects, but they are under one of the top level namespaces listed.
16
+
17
+ NAMESPACES_ADVANCED_CAMERA_TOOLS = %w[
18
+ ACTUtil
19
+ CameraAppObserver
20
+ CameraEntityObserver
21
+ CameraFrameChangeObserver
22
+ CameraRep
23
+ CameraToolModelObserver
24
+ CameraToolPagesObserver
25
+ CameraToolViewObserver
26
+ FilmCameraTool
27
+ FSCameraData
28
+ FSGeomUtils
29
+ FSValidate
30
+ PageNameChangeObserver
31
+ ].freeze
32
+
33
+ NAMESPACES_DYNAMIC_COMPONENTS = %w[
34
+ DCConverter
35
+ DCDownloader
36
+ DCFunctionsV1
37
+ DCInteractTool
38
+ DCObservers
39
+ DCProgressBar
40
+ DynamicComponents
41
+ DynamicComponentsV1
42
+ ].freeze
43
+
44
+ NAMESPACES_TRIMBLE_CONNECT = %w[
45
+ Trimble
46
+ ].freeze
47
+
48
+ NAMESPACES = (
49
+ NAMESPACES_ADVANCED_CAMERA_TOOLS |
50
+ NAMESPACES_DYNAMIC_COMPONENTS |
51
+ NAMESPACES_TRIMBLE_CONNECT
52
+ ).freeze
53
+
54
+ def namespaces
55
+ NAMESPACES
56
+ end
57
+
58
+ end
59
+ end
60
+ end
61
+ end
@@ -1,119 +1,119 @@
1
- # frozen_string_literal: true
2
-
3
- module RuboCop
4
- module Cop
5
- module SketchupRequirements
6
- # Register a single instance of SketchupExtension per extension.
7
- # This should be done by the root .rb file in the extension package.
8
- #
9
- # @example Good - a single SketchupExtension is registered.
10
- # module Example
11
- # unless file_loaded?(__FILE__)
12
- # extension = SketchupExtension.new('Hello World', 'example/main')
13
- # Sketchup.register_extension(extension, true)
14
- # file_loaded(__FILE__)
15
- # end
16
- # end
17
- class SketchupExtension < SketchUp::Cop
18
-
19
- include SketchUp::NoCommentDisable
20
- include SketchUp::ExtensionProject
21
- include RangeHelp
22
-
23
- # rubocop:disable Layout/LineLength
24
- MSG = 'Create and register one SketchupExtension instance per extension.'
25
- MSG_CREATE_ONE = 'Create only SketchupExtension instance per extension.'
26
- MSG_CREATE_MISSING = 'SketchupExtension.new not found.'
27
- MSG_REGISTER_ONE = 'Only register one SketchupExtension instance per extension.'
28
- MSG_REGISTER_MISSING = 'Registration of SketchupExtension not found. Expected %s'
29
- # rubocop:enable Layout/LineLength
30
-
31
- # Reference: http://rubocop.readthedocs.io/en/latest/node_pattern/
32
- def_node_search :sketchup_extension_new, <<-PATTERN
33
- (send
34
- (const {nil? cbase} :SketchupExtension) :new ...)
35
- PATTERN
36
-
37
- def_node_search :sketchup_register_extension, <<-PATTERN
38
- (send
39
- (const {nil? cbase} :Sketchup) :register_extension
40
- {({lvar ivar cvar gvar} $_)(const nil? $_)}
41
- _ ?)
42
- PATTERN
43
-
44
- def investigate(processed_source)
45
- return unless root_file?(processed_source)
46
-
47
- source_node = processed_source.ast
48
- # Using range similar to RuboCop::Cop::Naming::Filename (file_name.rb)
49
- range = source_range(processed_source.buffer, 1, 0)
50
-
51
- # Look for SketchupExtension.new.
52
- extension_nodes = sketchup_extension_new(source_node).to_a
53
-
54
- # Threat instances not assigned to anything as non-existing.
55
- extension_nodes.select! { |node|
56
- node.parent&.assignment?
57
- }
58
-
59
- # There should not be multiple instances.
60
- if extension_nodes.size > 1
61
- add_offense(nil,
62
- location: range,
63
- message: MSG_CREATE_ONE)
64
- return
65
- end
66
-
67
- # There should be exactly one.
68
- extension_node = extension_nodes.first
69
- if extension_node.nil?
70
- add_offense(nil,
71
- location: range,
72
- message: MSG_CREATE_MISSING)
73
- return
74
- end
75
-
76
- # Ensure it have two arguments.
77
- if extension_node.arguments.size < 2
78
- message = if extension_node.arguments.size == 1
79
- 'Missing second argument for the path'
80
- else
81
- 'Missing required name arguments'
82
- end
83
- add_offense(extension_node,
84
- message: message)
85
- return
86
- end
87
-
88
- # Find the name of the value SketchupExtension.new was assigned to.
89
- assignment_node = extension_node.parent
90
- if assignment_node.casgn_type?
91
- extension_var = assignment_node.to_a[1]
92
- else
93
- extension_var = assignment_node.to_a[0]
94
- end
95
-
96
- # Look for Sketchup.register and make sure it register the extension
97
- # object detected earlier.
98
- registered_vars = sketchup_register_extension(source_node).to_a
99
-
100
- # Make sure there is only one call to `register_extension`.
101
- if registered_vars.size > 1
102
- add_offense(registered_vars[1],
103
- message: MSG_REGISTER_ONE)
104
- return
105
- end
106
-
107
- registered_var = sketchup_register_extension(source_node).first
108
- unless registered_var == extension_var
109
- msg = MSG_REGISTER_MISSING % extension_var.to_s
110
- add_offense(nil,
111
- location: range,
112
- message: msg)
113
- end
114
- end
115
-
116
- end
117
- end
118
- end
119
- end
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module SketchupRequirements
6
+ # Register a single instance of SketchupExtension per extension.
7
+ # This should be done by the root .rb file in the extension package.
8
+ #
9
+ # @example Good - a single SketchupExtension is registered.
10
+ # module Example
11
+ # unless file_loaded?(__FILE__)
12
+ # extension = SketchupExtension.new('Hello World', 'example/main')
13
+ # Sketchup.register_extension(extension, true)
14
+ # file_loaded(__FILE__)
15
+ # end
16
+ # end
17
+ class SketchupExtension < SketchUp::Cop
18
+
19
+ include SketchUp::NoCommentDisable
20
+ include SketchUp::ExtensionProject
21
+ include RangeHelp
22
+
23
+ # rubocop:disable Layout/LineLength
24
+ MSG = 'Create and register one SketchupExtension instance per extension.'
25
+ MSG_CREATE_ONE = 'Create only SketchupExtension instance per extension.'
26
+ MSG_CREATE_MISSING = 'SketchupExtension.new not found.'
27
+ MSG_REGISTER_ONE = 'Only register one SketchupExtension instance per extension.'
28
+ MSG_REGISTER_MISSING = 'Registration of SketchupExtension not found. Expected %s'
29
+ # rubocop:enable Layout/LineLength
30
+
31
+ # Reference: http://rubocop.readthedocs.io/en/latest/node_pattern/
32
+ def_node_search :sketchup_extension_new, <<-PATTERN
33
+ (send
34
+ (const {nil? cbase} :SketchupExtension) :new ...)
35
+ PATTERN
36
+
37
+ def_node_search :sketchup_register_extension, <<-PATTERN
38
+ (send
39
+ (const {nil? cbase} :Sketchup) :register_extension
40
+ {({lvar ivar cvar gvar} $_)(const nil? $_)}
41
+ _ ?)
42
+ PATTERN
43
+
44
+ def investigate(processed_source)
45
+ return unless root_file?(processed_source)
46
+
47
+ source_node = processed_source.ast
48
+ # Using range similar to RuboCop::Cop::Naming::Filename (file_name.rb)
49
+ range = source_range(processed_source.buffer, 1, 0)
50
+
51
+ # Look for SketchupExtension.new.
52
+ extension_nodes = sketchup_extension_new(source_node).to_a
53
+
54
+ # Threat instances not assigned to anything as non-existing.
55
+ extension_nodes.select! { |node|
56
+ node.parent&.assignment?
57
+ }
58
+
59
+ # There should not be multiple instances.
60
+ if extension_nodes.size > 1
61
+ add_offense(nil,
62
+ location: range,
63
+ message: MSG_CREATE_ONE)
64
+ return
65
+ end
66
+
67
+ # There should be exactly one.
68
+ extension_node = extension_nodes.first
69
+ if extension_node.nil?
70
+ add_offense(nil,
71
+ location: range,
72
+ message: MSG_CREATE_MISSING)
73
+ return
74
+ end
75
+
76
+ # Ensure it have two arguments.
77
+ if extension_node.arguments.size < 2
78
+ message = if extension_node.arguments.size == 1
79
+ 'Missing second argument for the path'
80
+ else
81
+ 'Missing required name arguments'
82
+ end
83
+ add_offense(extension_node,
84
+ message: message)
85
+ return
86
+ end
87
+
88
+ # Find the name of the value SketchupExtension.new was assigned to.
89
+ assignment_node = extension_node.parent
90
+ if assignment_node.casgn_type?
91
+ extension_var = assignment_node.to_a[1]
92
+ else
93
+ extension_var = assignment_node.to_a[0]
94
+ end
95
+
96
+ # Look for Sketchup.register and make sure it register the extension
97
+ # object detected earlier.
98
+ registered_vars = sketchup_register_extension(source_node).to_a
99
+
100
+ # Make sure there is only one call to `register_extension`.
101
+ if registered_vars.size > 1
102
+ add_offense(registered_vars[1],
103
+ message: MSG_REGISTER_ONE)
104
+ return
105
+ end
106
+
107
+ registered_var = sketchup_register_extension(source_node).first
108
+ unless registered_var == extension_var
109
+ msg = MSG_REGISTER_MISSING % extension_var.to_s
110
+ add_offense(nil,
111
+ location: range,
112
+ message: msg)
113
+ end
114
+ end
115
+
116
+ end
117
+ end
118
+ end
119
+ end
@@ -1,149 +1,150 @@
1
- # frozen_string_literal: true
2
-
3
- module RuboCop
4
- module Cop
5
- module SketchupRequirements
6
- # Omit file extensions when using `Sketchup.require` to allow encrypted
7
- # files to be loaded.
8
- #
9
- # Ruby C extensions, `.so`/`.bundle` libraries must always be loaded via
10
- # the normal `require`.
11
- #
12
- # @example Bad - This will fail if extension is encrypted
13
- # Sketchup.require 'hello/world.rb'
14
- #
15
- # @example Good - This will work for `.rbe`, `.rbs` and `rb` files.
16
- # Sketchup.require 'hello/world'
17
- #
18
- # @example Bad - This will fail if extension is encrypted
19
- # extension = SketchupExtension.new("Example", "Example/main.rb")
20
- #
21
- # @example Good - This will work for `.rbe`, `.rbs` and `rb` files.
22
- # extension = SketchupExtension.new("Example", "Example/main")
23
- class SketchupRequire < SketchUp::Cop
24
-
25
- include SketchUp::ExtensionProject
26
- include SketchUp::NoCommentDisable
27
- include RangeHelp
28
-
29
- MSG_SKETCHUP_REQUIRE_EXT_NAME = 'Do not hard code file extensions '\
30
- 'with `Sketchup.require`.'
31
-
32
- MSG_EXTENSION_NEW_EXT_NAME = 'Do not hard code file extensions '\
33
- 'with `SketchupExtension.new`.'
34
-
35
- MSG_REQUIRE_FOR_BINARY = 'Use `require` instead of `Sketchup.require` '\
36
- 'to load binary Ruby libraries.'
37
-
38
- MSG_REQUIRE_ENCRYPTED = 'Use `Sketchup.require` when loading Ruby '\
39
- 'files for encrypted extensions.'
40
-
41
- def_node_matcher :ruby_require, <<-PATTERN
42
- (send nil? :require (str $_))
43
- PATTERN
44
-
45
- def_node_matcher :ruby_require?, <<-PATTERN
46
- (send nil? :require (str _))
47
- PATTERN
48
-
49
-
50
- def_node_matcher :sketchup_require, <<-PATTERN
51
- (send (const nil? :Sketchup) :require (str $_))
52
- PATTERN
53
-
54
- def_node_matcher :sketchup_require?, <<-PATTERN
55
- (send (const nil? :Sketchup) :require (str _))
56
- PATTERN
57
-
58
-
59
- def_node_matcher :sketchup_extension_new, <<-PATTERN
60
- (send (const nil? :SketchupExtension) :new _ (str $_))
61
- PATTERN
62
-
63
- def_node_matcher :sketchup_extension_new?, <<-PATTERN
64
- (send (const nil? :SketchupExtension) :new _ (str _))
65
- PATTERN
66
-
67
-
68
- TOOLS_RUBY_FILES = %w[extensions.rb langhandler.rb sketchup.rb].freeze
69
-
70
-
71
- def on_send(node)
72
- if sketchup_require?(node)
73
- filename = sketchup_require(node)
74
- return if check_binary_sketchup_require(node, filename)
75
- return if check_sketchup_require_filename(node, filename)
76
-
77
- elsif ruby_require?(node)
78
- filename = ruby_require(node)
79
- return if check_encrypted_require(node, filename)
80
-
81
- elsif sketchup_extension_new?(node)
82
- filename = sketchup_extension_new(node)
83
- return if check_sketchup_extension_new_filename(node, filename)
84
-
85
- end
86
- end
87
-
88
- private
89
-
90
- def binary_require?(filename)
91
- return unless extension_binaries?
92
- return if extension_binaries.empty?
93
-
94
- extension_binaries.include?(filename)
95
- end
96
-
97
- def check_binary_sketchup_require(node, filename)
98
- return unless binary_require?(filename)
99
-
100
- end_pos = node.loc.dot.end_pos
101
- range = node.receiver.loc.expression.with(end_pos: end_pos)
102
- add_offense(node, location: range, message: MSG_REQUIRE_FOR_BINARY)
103
- true
104
- end
105
-
106
- def check_sketchup_require_filename(node, filename)
107
- return if valid_filename?(filename)
108
-
109
- add_offense(node, location: file_ext_range(node.arguments.first),
110
- message: MSG_SKETCHUP_REQUIRE_EXT_NAME)
111
- true
112
- end
113
-
114
- def check_sketchup_extension_new_filename(node, filename)
115
- return if valid_filename?(filename)
116
-
117
- add_offense(node, location: file_ext_range(node.arguments.last),
118
- message: MSG_EXTENSION_NEW_EXT_NAME)
119
- true
120
- end
121
-
122
- def check_encrypted_require(node, filename)
123
- return unless encrypted_extension?
124
- return unless extension_file?(filename)
125
-
126
- add_offense(node, location: node.loc.selector,
127
- message: MSG_REQUIRE_ENCRYPTED)
128
- true
129
- end
130
-
131
- def first_directory(path)
132
- path.to_s.split(File::SEPARATOR).first.downcase
133
- end
134
-
135
- def extension_file?(filename)
136
- pathname = Pathname.new(filename).cleanpath
137
- return false unless pathname.relative?
138
-
139
- first_directory(extension_directory) == first_directory(pathname)
140
- end
141
-
142
- def valid_filename?(filename)
143
- File.extname(filename).empty? || TOOLS_RUBY_FILES.include?(filename)
144
- end
145
-
146
- end
147
- end
148
- end
149
- end
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module SketchupRequirements
6
+ # Omit file extensions when using `Sketchup.require` to allow encrypted
7
+ # files to be loaded.
8
+ #
9
+ # Ruby C extensions, `.so`/`.bundle` libraries must always be loaded via
10
+ # the normal `require`.
11
+ #
12
+ # @example Bad - This will fail if extension is encrypted
13
+ # Sketchup.require 'hello/world.rb'
14
+ #
15
+ # @example Good - This will work for `.rbe`, `.rbs` and `rb` files.
16
+ # Sketchup.require 'hello/world'
17
+ #
18
+ # @example Bad - This will fail if extension is encrypted
19
+ # extension = SketchupExtension.new("Example", "Example/main.rb")
20
+ #
21
+ # @example Good - This will work for `.rbe`, `.rbs` and `rb` files.
22
+ # extension = SketchupExtension.new("Example", "Example/main")
23
+ class SketchupRequire < SketchUp::Cop
24
+
25
+ include SketchUp::ExtensionProject
26
+ include SketchUp::NoCommentDisable
27
+ include RangeHelp
28
+
29
+ MSG_SKETCHUP_REQUIRE_EXT_NAME = 'Do not hard code file extensions ' \
30
+ 'with `Sketchup.require`.'
31
+
32
+ MSG_EXTENSION_NEW_EXT_NAME = 'Do not hard code file extensions ' \
33
+ 'with `SketchupExtension.new`.'
34
+
35
+ MSG_REQUIRE_FOR_BINARY = 'Use `require` instead of ' \
36
+ '`Sketchup.require` to load binary Ruby ' \
37
+ 'libraries.'
38
+
39
+ MSG_REQUIRE_ENCRYPTED = 'Use `Sketchup.require` when loading Ruby ' \
40
+ 'files for encrypted extensions.'
41
+
42
+ def_node_matcher :ruby_require, <<-PATTERN
43
+ (send nil? :require (str $_))
44
+ PATTERN
45
+
46
+ def_node_matcher :ruby_require?, <<-PATTERN
47
+ (send nil? :require (str _))
48
+ PATTERN
49
+
50
+
51
+ def_node_matcher :sketchup_require, <<-PATTERN
52
+ (send (const nil? :Sketchup) :require (str $_))
53
+ PATTERN
54
+
55
+ def_node_matcher :sketchup_require?, <<-PATTERN
56
+ (send (const nil? :Sketchup) :require (str _))
57
+ PATTERN
58
+
59
+
60
+ def_node_matcher :sketchup_extension_new, <<-PATTERN
61
+ (send (const nil? :SketchupExtension) :new _ (str $_))
62
+ PATTERN
63
+
64
+ def_node_matcher :sketchup_extension_new?, <<-PATTERN
65
+ (send (const nil? :SketchupExtension) :new _ (str _))
66
+ PATTERN
67
+
68
+
69
+ TOOLS_RUBY_FILES = %w[extensions.rb langhandler.rb sketchup.rb].freeze
70
+
71
+
72
+ def on_send(node)
73
+ if sketchup_require?(node)
74
+ filename = sketchup_require(node)
75
+ return if check_binary_sketchup_require(node, filename)
76
+ return if check_sketchup_require_filename(node, filename)
77
+
78
+ elsif ruby_require?(node)
79
+ filename = ruby_require(node)
80
+ return if check_encrypted_require(node, filename)
81
+
82
+ elsif sketchup_extension_new?(node)
83
+ filename = sketchup_extension_new(node)
84
+ return if check_sketchup_extension_new_filename(node, filename)
85
+
86
+ end
87
+ end
88
+
89
+ private
90
+
91
+ def binary_require?(filename)
92
+ return unless extension_binaries?
93
+ return if extension_binaries.empty?
94
+
95
+ extension_binaries.include?(filename)
96
+ end
97
+
98
+ def check_binary_sketchup_require(node, filename)
99
+ return unless binary_require?(filename)
100
+
101
+ end_pos = node.loc.dot.end_pos
102
+ range = node.receiver.loc.expression.with(end_pos: end_pos)
103
+ add_offense(node, location: range, message: MSG_REQUIRE_FOR_BINARY)
104
+ true
105
+ end
106
+
107
+ def check_sketchup_require_filename(node, filename)
108
+ return if valid_filename?(filename)
109
+
110
+ add_offense(node, location: file_ext_range(node.arguments.first),
111
+ message: MSG_SKETCHUP_REQUIRE_EXT_NAME)
112
+ true
113
+ end
114
+
115
+ def check_sketchup_extension_new_filename(node, filename)
116
+ return if valid_filename?(filename)
117
+
118
+ add_offense(node, location: file_ext_range(node.arguments.last),
119
+ message: MSG_EXTENSION_NEW_EXT_NAME)
120
+ true
121
+ end
122
+
123
+ def check_encrypted_require(node, filename)
124
+ return unless encrypted_extension?
125
+ return unless extension_file?(filename)
126
+
127
+ add_offense(node, location: node.loc.selector,
128
+ message: MSG_REQUIRE_ENCRYPTED)
129
+ true
130
+ end
131
+
132
+ def first_directory(path)
133
+ path.to_s.split(File::SEPARATOR).first.downcase
134
+ end
135
+
136
+ def extension_file?(filename)
137
+ pathname = Pathname.new(filename).cleanpath
138
+ return false unless pathname.relative?
139
+
140
+ first_directory(extension_directory) == first_directory(pathname)
141
+ end
142
+
143
+ def valid_filename?(filename)
144
+ File.extname(filename).empty? || TOOLS_RUBY_FILES.include?(filename)
145
+ end
146
+
147
+ end
148
+ end
149
+ end
150
+ end