Commit Graph

949 Commits (0adeadb16ea5e70e18902bc700d0cce3260f962d)

Author SHA1 Message Date
Simon Lydell 0adeadb16e Link to node-glob's "glob primer"
As mentioned in #1707.
2017-05-24 18:17:31 +02:00
Michał Pierzchała 09df0fbe15 Extract contributing guide into separate document (#1702) 2017-05-24 08:32:20 -07:00
Michał Pierzchała 3a431d3fe7 Document --with-node-modules flag (#1701) 2017-05-24 03:25:45 -07:00
Christopher Chedeau 1df1389764 Add parenthesis for as inside of class extends (#1699)
Looks like we didn't properly support it for all the binary operators :p
2017-05-23 18:39:01 -07:00
Christopher Chedeau 7cd0b78fe7 Properly add parenthesis for as inside of binary expression (#1698)
The binary logic doesn't work if the node isn't a binary or logical expression
2017-05-23 18:36:14 -07:00
Christopher Chedeau cd60e020c3 Add support for TSQualifiedType in ast-types (#1696)
The comment doesn't print in the right location so there's likely more types missing but at least it's not crashing anymore
2017-05-23 18:18:46 -07:00
Christopher Chedeau a446cf18ce Add parenthesis for ternary inside of as (#1697) 2017-05-23 18:18:34 -07:00
Christopher Chedeau 44d02b8680 Support empty declare module (#1693) 2017-05-23 17:13:47 -07:00
Christopher Chedeau 5671f32c07 Handle comments at the beginning of ClassProperty value (#1692)
We didn't call the helper that handles this use case
2017-05-23 17:13:40 -07:00
Christopher Chedeau 9af859f0b2 Fix master 2017-05-23 17:13:01 -07:00
Christopher Chedeau 8ad31635b6 Make the comment chain comment detection more robust (#1691)
I kept adding places where comments could be but more use cases kept creeping in. So now this is a holistical approach, we check all the comments that can be between two nodes. This way we should be good to go :)
2017-05-23 16:31:10 -07:00
Christopher Chedeau f8ff7e5cd2 Add TSArrayType to ast-types (#1690) 2017-05-23 16:31:01 -07:00
Christopher Chedeau 7ab73e688d Add parenthesis around arrow functions in a cast (#1689) 2017-05-23 16:30:47 -07:00
Christopher Chedeau 474eed0ecb Do not add double space for export function without body with a comment (#1688) 2017-05-23 16:30:35 -07:00
Michał Pierzchała 7a0df958dc Ignore node_modules when running prettier from CLI (#1683)
* Ignore node_modules by default

* Add integration test suite for --with-node-modules CLI arg

* Refactor tests to snapshots

* Disable eslint for purposely faulty files

* Fix node 4

* Fix prettier

* Gitignore only top-level node_modules
2017-05-23 14:19:04 -07:00
Christopher Chedeau cf716e5538 Handle trailing comments in member chains (#1686) 2017-05-23 13:58:52 -07:00
Christopher Chedeau 47d9686402 Do not indent for switch case with `;` (#1687)
The indentation detection didn't discard empty statements
2017-05-23 13:58:43 -07:00
Christopher Chedeau bcc3eb9272 Add TSTupleType to ast-types (#1685)
Otherwise this throws
2017-05-23 13:14:30 -07:00
Christopher Chedeau 1e351436da Add parenthesis for binary expression inside of as (#1682) 2017-05-23 13:14:22 -07:00
Christopher Chedeau f69c5496c6 Break if the first group has an end of line comment (#1681)
This was unstable
2017-05-23 13:14:13 -07:00
Christopher Chedeau c46f230a40 Fix TSAbstractClass base (#1676)
This lets the comment traversal code discover it
2017-05-23 12:34:40 -07:00
Christopher Chedeau c2007ef2a4 Handle TypeScript decorators (#1678)
See this issue for creating the same decorators as babylon ( https://github.com/eslint/typescript-eslint-parser/issues/250 ), in the meantime, this is making a ton of files in vscode crash. So we can just support both :)
2017-05-23 12:33:54 -07:00
Christopher Chedeau 069df25475 Fix last comment of import (#1677)
We used to use isBlock as a heuristic but the real thing we want is to know if the comment is on its own line. Now it works with both :)
2017-05-23 12:33:46 -07:00
Christopher Chedeau 19486bdd17 Add parenthesis for ternaries inside of a cast (#1680) 2017-05-23 12:33:32 -07:00
Christopher Chedeau f50e42d8e9 Do not put softline before TSImportEqualsDeclaration (#1679)
I'm not sure why it was there but it makes the transform not stable
2017-05-23 12:33:26 -07:00
Christopher Chedeau e37ea2c1f6 Add expression node in TSAsExpression (#1675)
This lets the comment traversal code discover it
2017-05-23 12:33:18 -07:00
Christopher Chedeau 2c5dd4cc04 Print typescript class generics (#1673)
This is the only issue with the typescript codebase itself :)
2017-05-23 12:33:10 -07:00
Christopher Chedeau fc88aa185b Add parenthesis around binary expression inside of update expression (#1672)
This is not valid code but TypeScript parses it. It's easy to fix so might as well so we have one less failing test.
2017-05-23 12:33:03 -07:00
Karl O'Keeffe 2512497288 Hug JSX text after multi line tags (#1671)
* Hug JSX text after multi line tags

* Hug text to beginning of tags
2017-05-23 11:56:21 -07:00
Karl O'Keeffe 95e981dd21 Preserve unusual unicode whitespace (#1658)
* Keep unusual unicode spaces

This no longer converts unusual unicode whitespace characters (such as a non-breaking space) into normal spaces.

* Tweak comment based on PR feedback
2017-05-23 09:09:29 -07:00
Joseph Frazier 330601c77d Find nearest node when formatting range (#1659)
* Move range extension code into helper functions

* Add findNodeByOffset() helper

This was adapted from cbc1929c64

* Test extending formatted range to entire node

* Fix extending formatted range to entire node

* Fix style errors

* Add run_file test function

This makes it possible to use different options on a per-file basis,
which is useful for things like range formatting tests.

* Test extending the format range to nearest parseable node

This means you can select the range of a `catch` clause, attempt to
format it, and have the `try` formatted as well, rather than throwing an
error.

* Fix extending the format range to nearest parseable node

This means you can select the range of a `catch` clause, attempt to
format it, and have the `try` formatted as well, rather than throwing an
error.

* Test that external indentation is left alone when formatting a range

* Preserve external indentation when formatting a range

* Dedupe range formatting traversal callbacks

* Simplify range formatting traversal using ast-types

See https://github.com/prettier/prettier/pull/1659#issuecomment-302974798

* Make range formatting traversal more efficient

There's less unnecessary parsing now.

* Fix style errors

* Add test where range expanding fails

* Fix test where range expanding fails

This makes sure that the range contains the entirety of the nodes
containing each of the range's endpoints.

* Add test for expanding range to beginning of line

* Pass test for expanding range to beginning of line

This makes it so that indentation before the range is added to the
formatted range.

* Don't parse/stringify AST to detect pre-range indentation

See https://github.com/prettier/prettier/pull/1659#discussion_r117790671

* When formatting a range, find closest statement rather than parsing

The `isStatement` implementation came from `docs/prettier.min.js`.

See https://github.com/prettier/prettier/pull/1659#issuecomment-303154770

* Add test for range-formatting a FunctionDeclaration's argument object

* Include FunctionDeclaration when searching for nearest node to range-format

From the spec, a Program is a series of SourceElements, each of which is
either a Statement or a FunctionDeclaration. See
https://www.ecma-international.org/ecma-262/5.1/#sec-A.5

* Remove unnecessary try-catch

See https://github.com/prettier/prettier/pull/1659#discussion_r117810096

* Add tests with multiple statements

See https://github.com/prettier/prettier/pull/1659#discussion_r117810753

* Remove unnecessary arguments from findNodeByOffset()

* Contract format range to ensure it starts/ends on nodes

* Specify test ranges in the fixtures

See https://github.com/prettier/prettier/pull/1659#discussion_r117811186

* Remove unnecessary comments from range fixtures

* Remove run_file test function

It's no longer used. This essentially reverts
8241216e68f2e0da997a4f558b03658d642c89a2

* Update range formatting docs

Clarify that the range expands to the nearest statement, and not to the
end of the line.

* Don't overwrite test options when detecting range

Now that multiple files share the same object again, we shouldn't be
re-assigning to it.

* Reuse already-read fixtures for AST_COMPARE=1 tests

* Remove `run_file` global from test eslintrc

* Undo package.json churn

`yarn` reformatted it before, but the whitespace visually sets off the
comment, so let's put it back how it was before.

See https://github.com/prettier/prettier/pull/1659#discussion_r117864655

* Remove misleading comments from isSourceElement

See https://github.com/prettier/prettier/pull/1659#discussion_r117865196

* Loop backwards through string instead of reversing it

See https://github.com/prettier/prettier/pull/1659#discussion_r117865759

* Don't recompute indent string when formatting range

See https://github.com/prettier/prettier/pull/1659#discussion_r117867268

* Rename findNodeByOffset to findNodeAtOffset

"Find x by y" is the common usage for finding an `x` by a key `y`.
However, since "by" has positional meaning, let's use "at" instead.

See https://github.com/prettier/prettier/pull/1659#discussion_r117865121

* Always trimRight() in formatRange and explain why

See https://github.com/prettier/prettier/pull/1659#discussion_r117864635

* Test formatting a range that crosses AST levels

See https://github.com/prettier/prettier/pull/1659#issuecomment-303243688

* Fix formatting a range that crosses AST levels

See https://github.com/prettier/prettier/pull/1659#issuecomment-303243688

* Remove unnecessary try-catch

See e52db5e9f9 (r117878763)

* Add test demonstrating range formatting indent detection

* Detect alignment from text on line before range, but don't reformat it

This avoids reformatting non-indentation that happens to precede the
range on the same line, while still correctly indenting the range based
on it.

See https://github.com/prettier/prettier/pull/1659#discussion_r117881430
2017-05-23 07:43:58 -07:00
Brian Ng 316c76172b Add test for flow class field override (#1669) 2017-05-22 15:54:20 -07:00
Karl O'Keeffe 4083d92ad1 Fix fill wrapping when text starts with whitespace (#1666) 2017-05-22 15:32:57 -07:00
Brian Ng d608b7ba28 Bump Babylon and enable additional tests (#1668) 2017-05-22 15:31:53 -07:00
Simon Lydell c2a3d23133 Improve the `--list-different` example
- Use double quotes for Windows support.
- Use the long flag for readability.
- Show that you should pass in your formatting options as well.
2017-05-22 20:30:10 +02:00
Mateusz Zatorski 0a5d035a85 Add example of `--list-different` flag usage to readme (#1665)
* Add example of `--list-different` flag usage to readme

* review fixes

* typo

* add code blocks
2017-05-22 11:27:24 -07:00
Joseph Frazier 374a68201c Don't removeLines() from TemplateLiteral quasis (#1664)
* Add tests for newlines in template literals

These are related to the following issues:

* https://github.com/prettier/prettier/issues/1662
* https://github.com/prettier/prettier/issues/821
* https://github.com/prettier/prettier/issues/1183
* https://github.com/prettier/prettier/issues/1626

* Don't removeLines() from TemplateLiteral quasis

This fixes https://github.com/prettier/prettier/issues/1662
and fixes https://github.com/prettier/prettier/issues/1626

It also affects https://github.com/prettier/prettier/issues/821 and
https://github.com/prettier/prettier/issues/1183
2017-05-22 09:00:01 -07:00
Christopher Chedeau aeed7840e2 Do not inline arrow when argument has a leading comment (#1660)
Fixes #1614
2017-05-22 09:40:19 -04:00
Joseph Frazier fa8408b53f Upgrade typescript parser, fix and run AST tests on Travis (#1657)
* Run `yarn` to update yarn.lock

* Upgrade typescript-eslint-parser, use JSXText instead of Literal for strings inside JSXElement

https://github.com/eslint/typescript-eslint-parser/pull/272 was merged,
so this fixes https://github.com/prettier/prettier/issues/1558

See here for more info about the new TSQualifiedName node type:
3491b4b73a

* Run AST comparison tests on Travis
2017-05-21 14:16:24 -07:00
Christopher Chedeau 50e95d6c7c Update README.md 2017-05-21 13:37:58 -07:00
Christopher Chedeau 5e8125b96e Move options around 2017-05-21 13:21:29 -07:00
Christopher Chedeau 801519ec79 Warn against > 80 columns 2017-05-21 13:17:02 -07:00
Jimmy Yuen Ho Wong 6f0707a995 Make emacs package summary clearer (#1655)
Currently the package summary makes no sense when viewed from Emacs's package manager, this fixes it.
2017-05-21 12:12:23 -07:00
Simon Lydell 378682350f Improve ESLint setup and fix errors (#1656)
* Improve ESLint setup

- Uses `eslint:recommended` + a handful more rules.
- Uses .eslintignore so that editors can understand which files to lint.
- Uses .eslintrc<strong>.js</strong> so more editors get syntax highlighting.

* Fix ESLint errors

About half of them were fixed by `eslint . --fix`.
2017-05-21 12:11:09 -07:00
Joseph Frazier d7aaaee0dd Add `lint` script, run it on Travis, prettify source code (#1634)
* Add `lint` script with eslint-plugin-prettier, use it in `format:all`

See https://github.com/prettier/prettier/pull/1634#issuecomment-302945967

* Run `lint` script on Travis

* Run Prettier on its own source code

This was done with

    npm run format:all
2017-05-21 11:00:16 -07:00
Joseph Frazier 569380155b Add `--range-start` and `--range-end` options to format only parts of the input (#1609)
* Add `--range-start` and `--range-end` options to format only parts of the input

These options default to `0` and `Infinity`, respectively, so that the
entire input is formatted by default. However, if either option is
specified such that a node lies completely outside the resulting range,
the node will be treated as if it has a `// prettier-ignore` comment.

Related to https://github.com/prettier/prettier/pull/1577#issuecomment-300551179
Related to https://github.com/prettier/prettier/issues/1324
Related to https://github.com/prettier/prettier/issues/593

* printer: Extract hasPrettierIgnoreComment() helper

* Move isOutsideRange() to util

* Don't throw errors about comments outside range "not printing"

* Remove unnecessary check from isOutsideRange()

* Make --range-end exclusive

This lets it use the conventional way of specifying ranges in strings.

Note that if the rangeEnd in the tests is changed to 158, it will fail,
but it wouldn't have failed before this change.

* Change range formatting approach

NOTE: This doesn't pass its test yet. Note that since we're reading the
indentation from the first line, it is expected not to change. However,
a semicolon is added, and the lines outside the range are not changed.

The new approach is roughly:

* Require that the range exactly covers an integer number of lines of the input
* Detect the indentation of the line the range starts on
* Format the range's substring using `printAstToDoc`
* Add enough `indent`s to the doc to restore the detected indentation
* Format the doc to a string with `printDocToString`
* Prepend/append the original input before/after the range

See https://github.com/prettier/prettier/pull/1609#issuecomment-301582273

---

Given `tests/range/range.js`, run the following:

    prettier tests/range/range.js --range-start 165 --range-end 246

See the range's text with:

    dd if=tests/range/range.js ibs=1 skip=165 count=81 2>/dev/null

* Don't use default function parameters

Node v4 doesn't support them. See
http://node.green/#ES2015-syntax-default-function-parameters

* Hackily fix indentation of range formatting

See
https://github.com/prettier/prettier/pull/1609#issuecomment-301625368

Also update the snapshot to reflect that the indentation actually should
decrease by one space, since there were 13 spaces in the input and we
round down after dividing by tabWidth.

* Revert "printer: Extract hasPrettierIgnoreComment() helper"

See https://github.com/prettier/prettier/pull/1609#discussion_r116804853

This reverts commit 62bf068ca98f69d4a7fd0ae188b3554d409eee8d.

* Test automatically using the beginning of the rangeStart line and same for the end

See https://github.com/prettier/prettier/pull/1609#issuecomment-301862076

* Fix automatically using the beginning of the rangeStart line and same for the end

See https://github.com/prettier/prettier/pull/1609#issuecomment-301862076

* Propagate breaks after adding an indentation-triggering hardline

See c1a61ebde8 (r116805581)

* Extract getAlignmentSize(), use instead of countIndents()

See c1a61ebde8 (r116804694)

* Extract addAlignmentToDoc(), use instead of addIndentsToDoc()

See c1a61ebde8 (r116804694)

* Document that --range-start and --range-end include the entire line

* Fix rangeStart calculation

Before, it was incorrectly resulting in 1 when the originally provided
value was 0

* Extract formatRange() helper function

* Move getAlignmentSize() from printer to util

This addresses https://github.com/prettier/prettier/pull/1609#discussion_r117636241

* Move addAlignmentToDoc() from printer to doc-builders

This addresses https://github.com/prettier/prettier/pull/1609#discussion_r117636251
2017-05-21 10:14:13 -07:00
Henric Trotzig f3a049627d Add instruction on how to set up key binding in vim (#1654)
I'm a vim user, and I use prettier on a few of my projects, but not all.
The Prettier docs listed ways to make prettier run automatically on save
(and other autocmd events), but I needed a way to invoke it manually.
After spending some time finding the right vim config, I decided it
could be useful to others as well.

Fixes #1615
2017-05-21 10:08:26 -07:00
Simon Lydell bc5186e544 Run Prettier on all source files (#1648)
This commit updates `npm run format:all` to not only format .js files in
src/ and bin/, but also tests_config/ and scripts/, as well as all
jsfmt.spec.js files.

It also includes the result of running `npm run format:all`, except
changes to src/ and bin/.
2017-05-21 08:13:11 -07:00
Joseph Frazier 226adb2e81 Allow `--write` to be used with `--list-different` (#1633)
* Allow `--write` to be used with `--list-different`

This makes it possible to simultaneously check for formatting errors and
fix them, which can be useful for CI services. For example, a CI service
could run:

    prettier --list-different --write $FILES || git diff --exit-code

to show the formatting errors. Before this change, it would be necessary
to do:

    prettier --list-different $FILES || (prettier --write $FILES; git diff --exit-code)

Here are some commands which are useful to verify that the outputs and
exit codes of `prettier --list-different` and `prettier --write` have
not changed:

    ./bin/prettier.js --list-different index.js src/*.js bin/*.js; echo $?
    ./bin/prettier.js --write index.js src/*.js bin/*.js; echo $?

* fixup! Allow `--write` to be used with `--list-different`
2017-05-21 08:12:00 -07:00
Roman Coedo 83fe8d3d18 Make package-lint happy (#1629) 2017-05-21 08:11:11 -07:00