Commit graph

100 commits

Author SHA1 Message Date
Ray Zeng
89fb7fe524 Calculate syntactic position for statement nodes (1)
This commit covers most simple statements and if statements.
2019-07-26 12:48:22 -07:00
Jennifer Taylor
3fb60b9706 Make parser package private
Now that this is imported from the top level, make it private.
2019-07-26 12:48:22 -07:00
Jennifer Taylor
1190dc8a2f Import parser from the top level
This was a pain, because apparently we can't refer to
"static_analysis.libcst" as a module before its finished executing, due
to https://bugs.python.org/issue25294. To combat this, I had to change
to direct importing inside parser, which was a bit of a pain due to the
number of nodes used. But, it works.
2019-07-26 12:48:22 -07:00
Jennifer Taylor
818a7e63df Make nodes package private
Now that nodes are imported from the top level, make the nodes
directory private.
2019-07-26 12:48:22 -07:00
Jennifer Taylor
032cd49e6d Import nodes from top level instead of libcst.nodes 2019-07-26 12:48:22 -07:00
Benjamin Woodruff
eae98942fd Implement Dict node
This implements the required nodes, but not the parsing logic (that'll
be a later diff).

To support `Dict`, this diff also introduces `BaseDictElement`,
`DictElement`, `StarredDictElement`, and `BaseDict`.

It also refactors some of the logic in `BaseSet` into `_BaseSetOrDict`
so that it can be shared between `BaseSet` and `BaseDict`.

`DictComp` will come later.
2019-07-26 12:48:22 -07:00
Jennifer Taylor
3dec7be6ef Export things from libcst's base__init__.py
Go through and clean up where we export things from in the top level
directories. This is the first pass at cleaning up the public API. In
this diff I clean up all top-level exports that were coming out of .py
files and instead export them from libcst's base module itself.

I also didn't export a few of the things defined in newly underscored
files because they were only used internally.
2019-07-26 12:48:22 -07:00
Ray Zeng
6013be76e3 Disable deep cloning during metadata resolution
This is a temporary change until we have a better metadata API in place.
2019-07-26 12:48:22 -07:00
jimmylai
0b8422cee2
add copyright header to custom.css 2019-07-24 19:30:21 -07:00
jimmylai
93bf52b2ed
use cache to speedup pyre CI job 2019-07-24 19:25:31 -07:00
jimmylai
f059638b08
Fix circleci cache to speed up CI builds 2019-07-24 15:09:57 -07:00
jimmylai
14ee31dc6a [doc] Why libcst?
* reuse README intro section in doc

* [doc] Why LibCST?

* reuse README intro section in doc

* [doc] Why LibCST?
2019-07-24 12:19:00 -07:00
jimmylai
13ff089dad cache tox env files to speed up CI jobs 2019-07-24 12:18:28 -07:00
Jennifer Taylor
5d1ba5d465 Add a changelog file. 2019-07-24 12:11:50 -07:00
jimmylai
c0764745fa reuse README intro section in doc 2019-07-23 18:57:47 -07:00
Benjamin Woodruff
27e816059b Change autodoc member order to "bysource"
Without this option, autodoc defaults to ordering all members of
modules and classes alphabetically.

