Coder Social home page Coder Social logo

Comments (11)

pirj avatar pirj commented on August 27, 2024 1

It will be able to detect identical definitions, that's for sure.
As we go further, the same similarity analysis algorithms will be required. I like the idea of making it a cop, as it would be standalone and non-intrusive. It applies restrictions, though. It won't be able to point out that a part of the node pattern is available already in a given class' inheritance chain. Neither it will be able to detect that a and b are identical:

class_or_module = '{:Class :Module}'
module_or_class = '{:Module :Class}'
def_node_matcher :a, "(const nil? #{class_or_module})"
def_node_matcher :b, "(const nil? #{module_or_class})"

I would be more in favour of a runtime tool, profiler-alike, that would load the code up, collect a tree considering inheritance, and would come up with refactor recommendations.

As @marcandre points out, it's quite a challenge, since NP is not being parsed to an AST right now, and it's a lot of work for just a single usage. Also, a weighting algorithm should be present, taking the length of the matching sequence, and their relationship in the inheritance chain. Not to say that some may have distinct parts that can be %'d.

So basically the idea was to reduce NPs into smaller parts, be able to extract common parts out of them while avoiding too much generality (end up with all variants of (%), (% %) etc in a single includable).

from rubocop-ast.

marcandre avatar marcandre commented on August 27, 2024 1

I have a working proof of concept of a NodePattern compiler, working basically exactly as parser works (it generates ::Parser::AST::Nodes with locations and all), which would make this not too difficult. I'm planning on specializing the case of {series of literals} to a set-like node, so {:Class :Module} could be detected as similar to {:Module :Class}, but if we want more complex things like {nil? const} === {const nil?}, this could be achieved easily by specializing the eql? for the Union node to disregard the order of its children.

We could even envision applying NodePattern to... node patterns ๐Ÿ˜†

from rubocop-ast.

marcandre avatar marcandre commented on August 27, 2024 1

Example:

> rake parse '(send {nil? const} :example <#func 42...> int+)'
(sequence
  (node-type :send)
  (union
    (predicate :nil?)
    (node-type :const))
  (symbol :example)
  (any-order
    (function-call :func)
    (number 42)
    (rest :"..."))
  (repetition
    (node-type :int) :+))

from rubocop-ast.

pirj avatar pirj commented on August 27, 2024 1

predicted rubocop-nodepattern will appear in the future

A RuboCop extension that would enforce writing good node patterns. And a node pattern style guide indeed :D

from rubocop-ast.

marcandre avatar marcandre commented on August 27, 2024

Seems quite challenging ๐Ÿ˜ฎ

Not related, but we can now write (block (send #global_const(:MyClass) :goodbye)) which I think is much better and can only be done by humans.

from rubocop-ast.

pirj avatar pirj commented on August 27, 2024

Sure, it's understandable that :myclass matcher can be done in a better way.
The point is similarities.

No doubt challenging, maybe even to a point where it's not really worth it :(

One option is to make it a separate binary. In this case, it would have to extract node patterns, but it will fail to find similarities in node patterns that are inherited from base classes and included from concerns.

For runtime detector, it's hard to tell when it's the right moment to start this analysis and to keep the original definitions of node patterns somewhere.

Either way, a parser for Node Pattern or an algorithm operating on expanded node pattern strings will have to be written.

from rubocop-ast.

jonatas avatar jonatas commented on August 27, 2024

from rubocop-ast.

jonatas avatar jonatas commented on August 27, 2024

Maybe we can start with a simple warning as a cop to detect the same node pattern already defined in another file?

 fast "^^(send nil? {:def_node_matcher :def_node_search} ...)" lib/rubocop 
# lib/rubocop/ast/node.rb:325
        {(class (const $_ $_) ...)
# lib/rubocop/ast/node.rb:326
         (module (const $_ $_) ...)
# lib/rubocop/ast/node.rb:327
         (casgn $_ $_        (send (const nil? {:Class :Module}) :new ...))
# lib/rubocop/ast/node.rb:328
         (casgn $_ $_ (block (send (const nil? {:Class :Module}) :new ...) ...))}
# lib/rubocop/ast/node.rb:479
        {(block (send nil? :proc) ...)

from rubocop-ast.

pirj avatar pirj commented on August 27, 2024

I recall someone has predicted rubocop-nodepattern will appear in the future :D

from rubocop-ast.

bbatsov avatar bbatsov commented on August 27, 2024

@marcandre Pretty cool!

I recall someone has predicted rubocop-nodepattern will appear in the future :D

Well, technically that's rubocop-ast. ๐Ÿ˜‰

from rubocop-ast.

pirj avatar pirj commented on August 27, 2024

The idea may be interesting, but probably not worth the effort. I'll close for now.

from rubocop-ast.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.