Commit graph

17 commits

Author SHA1 Message Date
Ray Zeng
39e4e433d8 Refactor CodePosition
Renames `CodePosition` to `CodeRange` and stores start and end position
as a `CodePosition` dataclass to avoid the ambiguity of using tuples.
2019-07-22 19:53:49 -07:00
Ray Zeng
d0ecb018e1 Port line and column numbers to metadata framework
Refactors codegen to write position information to the `__metadata__`
fields in nodes keyed by `BasicPositionProvider` and
`SyntacticPositionProvider` as defined in position_metadata.py.

This commit also updates `deep_equals` to ignore dataclass fields that
are marked `compare=False` to avoid comparing metadata when doing
equality checks.
2019-07-22 19:53:49 -07:00
Benjamin Woodruff
8fba418f2c Add runtime type validation support
This ports `CSTNode.validate_types_shallow` and
`CSTNode.validate_types_deep`, as well as `libcst._type_enforce` to the
open-source release.

These are useful if someone wants to use LibCST without a static type
checker.

These weren't originally included because `libcst._type_enforce` wasn't
3.7 compatible.
2019-07-22 19:53:49 -07:00
Ray Zeng
a19fa41780 Define metadata base class and provider
Define base classes for metadata API in libcst/metadata and adds the
`__metadata__` field to `CSTNode` and `get_metadata()` method to
`CSTVisitor`.
2019-07-22 19:53:49 -07:00
Ray Zeng
8984ce45b2 Semantic position of expression nodes
Updates codegen to also track semantic position of expression nodes
(found in _expression.py).
2019-07-22 19:53:49 -07:00
Ray Zeng
f2f2510297 Define a context manager that can be used to track "semantic" start and end positions for nodes
Created a context manager `record_semantic_position()` in `CodegenState`
to be used for tracking semantic positions of nodes in the CodegenState.
2019-07-22 19:53:49 -07:00
Ray Zeng
d3544824fc Wrap _codegen methods in a helper function to track where nodes start and end.
Converts `_codegen` methods into `_codegen_impl` to wrap implementations
to calls to update the position of each node in the `CodegenState`. The
stored position is the syntactic position of a node (that includes any
whitespace attached to that particular node).

Also updates implementation of tool and `CSTNode.__repr__` to not print
fields of `CSTNode` objects prefixed with "_".
2019-07-22 19:53:49 -07:00
Ray Zeng
65cea1ce21 Rename indent to indent_tokens in CodegenState
Follow-up to previous commits.
2019-07-22 19:53:49 -07:00
Ray Zeng
85e2d7171e Convert _codegen methods in nodes to use the new CodegenState methods
Converts all nodes to use new CodegenState methods as defined in the
previous commit.

Ran codemods:

    codemod -d libcst 'state.tokens.append\((.*)\)' 'state.add_token(\1)'
    codemod -d libcst 'state.tokens.extend\(state.indent\)' 'state.add_indent_tokens()'
    codemod -d libcst 'state.indent.pop()' 'state.decrease_indent()'
    codemod -d libcst 'state.indent.append\((.*)\)' 'state.increase_indent(\1)'

Where `codemod` refers to https://github.com/facebook/codemod
2019-07-22 19:53:49 -07:00
Ray Zeng
d6bda7e69a Add hooks to CodegenState for line/column tracking
Adds methods to CodegenState for common indentation change operations to
allow for tracking of line and column numbers.
2019-07-22 19:53:49 -07:00
Benjamin Woodruff
76b5ac0de2 Add config files to make tools easier to use
- `pyproject.toml` is supported by isort and black, and lets us call
  those tools without supplying a ton of arguments.
- `.editorconfig` is supported by a wide range of editors, and can
  automatically set per-project configuration.
- `.pyre_configuration` is used by pyre.
- I added test discovery to the `setup.py` file, which required some
  extra `__init__.py` files.
2019-07-22 19:53:49 -07:00
Benjamin Woodruff
1dab051ddb Remove Starred element type
We decided to merge this functionality into StarredElement.
2019-07-22 19:53:49 -07:00
Benjamin Woodruff
ca15eb2685 Add nodedefs (no parsing yet) needed for Tuple
This ended up being pretty complicated, so the parser stuff will come in
another diff.

Hopefully this should set up up nicely for dicts, sets, and lists too.
2019-06-05 15:23:22 -07:00
Jennifer Taylor
21ace9df33 Fix comment ownership around IndentedBlock 2019-06-05 14:31:04 -07:00
Jennifer Taylor
3888c31835 Use .empty property on BaseParenthesizableWhitespace 2019-06-03 15:44:10 -07:00
Jennifer Taylor
e5938df16a Better serialization for MaybeSentinel 2019-06-03 15:43:55 -07:00
Jennifer Taylor
0719faa198 Initial export of LibCST to github. 2019-05-31 14:16:53 -07:00