While this is normally fine (and probably desirable), the ordering of
fields in dataclasses is significant, so we need to change this behavior
to preserve the original member ordering.
2019-07-23 17:18:32 -07:00
jimmylai
c621875c81 use rst format for README 2019-07-23 16:10:03 -07:00
Jennifer Taylor
161bc32b1a Rename 'matchers'
These aren't really matchers as much as they are the auto-generated
function stubs for visit/leave functions. So, rename them as such. This
isn't the best name either, but I think it gets us closer to what's
really the purpose.
2019-07-23 13:13:53 -07:00
Jennifer Taylor
7903c35b62 Optionally don't traverse children
Give node-based visitors the ability to state that they don't want to
visit their children. I use this in add_none_throws to skip entire
modules that I don't want to modify (non-tests) and to skip looking at
comments that are irrelevant. This should also make add_none_throws
faster since we skip visiting a lot of node types.
2019-07-23 13:13:53 -07:00
Jennifer Taylor
60763fc5dc Implement an ensure_type helper
We already have several places in unit tests that need this, as well
as a spot in an existing codemod that could benefit from it. So,
implement ensure_type which can be used to refine the type of a node to
an exact node type. I purposefully left the node type as 'object'
instead of libcst.CSTNode so that it could be used with RemovalSentinel
or MaybeSentinel without needing to import these.
2019-07-23 13:13:53 -07:00
Venkat Subramaniam
ffd9e4dd21 CallTest fix
Making tests more readable in libcst.
2019-07-23 13:13:53 -07:00
jimmylai
120cfa9229 add sphinx autodoc templates and docs CI job 2019-07-22 20:05:27 -07:00
Ray Zeng
3be6c0df3d Export CodePosition/CodeRange; fix pickle with add_slots
This commit supports some use-cases internal to Instagram.
2019-07-22 20:05:07 -07:00
Ray Zeng
55d0e759ec Inspect MRO chain and merge metadata dependencies
All classes that inherit from `_MetadataInterface` now have access to
any metadata declared in any of their base classes. Inherited metadata
is resolved by the metadata runner upon visiting a module.
2019-07-22 20:05:07 -07:00
Jimmy Lai
9bc30ab53d Sphinx autodoc: use #: for class attribute docstring
http://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#directive-autoattribute
2019-07-22 20:05:07 -07:00
Jimmy Lai
badb900d82 Add missing @add_slots to CST nodes 2019-07-22 20:05:07 -07:00
Benjamin Woodruff
f2228e2e61 Remove Instagram-specific bits from flake8 config 2019-07-22 20:05:07 -07:00
Benjamin Woodruff
46799cefcf Fix flake8 errors
This is a combination of:
- Fixing some issues upstream.
- Modifying our cleanup script to carry over noqa comments, and
  copying over the new versions of some files.
- A small addition to our flake8 config.
2019-07-22 20:05:07 -07:00
jimmylai
4b358efca6 Add CI build status badge to Readme
Based on https://circleci.com/docs/2.0/status-badges/
Example from other project https://github.com/python/cpython
2019-07-22 20:05:01 -07:00
Jennifer Taylor
dbe4a1be5b Fix some pyre errors in test
Some of these crept in before we had type-checking on node
constructors or decorators. So, lets fix these cases and get rid of a
few incorrect constructions in tests!
2019-07-22 20:04:37 -07:00
Venkat Subramaniam
a6ab27ecee Fix BooleanOperationTest
Changing data provider in test to be more readable.
2019-07-22 20:04:37 -07:00
Benjamin Woodruff
4f0eb3b043 Fix whitespace handling around strings
Strings are safe to use with a word operator in many cases, so this
fixes it to support that.

This was causing some parser errors in
`commerce/shopping_recommendations_service_shots.py` due to some whacky
f-strings. We weren't encountering those errors before, because of other
f-string parsing issues that we were hitting first.
2019-07-22 20:04:37 -07:00
Venkat Subramaniam
f4f6381849 AwaitTest fix
Currently, a lot of libcst node tests use data providers that use tuples
instead of dictionaries. This just makes the tests unreadable, so this
is one of several diffs aiming to solve this issue.
2019-07-22 20:04:37 -07:00
Venkat Subramaniam
3e088dd613 AttributeTest fix
Currently, a lot of libcst node tests use data providers that use tuples
instead of dictionaries. This just makes the tests unreadable, so this
is one of several diffs aiming to solve this issue.
2019-07-22 20:04:37 -07:00
Venkat Subramaniam
f4797945b3 Fix BinaryOperationTest
Currently, a lot of libcst node tests use data providers that use tuples
instead of dictionaries. This just makes the tests unreadable, so this
is one of several diffs aiming to solve this issue.
2019-07-22 20:04:37 -07:00
Venkat Subramaniam
0b08682f3e AtomTest fixes
Currently, a lot of libcst node tests use data providers that use tuples
instead of dictionaries. This just makes the tests unreadable, so this
is one of several diffs aiming to solve this issue.
2019-07-22 20:04:37 -07:00
Venkat Subramaniam
f162b37b9c Changing data provider to accept dictionaries in testAssign
Currently, a lot of libcst node tests use data providers that use tuples
instead of dictionaries. This just makes the tests unreadable, so this
is one of several diffs aiming to solve this issue.
2019-07-22 20:04:37 -07:00
Venkat Subramaniam
9874534789 AssertTest fix
Changing all tests to accept kwargs instead of regular arguments.
2019-07-22 20:04:37 -07:00
Ray Zeng
0b0ffb8824 Fix batched vistor dependency resolution
Adds logic to gather metadata dependencies from `BatchableCSTVisitors`
when constructing a batched visitor.
2019-07-22 20:04:37 -07:00
Ray Zeng
36cfb512ea Refactor metadata runner
Refactors various metadata related classes to better reflect intended
usage of the API.

