Commit graph

110 commits

Author SHA1 Message Date
Jimmy Lai
2e788a25dc
[doc] use cst node in ensure_type in best practice doc (#361) 2020-08-05 12:57:37 -07:00
Zsolt Dollenstein
6a5e7394c0
[RemoveUnusedImports] Support string type annotations (#353)
* [RemoveUnusedImports] Support string type annotations

This PR adds support for detecting imports being used by string type
annotations, as well as imports suppressed by comments.

It breaks up the existing visitor into multiple smaller, single-purpose
visitors, and composes them together.
2020-07-31 15:54:39 +01:00
jimmylai
c023fa7c4c
[typing] enable Pyre strict mode by default (#313)
Co-authored-by: Jimmy Lai <jimmylai@fb.com>
2020-06-12 18:24:18 -07:00
Josie Eshkenazi
ed1a2ad4d1
Add fb open source required links (#307)
Links to Privacy Policy and Terms of Use in README.rst and docs/index
2020-06-04 14:27:54 -04:00
Zsolt Dollenstein
477a03e3a8
[ScopeProvider] Expose more granular Assignments and Accesses for dotted imports (#284) 2020-04-21 10:27:47 +01:00
jkarres
621d9a949a typo fix 2020-04-05 10:50:10 -07:00
Pradeep Kumar Srinivasan
1b9a52b074 Apply annotations from stub.
Basically move the apply_annotations code from Pyre.

Make it a ContextAwareTransformer named ApplyTypeAnnotationsVisitor. Use GatherImportsVisitor to collect imports. Add a static method `add_stub_to_context` so that users can schedule a stub whose types are to be applied later. Use `assertCodemod` and `data_provider` in the tests. Add documentation. Remove fixmes.
2020-03-23 09:16:37 -07:00
Tim Hatch
ef56771900 Add comment to docs 2020-03-05 07:00:24 -08:00
Zsolt Dollenstein
bd31d3feac add docs and expose provider in libcst.metadata 2020-02-28 09:28:11 -08:00
Jennifer Taylor
0ff9a6c5b7 Add a visitor which can remove unused imports using scope analysis. 2020-02-18 16:02:10 -08:00
Jennifer Taylor
8479513e8f Add a visitor that can gather exports specified in the __all__ module identifier. 2020-02-18 16:02:10 -08:00
Jimmy Lai
abd37df6b6 [doc] add get_full_name_for_node_or_raise 2020-02-12 12:03:57 -08:00
Jennifer Taylor
63f7c5a5d8 Fix typing issue with subscript elements where visitors of Index and Slice could not return each other. 2020-02-03 10:30:29 -08:00
Jimmy Lai
7b474703a0 [doc] fix indentation 2020-01-29 05:25:30 +08:00
Jennifer Taylor
e48aa6eaf7 Implement basic templated statement/expression/module parsing.
This has some corner cases that we do not currently handle, but those will come in a later commit.
2020-01-23 10:17:47 -08:00
Jennifer Taylor
fdd5cbbfe6 Implement parsing of posonly param indicator ('/'). This brings us to full 3.8 compatibility. 2020-01-17 10:57:48 -08:00
Jennifer Taylor
6eb6ec7b1d Add posonly parameter support to Parameters node.
This is part one of a two-part change to support the posonly param indicator "/".
2020-01-15 11:18:12 -08:00
Jimmy Lai
0d01792e6d expose all helpers in libcst.helpers for convenience 2020-01-14 15:11:48 -08:00
Jimmy Lai
257fd5755e move ensure_future to libcst.helpers.expression 2020-01-10 12:15:38 -08:00
Jimmy Lai
dffb27b5b9 add get_full_name_for_expression helper 2020-01-10 12:15:38 -08:00
Jennifer Taylor
92050fac39 Fix a few typos in docs. 2020-01-08 18:08:30 -08:00
Jennifer Taylor
3ca41ffabc Remove deprecated position provider objects. 2020-01-08 17:19:03 -08:00
Jennifer Taylor
40e19ac457 Remove deprecated ExtSlice. 2020-01-08 17:18:19 -08:00
Jennifer Taylor
b319312de3 Add docstring for CodemodTest. 2020-01-08 17:17:48 -08:00
Jimmy Lai
3c4c282bbe add documents for TypeInferenceProvider and FullRepoManager 2019-12-28 20:51:29 -08:00
Jennifer Taylor
9bfc4faea9 Add documentation for libcst.codemod and friends. 2019-12-17 11:59:51 -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
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
8cd4a87391 Implement an `extract` function, which will extract child nodes/sequences/metadata given a successful match. 2019-11-19 12:06:10 -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
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
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
Benjamin Woodruff
88ac18872c Add a reentrant (incremental) codegen API
**Context:** This is an experimental performance optimization that we're
hoping to use for our internal linter at Instagram. I added some
documentation, but it's unsupported, and isn't very user-friendly.

This adds `ExperimentalReentrantCodegenProvider`, which tracks the
codegen's internal state (indentation level, character offsets,
encoding, etc.) and for each statement, it stores a `CodegenPartial`
object.

The `CodegenPartial` object has enough information about the previous
codegen pass to run the codegen on part of a tree and patch the result
back into the original module's string.

In cases where we need to generate a bunch of small independent patches
for the same file (and we can't just generate a new tree with each patch
applied), this *should* be a faster alternative.

I don't have any performance numbers because I still need to test this
end-to-end with our internal codebase, but I'd be shocked if it was
slower than what we're doing.

This could theoretically live outside of LibCST, but it depends on a
whole bunch of LibCST internals, so there's some value in making sure
that this is in sync with the rest of LibCST.
2019-10-28 17:23:26 -07:00
Jennifer Taylor
ff65afaa26 Add a best practices section with various recommended practices.
There are going to be many more where this comes from, but these are the three cases that have come up most frequently, so I started with these. Hopefully this helps give additional direction to people using LibCST.
2019-10-23 16:50:47 -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
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
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
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
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
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
13637b7d58 add docs for Assignments and Accesses 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
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
Jimmy Lai
7b96b070ca Organize document menu as three parts: introduction, tutorial and reference 2019-10-02 12:26:35 -07:00