Comments (11)
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.
I have a working proof of concept of a NodePattern
compiler, working basically exactly as parser
works (it generates ::Parser::AST::Node
s 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.
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.
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.
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.
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.
from rubocop-ast.
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.
I recall someone has predicted rubocop-nodepattern
will appear in the future :D
from rubocop-ast.
@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.
The idea may be interesting, but probably not worth the effort. I'll close for now.
from rubocop-ast.
Related Issues (20)
- rubocop-ast-0.4.0 has broken Layout/LineLength HOT 4
- Fix `traversal`
- Changelog link is a 404 HOT 3
- CI: check JRuby and other platforms
- Broken links in "Using Node Matcher Macros" documentation HOT 1
- Request: allow method calls on constants HOT 5
- NodePattern for numblock "count" element HOT 6
- `global_const?` and some other matchers should be available to cops
- Documentation for named `_` and backreferences in node pattern HOT 3
- Undefined method emit_forward_arg= in AST::Builder:Class HOT 3
- `ProcessedSource#ast_with_comments` doesn't differentiate between identical nodes HOT 4
- Issues with `parent_module_name`
- Missing files when attempting to do a gem build HOT 3
- `send_node.block_node` does not handle `numblock`s
- Long-shot parser -> lib-ruby-parser transition for performance boost? HOT 1
- Basic documentation of Node Pattern HOT 1
- Why is the live debugger not online anymore ? https://nodepattern.herokuapp.com/ HOT 4
- Do we need an MasgnNode ? HOT 2
- Experimental Prism feature listed as runtime dependency HOT 11
- Changelog for v1.31.2 is missing HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rubocop-ast.