Commit graph

25 commits

Author SHA1 Message Date
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
Benjamin Woodruff
670ece1e91 pyre-strict libcst/__init__.py and libcst/tool.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
Jennifer Taylor
a0bfc90339 Make ParserSyntaxError picklable 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
7c069518a5 Make libcst/tests/test_tool.py pyre-strict 2019-07-22 19:53:49 -07:00
Benjamin Woodruff
31c0ee65a2 Export maybe and removal sentinels from __init__
Otherwise, these aren't exposed publicly anywhere.

This was @DragonMinded's change, I'm just importing it to github.
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
c5f24f35a4 Improve the way dependencies are declared
@jreese had some suggestions in my previous pull request after it got
merged, so this addresses some of those suggestions:

- Uses the PEP 508 `python_version` environment marker instead of
  conditional logic inside `setup.py`. I've updated `requirements.txt`
  to use this too.
- Split dev requirements into a separate `requirements-dev.txt`, and
  updated the README to include instructions for it.

This PR does not use pyup, because it looks like it's free for
non-commercial use only (I don't know that that means in this context),
and because this project isn't public yet.

It also does not include a makefile yet, because Jennifer and I need to
talk through where we'd stick the virtualenv in that case.

I tested these changes on 3.6 and 3.7.
2019-07-22 19:53:49 -07:00
Benjamin Woodruff
a45bf3ac2e Improve the reproducibility of setting up a dev environment
- Reorganized the docs a bit to separate "getting started" from
  "development".

- Added a section about setting up a virtualenv (and tested that
  workflow on my machine).

- Added isort, pyre-check, and black to the `requirements.txt`, and
  updated the `requirements.txt` to include exact versions so that we
  can be sure that our tools will behave the same (e.g. there are
  frequently breaking changes between releases of pyre).

- Added a `dev` section to `setup.py`'s `extras_require`. This allows
  you to `pip install -e '.[dev]'` to get a set of development packages
  installed. I used this to help build the updated `requirements.txt`.
  This appears to be a pattern that other projects use.
2019-07-22 19:53:49 -07:00
Benjamin Woodruff
d4551da477 Improve setup.py compatibility
- Add `libcst/__init__.py` back which I accidentally deleted in another
  commit.
- Add `*.egg-info/` to the gitignore, because `libcst.egg-info` is it's
  created by pip/setuptools when locally installing libcst, and it's
  annoying.
- Changed the version number from `0.1.dev` to `0.1.dev0`, since pip was
  warning that it was normalizing the version number from the former to
  the later.
- Add a `python_requires` field, since we know that libcst only works on
  3.6+.
- Add an `install_requires`. Pip uses this to find dependencies, and
  ignores `requirements.txt` (since `requirements.txt` is really only
  intended to be a freeze file).
- Add the dataclasses backport as a dependency for Python 3.6. I
  validated that installing and using libcst works in both 3.6 and 3.7.

**Test Plan:**

```
$ python3 -m venv libcst-install-test  # my system python is 3.7

$ libcst-install-test/bin/pip install --upgrade pip ipython
Cache entry deserialization failed, entry ignored
Collecting pip
  Using cached
  be401c0032/pip-19.1.1-py2.py3-none-any.whl
Collecting ipython
...  # lots of output

$ ~/libcst-install-test/bin/pip install ~/libcst/
Processing ./libcst
Requirement already satisfied: parso in
./libcst-install-test/lib/python3.7/site-packages (from
libcst==0.1.dev0) (0.4.0)
Collecting typing_extensions (from libcst==0.1.dev0)
  Using cached
  c66e553258/typing_extensions-3.7.2-py3-none-any.whl
Installing collected packages: typing-extensions, libcst
  Running setup.py install for libcst ... done
  Successfully installed libcst-0.1.dev0 typing-extensions-3.7.2

$ ~/libcst-install-test/bin/ipython
Python 3.7.3 (default, Apr  3 2019, 05:39:12)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.5.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from libcst import parser

In [2]: parser.parse_expression("None")
Out[2]:
Name(
    value='None',
    lpar=[],
    rpar=[],
)

In [3]:
```

I then repeated the same with a copy of CPython 3.6 that I built from
source.
2019-06-06 15:37:27 -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
bb72c70a3b Hand-roll print instead of using implicit repr, allowing us to filter
most irrelevant node attributes out.
2019-06-05 14:25:29 -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
8ff42de3c5 Fix issue with @add_slots and py3.7 2019-06-03 15:43:28 -07:00
Jennifer Taylor
0719faa198 Initial export of LibCST to github. 2019-05-31 14:16:53 -07:00
Jennifer Taylor
0e448ac439 documentation placeholder 2019-05-29 11:33:27 -07:00
Jennifer Taylor
a1b5505027 first commit 2019-05-29 11:32:49 -07:00