The metadata runner now deep clones the module and returns a copy
containing the metadata information. Metadata providers also return the
tree to enforce the idea that the tree is immutable (even though no
copying is actually done and providers write directly to the original
tree).
2019-07-22 20:04:37 -07:00
Venkat Subramaniam
e452bd3a55 Changing data provider to accept dictionaries in testAssert
Currently, a lot of libcst node tests use data providers that use tuples
instead of dictionaries. This just makes the tests unreadable, so this
is one of several diffs aiming to solve this issue.
2019-07-22 20:04:37 -07:00
Benjamin Woodruff
a6aee836b0 Fix parsing of ConcatenatedString with mixed f-strings and simple strings
Previously, while `f"abc" f"def"` and `"abc" "def"` would parse as a
ConcatenatedString, `f"" ""` wouldn't because it mixes f-strings and
simple strings.

This joins `atom_string` and `atom_fstring` into a single `atom_string`
rule, which has an added benefit of simplifying the code.
2019-07-22 20:04:37 -07:00
Ray Zeng
f1232d9190 Record syntactic position of more expressions
Update codegen and tests to cover list, list comprehension and generator
expressions. Also cleans up some extraneous position tracking code in
codegen.
2019-07-22 20:04:37 -07:00
Jennifer Taylor
3b3f52673a Rework 'pass' on removal behavior
Instead of generating Pass nodes on removal, allow empty IndentedBlock,
SimpleStatementLine and SimpleStatementSuite nodes. Have them insert a
'pass' token manually when they encounter having nothing inside of
them. This allows us to more easily figure out when we should keep a
node around, and means that we no longer construct new nodes with
surprising subnodes based on somewhat-related user input. It also
matches how we do MaybeSentinel support for the rest of Python's
syntactic sugar. As a bonus (well, the point of this diff actually), we
no longer add a pass statement every time we remove another statement,
only when we actually need to.
2019-07-22 20:04:37 -07:00
Benjamin Woodruff
656f61b810 Implement nodes and parser logic for Set and SetComp
This supports the syntax:

```
myset = {1, 2, 3}
```

and

```
myset = {i + 1 for i in range(3)}
```
2019-07-22 20:04:37 -07:00
Jennifer Taylor
f9f7a202f7 Remove Number
Remove Number wrapper node and go back to just having UnaryOperation.
2019-07-22 20:04:37 -07:00
Ray Zeng
4b5a6a969e Change entry point to metadata runner
Changes `CSTNode.visit()` to `CSTNode._visit_impl()` and uses `visit()`
as a public entry point to prevent users from running visitors with
metadata dependencies on nodes other than Modules.
2019-07-22 20:04:37 -07:00
Ray Zeng
7c51249ec8 Convert metadata computation to use batched visitors
Converts metadata providers to extend from either CSTVisitor or from
BatchableCSTVisitor and updates the runner to batch providers when
possible. There is a known issue in this diff where metadata may be
computed multiple times on the same pass which is addressed in
a later diff. There should be no correctness issues in this diff as
metadata computation should have no side effects.
2019-07-22 20:04:37 -07:00
Ray Zeng
9e622ac74e Define batchable visitors
Defines a batchable visitor API for libcst.
2019-07-22 20:04:37 -07:00
Ray Zeng
64e7ba4e9a Define mutating/non-mutating visitors for the CST
Defines two new visitor base classes for libcst:

- `CSTTransformer` provides an visitor that can be used to create a new
  tree. This is a drop in replacement for the old `CSTVisitor` in
  `_base_visitor.py`.
- `CSTVisitor` provides a visitor that does not allow the user to mutate
  the tree by restricting access to the updated_node in `on_leave`.
2019-07-22 20:04:37 -07:00