Commit graph

14 commits

Author SHA1 Message Date
Jimmy Lai
e7315d2c28 remove unused import and run tox -e autofix 2019-11-19 16:09:26 -08:00
Benjamin Woodruff
c10a960fc9 Rename before_visit/after_leave to before_codegen/after_codegen
This addresses @DragonMinded's review here:
https://github.com/Instagram/LibCST/pull/126#pullrequestreview-305577895
2019-10-28 11:05:35 -07:00
Benjamin Woodruff
3bfbb4b2dd Make the codegen enter/leave tracking more generic
I need to do some additional work on visit/leave to make codegen
re-entrant, so this makes it more generic.

This should have an additional small positive effect of creating less
throwaway objects when we're doing codegen without position calculation.
2019-10-28 11:05:35 -07:00
Jennifer Taylor
bb72a0556b Fix deep_clone for sequence types.
It turns out that when we had a sequence type, like Module's body, we were incorrectly
leaning on copy.deepcopy for the rest of the tree. This works, but its likely to blow
the stack when doing a deep clone of a very very large file. Fix that oversight here.
2019-10-23 12:19:09 -07:00
Benjamin Woodruff
12b216339b Move CodegenState construction to PositionProvider
Previously, `libcst.Module.code_for_node` accepted a `provider`
parameter, and would construct the appropriate CodegenState subclass
based on some if/else logic.

This had a few knock-on effects:

- A tighter circular dependency between node definitions and metadata,
  which was previously mitigated with an inner import.

- Adding a new `CodegenState` subclass required the non-obvious task of
  modifying `Module`. I'll need to add a new `CodegenState` subclass to
  support incremental codegen.

- What was intended to be a private implementation detail (how positions
  are computed by hooking into codegen) was exposed as a parameter on a
  public method.

This diff aims to clean up those knock on effects. The position-related
subclasses have been moved from `libcst.nodes._internal` into
`libcst.metadata.position_provider`, which keeps more of the position
computation logic together.

Technically this is a breaking change. If somebody was passing the
second parameter into `code_for_node`, their code will break. However:

- It will break in a clear and obvious way.

- This second parameter was never documented (aside from my recent
  addition of some remarks telling people not to use it). There's plenty
  of documentation that shows how to fetch positions properly.

So it's my opinion that we shouldn't require a major version bump for
this change.
2019-10-22 14:53:04 -07:00
Benjamin Woodruff
e1c1d450ae Export CodePosition and CodeRange from metadata
While these classes are used by the codegen implementation, conceptually
they're part of `libcst.metadata`, so we should export them from
`libcst.metadata` instead of the top-level `libcst` package.
2019-10-18 14:30:33 -07:00
Jennifer Taylor
48ba3b0849 Remove parsing Subscript slice directly into Index/Slice.
This makes sure we always wrap elements in a SubscriptElement, even when there
is only one element. This makes things more regular while still being backwards
compatible with existing creation. The meat of this is in two halves, which can't
be split due to not wanting to break the build between commits. The first half
is just the changes to the parser and updates to tests. This includes a test to
be sure we can still render code that uses old construction types. The second half
is changes to codegen which made assumptions about `Subscript` and demonstrates
the need to make this change in the first place. This includes a fix to
`CSTNode.with_deep_changes` type to make it more correct and also more usable in
transforms without additional type assertions.
2019-10-18 13:40:10 -07:00
Jennifer Taylor
f0c1aa3be9 Add a 'with_deep_changes' helper method.
This allows you to do in one helper what you previously needed to combine
a `with_changes` and `deep_replace` helper call to do.
2019-10-15 17:32:52 -07:00
Jennifer Taylor
27b5a1d286 Improve removing nodes from parents in LibCST.
Add a RemoveFromParent() function as a convenience to returning RemovalSentinel.REMOVE.
Introduce a `deep_remove()` on CSTNode analogous to `deep_replace()` but for removing.
2019-10-02 15:43:48 -07:00
Jennifer Taylor
9fa2612673 Implement deep_replace helper on CSTNode. 2019-10-02 14:00:20 -07:00
Jennifer Taylor
271566bdbf Add a .remove() method to CSTNode as a convenience to returning RemovalSentinel.REMOVE 2019-10-01 15:28:58 -07:00
Jennifer Taylor
1d483ef787 Fix deep_clone behavior regarding BaseLeaf nodes.
We used _visit_and_replace_children to implement deep_clone, which was already
bad since it relied on the implicit behavior of _visit_and_replace_children to
make a copy on visit. If we fix that behavior in the future, deep_Clone would
have broken. However, its even more broken, because nodes that subclass from
BaseLeaf define their _visit_and_replace_children as returning self. So, not
only is this a bad coupling, but its also broken. Implement deep_clone properly
here.
2019-09-25 12:36:33 -07:00
Jennifer Taylor
93ed095c9e Fix non-unique tree generated by LibCST in some circumstances.
When parsing, we don't always fill in defaults unless we have a good reason to. That, coupled with the fact that we use dataclasses that allow you to set a default on creation instead of runtime construction means that we accidentally aliased a whole bunch of SimpleWhitespace nodes. Fix that by switching to the datalasses field() method which allows runtime evaluation. We do this by creating a simple (untyped, unfortunately) helper on CSTNode which makes for easier creation.
2019-09-25 12:36:33 -07:00
Jennifer Taylor
fc430343b5 Fix internal underscore convention.
Standardize on the convention that private modules (those we don't expect people to directly import) are prefixed with an underscore. Everything under a directory/module that has an underscore is considered private, unless it is re-exported from a non-underscored module. Most things are exported from libcst directly, but there are a few things in libcst.tool, libcst.codegen and libcst.metadata that are namedspaced as such.
2019-09-17 13:52:42 -07:00
Renamed from libcst/_nodes/_base.py (Browse further)