Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #148 Add cop for Entity new #161

Merged
merged 1 commit into from
Mar 23, 2023
Merged

Fix #148 Add cop for Entity new #161

merged 1 commit into from
Mar 23, 2023

Conversation

Eneroth3
Copy link
Contributor

@Eneroth3 Eneroth3 commented Mar 17, 2023

Work in progress.

My node matcher ((send (const (const nil? :Sketchup) :Face) :new _)) doesn't seem to catch anything.

For now it should catch Sketchup::Face.new and skip Edge and Group, but all 3 tests give identical results.

Failures:

  1) RuboCop::Cop::SketchupRequirements::InitEntity registers an offense when using `Sketchup::Edge.new`
     Failure/Error:
       expect_offense(<<~RUBY)
         Sketchup::Edge.new
         ^^^^^^^^^^^^^^^^^^ Entity objects (Face, Edge, Group etc) are managed SketchUp. Instead of using the `new` constructor, use the `Sketchup::Entities#add_*` methods.
       RUBY

       Diff:
       @@ -1,3 +1,2 @@
        Sketchup::Edge.new
       -^^^^^^^^^^^^^^^^^^ Entity objects (Face, Edge, Group etc) are managed SketchUp. Instead of using the `new` constructor, use the `Sketchup::Entities#add_*` methods.

     # ./spec/rubocop/cop/sketchup_requirements/init_entity_spec.rb:17:in `block (2 levels) in <top (required)>'

  2) RuboCop::Cop::SketchupRequirements::InitEntity registers an offense when using `Sketchup::Group.new`
     Failure/Error:
       expect_offense(<<~RUBY)
         Sketchup::Group.new
         ^^^^^^^^^^^^^^^^^^^ Entity objects (Face, Edge, Group etc) are managed SketchUp. Instead of using the `new` constructor, use the `Sketchup::Entities#add_*` methods.
       RUBY

       Diff:
       @@ -1,3 +1,2 @@
        Sketchup::Group.new
       -^^^^^^^^^^^^^^^^^^^ Entity objects (Face, Edge, Group etc) are managed SketchUp. Instead of using the `new` constructor, use the `Sketchup::Entities#add_*` methods.

     # ./spec/rubocop/cop/sketchup_requirements/init_entity_spec.rb:24:in `block (2 levels) in <top (required)>'

  3) RuboCop::Cop::SketchupRequirements::InitEntity registers an offense when using `Sketchup::Face.new`
     Failure/Error:
       expect_offense(<<~RUBY)
         Sketchup::Face.new
         ^^^^^^^^^^^^^^^^^^ Entity objects (Face, Edge, Group etc) are managed SketchUp. Instead of using the `new` constructor, use the `Sketchup::Entities#add_*` methods.
       RUBY

       Diff:
       @@ -1,3 +1,2 @@
        Sketchup::Face.new
       -^^^^^^^^^^^^^^^^^^ Entity objects (Face, Edge, Group etc) are managed SketchUp. Instead of using the `new` constructor, use the `Sketchup::Entities#add_*` methods.

I'm suspecting a syntax issue, but I'm not sure. @thomthom, can you have a look?

@thomthom
Copy link
Member

My node matcher ((send (const (const nil? :Sketchup) :Face) :new _)) doesn't seem to catch anything.

I'm guessing that it's trying to match on new as an instance method, but we want to catch usage of new as a class method.

Your matcher looks close though. (Have you found the ruby-parse tool?)

C:\Users\Thomas>ruby-parse -e 'Sketchup::Face.new'
warning: parser/current is loading parser/ruby27, which recognizes 2.7.7-compliant syntax, but you are running 2.7.4.
Please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
(send
  (const
    (const nil :Sketchup) :Face) :new)

C:\Users\Thomas>

One thing I wonder, is if the underscore in your matcher means it expects at least one param?

Btw, a matcher is capable of capturing part of the node, so that you should be able to capture the class name and then reuse that in the error message eventually.

@thomthom
Copy link
Member

thomthom commented Mar 20, 2023

Yea, you might want to replace _ with ... to match any number of nodes (including zero):
https://docs.rubocop.org/rubocop-ast/node_pattern.html#for-several-subsequent-nodes

And worth to add tests for both with and without arguments passed to new.

@Eneroth3
Copy link
Contributor Author

Got it to work know and added a bunch of more classes to check for

.rubocop.yml Show resolved Hide resolved
@@ -209,6 +209,11 @@ SketchupRequirements/GlobalVariables:
Reference: https://github.com/SketchUp/rubocop-sketchup/tree/main/manual/cops_requirements.md#globalvariables
Enabled: true

SketchupRequirements/InitEntity:
Description: Do not init SketchUp Entity objects with `new`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd expand init to initialize.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be regenerated? bundle exec rake ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still this init usage.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, missed the one in the description

lib/rubocop/sketchup/cop/requirements/init_entity.rb Outdated Show resolved Hide resolved
@Eneroth3
Copy link
Contributor Author

Ready for re-review!

(send
(const
(const nil? :Sketchup)
{:Face :Group :Edge :ConmponentInsstance :Image}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ConmponentInsstance => ComponentInstance

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible to call a method from a matcher. Syntax uses something like #methodname I think. But we could use that to make the node matcher look up a complete list of Sketchup::Entity derived classes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a method and also listed a whole bunch of more classes while at it.

Copy link
Member

@thomthom thomthom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good. You're quickly ramping up the complexity. 👍

Spotted a couple of spelling mistakes. And got a suggestion on how we can cover all the entities instead of just a subset.

@Eneroth3 Eneroth3 requested a review from thomthom March 22, 2023 10:28
# them.
class InitialiseEntity < SketchUp::Cop

ENTITY_CLASSES = %i[
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Known direct subclasses:

AttributeDictionaries, AttributeDictionary, Axes, Behavior, Curve, DefinitionList, Drawingelement, EdgeUse, Layer, LayerFolder, Layers, LineStyle, LineStyles, Loop, Material, Materials, Page, Pages, RenderingOptions, ShadowInfo, Style, Styles, Texture, Vertex

And then you have the subclasses of Drawinglement:

ComponentDefinition, ComponentInstance, ConstructionLine, ConstructionPoint, Dimension, Edge, Face, Group, Image, SectionPlane, Text


subject(:cop) { described_class.new }

entity_classes = %i[
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd just refer to the ENTITY_CLASSES constant in the cop here, reuse that.


require 'spec_helper'

describe RuboCop::Cop::SketchupRequirements::InitialiseEntity do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry, I messed up last round of comments, the correct spelling in Ruby is initialize. (I don't know why I thought otherwise before.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are too British! :P

@Eneroth3 Eneroth3 requested a review from thomthom March 22, 2023 14:21
Copy link
Member

@thomthom thomthom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM (small nit: still using "init" in default.yml)

@Eneroth3 Eneroth3 merged commit f969742 into main Mar 23, 2023
@Eneroth3 Eneroth3 deleted the add-entity-new-cop branch March 30, 2023 12:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants