Commit graph

418 commits

Author SHA1 Message Date
Jennifer Taylor
4ce17d43e9 Add some tests for uncovered areas of f-string codemod. 2019-12-12 15:55:03 -08:00
Jennifer Taylor
f7e13df680 Apply correct copyright header to all open-sourced files. 2019-12-12 15:34:13 -08:00
Jennifer Taylor
23accb47a8 Resolve full path of formatter so that it can be found in subprocess. 2019-12-12 11:31:05 -08:00
Jennifer Taylor
9d06c3e860 Allow wrapping scripts to override the name of the command when printing tool help. 2019-12-12 11:31:05 -08:00
Jennifer Taylor
5b81857673 Change codemod command help to print out description of codemod in question if it is available. 2019-12-12 11:31:05 -08:00
Jennifer Taylor
508927aabf Run add strict codemod across codemod directory, fix pyre errors. 2019-12-12 11:31:05 -08:00
Jennifer Taylor
f5c5f296fd Open-source a decent chunk of utility codemods that aren't instagram-specific. 2019-12-12 11:31:05 -08:00
Jennifer Taylor
1fcf279eed Add descriptions to all tool help, plumb tool name all the way to top level. 2019-12-12 11:31:05 -08:00
Jennifer Taylor
2f472d16b5 Add a utility to libcst.tool which can list out all available codemods in the current configuration. 2019-12-12 11:31:05 -08:00
Jennifer Taylor
196ca197c4 Add a utility to libcst.tool that can initialize a directory with a default LibCST codemod config. 2019-12-12 11:31:05 -08:00
Jennifer Taylor
9459b581f4 Add a config file format to allow customization of LibCST codemod runner. 2019-12-12 11:31:05 -08:00
Jennifer Taylor
fcb9ac71be Add some tests to fix missing coverage. 2019-12-12 11:28:53 -08:00
Jennifer Taylor
12946fcae4 Remove useless oncall designations for open-sourced files. 2019-12-12 11:28:53 -08:00
Jennifer Taylor
20acb169db Rename overrideable function to be public. 2019-12-12 11:28:53 -08:00
Jennifer Taylor
c2900b5a39 Initial work to open-source codemod runner frontend, integrating it into libcst.tool. 2019-12-12 11:28:53 -08:00
Jennifer Taylor
8854b42735 Separate base testing class to a mixin so that it can be used with alternative testing frameworks. 2019-12-12 11:28:53 -08:00
Jennifer Taylor
69fd60edcf Initial work to open-source codemod framework core. 2019-12-12 11:28:53 -08:00
Zach Hammer
20a73807c1 Fix black formatting 2019-12-06 10:58:50 -08:00
Zach Hammer
e211bea16c Fix inconsistent type override errors
It seems that @mark_no_op was not correctly preserving type signatures
in pyre previous to this change. Preserving type signatures in the
decorator caused some pyre errors in tests where visitors are defined
that don't share the same arg names as their parent.
2019-12-06 10:58:50 -08:00
Zach Hammer
745cf3a344 Use bound=Callable directly
As per @dragonminded's suggestion. This seems to work on mypy
2019-12-06 10:58:50 -08:00
Zach Hammer
8e6bf9e982 Just use plain Callable 2019-12-06 10:58:50 -08:00
Zach Hammer
6b696c2339 Replace "Any" with ... 2019-12-06 10:58:50 -08:00
Zach Hammer
6e19cc53ec Try removing nested TypeVar to fix pyre
from circleci pyre tests:

> libcst/_typed_visitor_base.py:15:0 Invalid type [31]: Expression `Variable[F (bound to typing.Callable[[libcst._typed_visitor.CSTTypedBaseFunctions, Variable[libcst._typed_visitor_base.T]], None])]` is not a valid type. Type variables cannot contain other type variables in their constraints.
2019-12-06 10:58:50 -08:00
Zach Hammer
70c7341225 Fix mark_no_op decorator type preservation on mypy
This seems to fix #161 for me locally, follows
https://mypy.readthedocs.io/en/latest/generics.html#declaring-decorators
2019-12-06 10:58:50 -08:00
Jennifer Taylor
9c6ccbb80e Bump LibCST to 0.2.5. 2019-12-05 16:36:09 -08:00
Jennifer Taylor
e4457425aa Implement a replace function.
This takes a tree, a matcher, and a replacement, replacing all nodes in the tree
which match the shape of the matcher with the replacement.
2019-12-05 14:11:22 -08:00
Jennifer Taylor
c9b10fe427 Deprecate "default_params" on Parameters in preparation for 3.8 position-only params. 2019-12-03 10:56:01 -08:00
Jennifer Taylor
0717ffa097 Implement an extractall function.
This is equivalent to calling `findall` on a node, and then calling `extract` with the same
matcher on each of the returned nodes from `findall`.
2019-11-21 15:37:37 -08:00
Jennifer Taylor
79df3af5ce Make matcher type hint exception pickleable. 2019-11-20 16:29:46 -08:00
Jimmy Lai
e7315d2c28 remove unused import and run tox -e autofix 2019-11-19 16:09:26 -08:00
Jimmy Lai
337abb26c9 [metadata] consolidate MetadataWrapper methods to the same file 2019-11-19 12:46:37 -08:00
Jimmy Lai
0d77f653ec remove redundant code 2019-11-19 12:46:29 -08:00
Jennifer Taylor
8cd4a87391 Implement an `extract` function, which will extract child nodes/sequences/metadata given a successful match. 2019-11-19 12:06:10 -08:00
Jimmy Lai
b3fd4cf268 [metadata] fix typo and improve error message 2019-11-18 16:56:17 -08:00
Jennifer Taylor
64e298b2cc Fix batchable visitor not calling attribute visitors. 2019-11-14 11:28:15 -08:00
Jennifer Taylor
d1c2979b3a Remove length restrictions for AllOf/OneOf so they can be used with sequence expansion operators. 2019-11-14 11:27:46 -08:00
Jennifer Taylor
652ddca974 Bump LibCST to 0.2.4. 2019-11-13 17:56:09 -08:00
Jennifer Taylor
35b308e73b Fix not widening matcher sequence types inside unions. 2019-11-13 15:35:25 -08:00
Jennifer Taylor
ec7ee6f7d7 Bump LibCST to 0.2.3. 2019-11-12 10:58:28 -08:00
Thomas Mansencal
761c33f2dd Fix typo in "why_libcst.rst" file. 2019-11-09 17:58:45 -05:00
Thomas Mansencal
cda91dfaae Fix typo in "scope_tutorial.ipynb" file. 2019-11-09 17:57:34 -05:00
Jimmy Lai
f9628ca767 [metadata] add for loop support to ExpressionContext 2019-11-08 12:20:32 -05:00
Jennifer Taylor
f5079f6b39 Implement a MatchMetadataIfTrue matcher.
This is especially helpful for checking qualified names of nodes against one item
or a list of items that you wish to match against. I chose to create a new matcher
instead of widening the type of `MatchMetadata` to take in either a value or
a callable. I was originally going to do the former, but having a MatchIfTrue
and a MatchMetadataIfTrue felt more orthogonal and became easier to explain than
a single MatchIfTrue that could take two types of values.
2019-11-08 08:22:27 -08:00
Jennifer Taylor
675f8b3e82 Implement 'findall' in matcher API.
Findall does what you expect given a matcher and a tree: It returns all nodes
that exist in that tree which match the given matcher. For convenience, findall
works with regular LibCST trees as well as MetadataWrappers. It is also provided
as a helper method on the matcher transforms.
2019-10-31 17:16:29 -07:00
Jimmy Lai
e6b84fc11a [scope] add qualified name support for imported attribute 2019-10-31 13:08:36 -07:00
Jennifer Taylor
17fa6add8a Redo matcher codegen to be much faster and smaller output.
This does a few things, since it was easier to combine than separate:
 - Uses type aliases instead of fully-unrolled types, making it far easier for a human to read.
 - Makes codegen approximately 3x faster, which has the side effect of halving our test run times.
 - Consolidate the way we use type aliases by dropping the MetadataPredicate type for now, increasing consistency.
 - Widen types for AllOf/OneOf matchers to allow for MatchIfTrue since this is supported under the hood.

This results in a generated matchers file that is 1/3 the size it was, more readable by a human, and most importantly, faster to codegen, parse, format and typecheck.
2019-10-31 12:54:26 -07:00
Jennifer Taylor
75ebd2c3ed Fix string sanitization to not change things such as '"'. 2019-10-31 12:54:26 -07:00
Benjamin Woodruff
42c3f3f267 Add an unsafe_skip_copy option to MetadataWrapper
In certain cases (e.g. inside Instagram's lint framework) we know that
our tree originates from the parser, so we know that there shouldn't be
any duplicate nodes in our tree.

MetadataWrapper exists to copy the tree ensuring that there's no
duplicate nodes.

This diff provides an escape hatch on MetadataWrapper that allows us to
save a little time and avoid a copy when we know that it's safe to skip
the copy.

As part of this, I ran into some issues with `InitVar` and pyre, so I
removed `@dataclass` from the class. This means that this is techincally
a breaking change if someone depended on the MetadataWrapper being an
actual dataclass, but I think this is unlikely. I implemented `__repr__`
and added tests for hashing/equality behavior.
2019-10-29 13:35:38 -07:00
Jennifer Taylor
f889c0993d Update matchers docs with some clarification based on user feedback. 2019-10-28 18:13:55 -07:00
Jennifer Taylor
b5789d42ad Use official release of Black now that it is out.
We don't need to worry about formatting changes since we already paid the cost
in b3253de9b8. So, now that there's a new 3.8-compatible
Black, lets use it!
2019-10-28 18:11:17 -07:00