Commit graph

1099 commits

Author SHA1 Message Date
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
Jennifer Taylor
1acf2c83d1 Add more test coverage to metadata matcher. 2019-10-21 13:25:20 -07:00
Jennifer Taylor
931fafd37d Hook up matcher metadata retrieval to matcher visitors.
This allows a slightly more convenient way of calling 'matches' with metadata inside a transform/visitor which also supports matcher decorators. It also hooks in automatic metadata connection and lookup for matcher decorators themselves.
2019-10-21 13:25:20 -07:00
Jennifer Taylor
e74ff11528 Implement base version of matchers + metadata
This adds the ability to match on metadata for a node as if it was an attribute on the node. It also opens up the ability to match on node attributes via metadata only. It allows for metadata match specifications with similar flexibility to standard matchers.
2019-10-21 13:25:20 -07:00
Jennifer Taylor
57860f3d76 Fix trailing newline detection around continuation.
If you have such a program like "pass\\\n", this is technically a program without a trailing newline, since line continuations are defined as being a `\` followed by a newline. We were misdetecting this as having a trailing newline, thus making it impossible to parse the continuation. Add some tests to verify this behavior and then fix the problem.

Note that this was found via hypothesis.
2019-10-21 13:24:25 -07:00
Jennifer Taylor
9522da0de2 Skip fuzz testing code with form feeds, due to incompatibility with LibCST. 2019-10-21 13:16:35 -07:00
Jennifer Taylor
fa646790df Fix expression fuzzing by making it only compare if LibCST can parse. 2019-10-21 13:16:35 -07:00
Benjamin Woodruff
1776a7c308 Update tutorial to use renamed PositionProvider
I missed this in #114 when renaming `SyntacticPositionProvider` to
`PositionProvider` because it's a different file extension and I was
only grepping for rst and py files.
2019-10-18 16:04:00 -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
Jimmy Lai
f4eb9d197e [metadata] make QualifiedNameProvider typing more specific 2019-10-18 08:06:01 -07:00
Benjamin Woodruff
94e40d84ac Reorder position providers in documentation
PositionProvider is the best choice in most cases, so it should come
before WhitespaceInclusivePositionProvider in the documentation.
2019-10-17 14:59:52 -07:00
Benjamin Woodruff
9d68a58c17 Rename position provider classes
I discussed the high-level idea here with @DragonMinded a few months
ago, but this isn't set in stone. If people have better ideas for names,
I'd love to hear it.

Publicly-Visible Changes
------------------------

- SyntacticPositionProvider is deprecated. The new name is
  PositionProvider.
- BasicPositionProvider is deprecated. The new name is
  WhitespaceInclusivePositionProvider.
- Documentation is updated to better explain these renamed providers and
  how to use them.

The prefixes "Syntactic" and "Basic" were pretty bad because they're
just concepts that we made up for LibCST.

The idea for the new names is that most users will want the
SyntacticPositionProvider, and so we should name things so that the user
will naturally gravitate towards the correct choice.

There's some argument that we shouldn't even bother exposing
WhitespaceInclusivePositionProvider, but we already need to implement it
as a fallback for PositionProvider, and it might be useful for some
niche use-cases.

Once we have another major version bump, we can remove the old class
names. The old class names have already be removed from the
documentation so that new users aren't tempted to use them.

Internal-Only Changes
---------------------

- `PositionProvider` is now `_PositionProviderUnion`. This type alias
  was never a public API (and probably never will be).
- `BasicCodegenState` is now
  `WhitespaceInclusivePositionProvidingCodegenState`.
- `SyntacticCodegenState` is now `PositionProvidingCodegenState`.
2019-10-17 14:59:52 -07:00
Jennifer Taylor
e533e6ae73 Rename ExtSlice to SubscriptElement in anticipation of further fitxes.
This is somewhat complicated by the fact that we need to not just allow
construction of nodes/matchers using `ExtSlice` still for backwards compatibility,
but we also need to be able to call `visit_ExtSlice` and `leave_ExtSlice` on
old visitors even though the new node is named `SubscriptElement`. The
construction/instance check/matching side of things will work since internally we
refer to everything as `SubscriptElement` and alias `ExtSlice` to this everywhere,
but for string-based function lookup, we need to get a little more clever and make
the default `visit_SubscriptElement` delegate onward to `visit_ExtSlice` so that
either form works.

This can all be removed again once we're past the deprecation period for ExtSlice.
2019-10-16 16:00:27 -07:00
Jennifer Taylor
1dd85d3c52 Refactor matcher class codegen to use common gather functions. 2019-10-16 16:00:27 -07:00
Jennifer Taylor
d4e33f264e Disallow adding @visit/@leave to concrete visitors. 2019-10-16 12:48:20 -07:00
Jennifer Taylor
0de26fc769 Address TODO in visitor type checking using mapping of CSTNode to valid return types. 2019-10-16 12:48:20 -07:00
Jennifer Taylor
704fe2bc3a Codegen valid return types mapping for each CSTNode class. 2019-10-16 12:48:20 -07:00
Benjamin Woodruff
3c92a7b367 Tweak wording/formatting of the scope provider intro
Explaining the implementation details of scopes to someone unfamiliar
with compilers can be tricky. Hopefully this helps.

- Rephrased the definition of a scope to be more applicable to Python
  (remove references to "blocks"), and made it use an example for
  (hopefully) better clarity.
- New scopes are also created for comprehensions.
- Set a fixed width (400px) for the scope diagram, since it was too
  large before.
- Tweaked some tenses.
- Add a final call to action: "LibCST allows you to inspect these
  scopes"
2019-10-15 18:02:51 -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
Jimmy Lai
c88ff66aef bump version to 0.2.1 2019-10-14 10:30:58 -07:00
Jimmy Lai
703cb9f3b4 Use import libcst as cst and explicit keyword arg for consistency and readability 2019-10-14 10:30:39 -07:00
Jimmy Lai
578f161acb use list instead of tuple for sequence in docs for readability 2019-10-14 10:29:32 -07:00
Jimmy Lai
242bd7a7f6 add docs for <comprehension> qualified name 2019-10-11 12:37:50 -07:00
Jimmy Lai
94fe0bfa3f handle ComprehensionScope in QualifiedName 2019-10-11 12:37:50 -07:00
Jennifer Taylor
ba471909c4 Generalize codegen cleanup step and apply it to all codegen.
I noticed that the typed visitors codegen was creating messy types such as Union[SingleType]. We have a clean-up for Union[SingleType] snuck into gen_matcher_classes. So, lets remove that snuck-in clean-up from gen_matcher_classes and apply it globally to all codegen. This makes its purpose a lot more obvious, while helping decouple necessary codegen from prettifying/simplifying transforms. It also cleans up typed visitors, so that's a bonus.
2019-10-11 12:29:23 -07:00
Jennifer Taylor
76dbfd65b9 Explicitly define hash and equality for matchers.
This makes matcher equality and hash equivalent to the way LibCST nodes behave. Not only does this make us more consistent, but it also fixes a bug where matcher decorators could not be used with a matcher that initialized a sequence type as a list.
2019-10-11 11:46:06 -07:00
Jennifer Taylor
bad27b68a2 Enforce immutability with special matchers. 2019-10-11 11:46:06 -07:00
Benjamin Woodruff
194a930f64 Make BaseMetadataProvider[T] covariant over T
Because we'd consider `BaseMetadataProvider[int]` to be a subtype of
`BaseMetadataProvider[object]`, it should be covariant over its
typevar, rather than invariant.

This isn't entirely correct because we have a mutable data structure
(`_computed`) that depends on the typevar, and pyre points this out
(though with a really confusing error message). However, it's not
correct to say that `BaseMetadataProvider` is invariant either, so I
think this is the lesser evil.

I don't think it's practical to redesign this API to avoid the variance
issue, so I'm ignoring the new type error that results from this change.

I think this may resolve some of the issues we've seen internally with
D17820032.
2019-10-10 18:08:44 -07:00
Jennifer Taylor
435ffc4bca Clean up on-call specification leftover from open-sourcing. 2019-10-10 18:07:14 -07:00
Jennifer Taylor
ba64cb9d4e Remove useless internal-only noqa. 2019-10-09 21:51:56 -07:00
Jennifer Taylor
4948c6a5f5 Add missing license header to matchers generator file. 2019-10-09 21:51:56 -07:00
Jimmy Lai
3965e4601b [doc] add docstring to QualifiedName 2019-10-09 11:32:08 -07:00
Jimmy Lai
5f8f5c5624 address review feedback form #94 2019-10-08 15:48:59 -07:00
Jimmy Lai
b690c591e5 [doc] add Scope Analysis tutorial 2019-10-08 15:48:59 -07:00
Jimmy Lai
9e7a9879ea [doc] fix expression context usage in scope api and explain access don't handle simple string typing access 2019-10-08 15:48:59 -07:00
Jimmy Lai
dea7c20794 add warning box to clarify Scope metadata only handles local variable 2019-10-08 15:48:59 -07:00
Jimmy Lai
9f8e69b1b3 [doc] use libcst.metadata for referencing metadata classes and add docstring for Scope magic functions 2019-10-08 15:48:59 -07:00
Jimmy Lai
c9dc3bfa70 address review feedback in #94 2019-10-08 15:48:59 -07:00
Jimmy Lai
13637b7d58 add docs for Assignments and Accesses 2019-10-08 15:48:59 -07:00
Jimmy Lai
855232e24f test Access.referents and Assignment.references 2019-10-08 15:48:59 -07:00
Jimmy Lai
65cc4d23bd add test case for Assignments and Accesses 2019-10-08 15:48:59 -07:00
Jimmy Lai
392426d6bd add Scope.record_access and Access.record_assignment 2019-10-08 15:48:59 -07:00
Jimmy Lai
ee14c48e46 add Scope.assignments and Scope.accesses 2019-10-08 15:48:59 -07:00
Jimmy Lai
7a2d0048c7 add deprecation message to Assignment.accesses 2019-10-08 15:48:59 -07:00
Jimmy Lai
120fd04ce2 rename Assignment.accesses as Assignment.references and add Accesses/Assignments 2019-10-08 15:48:59 -07:00
Christopher Hunt
6520d25816 Fix visitor helper function name pattern in docs 2019-10-04 21:40:52 -07:00
Jimmy Lai
c92bc02d8c rewrite ExpressionContextProvider to handle nested List/Tuple and Attribute 2019-10-04 15:16:55 -07:00
Jennifer Taylor
08b6657db2 Bump version to 0.2.0, update changelog. 2019-10-04 13:13:30 -07:00
Jennifer Taylor
8a8ea4b27b Upgrade Pyre to 0.32.
This fixes a TODO, removes several ignore statements and most importantly, addresses a massive speed regression triggered by importing matchers.
2019-10-04 11:05:37 -07:00