diff --git a/.eslintrc.yml b/.eslintrc.yml index 037d950ebd26..26bc7af25907 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -73,7 +73,7 @@ overrides: rules: no-console: off - files: - - scripts/build/shims/*.js + - "**/*.mjs" parserOptions: sourceType: module - files: @@ -99,3 +99,13 @@ overrides: - error - patterns: - ".." + - files: src/language-js/needs-parens.js + rules: + prettier-internal-rules/better-parent-property-check-in-needs-parens: error + - files: src/**/*.js + rules: + prettier-internal-rules/prefer-fast-path-each: error + - files: + - src/language-*/**/*.js + rules: + prettier-internal-rules/directly-loc-start-end: error diff --git a/.flowconfig b/.flowconfig deleted file mode 100644 index b2680ab2ad01..000000000000 --- a/.flowconfig +++ /dev/null @@ -1,4 +0,0 @@ -[ignore] -.*/tests/.* -.*/node_modules/.* -.*/dist/.* diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs new file mode 100644 index 000000000000..6dba275511ec --- /dev/null +++ b/.git-blame-ignore-revs @@ -0,0 +1,31 @@ +# git-blame ignored revisions +# To configure, run +# git config blame.ignoreRevsFile .git-blame-ignore-revs +# Requires Git > 2.23 +# See https://git-scm.com/docs/git-blame#Documentation/git-blame.txt---ignore-revs-fileltfilegt + +# Prettier bump after release +# 2.1.2 +c4d3014b95122f4ad19c319a9b3f5f9625d6003f +# 2.1.1 +a8363197118e530d948978da6e5c414a765ba9c0 +# 2.1.0 +cef4bcafc7867050582d3107632bde7e722575d1 +# 2.0.5 +d33f8a3e2c0a59cb9f383ddec5bbf8d296bb1a23 +# 2.0.4 +592149791e4fea656d8c5fa34c25d4d19076a07a +# 2.0.3 +64b3ac9e8e933a09f049b7cace540ee526f4d5a4 +# 2.0.2 +c1dd17cf383b78fd8fd43442bb5db59b51900410 +# 2.0.1 +f56d620be529b60c13032681446c1eb76e0fb088 +# 2.0.0 +9dad95b35f935edce4c3d6cfa45c79a0b9c82b9f + +# Categorize tests (#8239 #8248 #8249 #8251) +b585bd6fa4d750a98e277303c428edfc48fea3f4 +f8c5b1fd1da4d67bc09d12bc3411b70d0fa4f4a1 +b6225788966a4a6b49e652044337436642dcd627 +7ad515111e79a3f304d5480d6586314222052333 diff --git a/.github/ISSUE_TEMPLATE/formatting.md b/.github/ISSUE_TEMPLATE/formatting.md index ba91ba43fa59..af8efd889cf6 100644 --- a/.github/ISSUE_TEMPLATE/formatting.md +++ b/.github/ISSUE_TEMPLATE/formatting.md @@ -26,7 +26,7 @@ Tip! Don't write this stuff manually. --> -**Prettier 2.1.2** +**Prettier 2.2.0** [Playground link](https://prettier.io/playground/#.....) ```sh diff --git a/.github/ISSUE_TEMPLATE/integration.md b/.github/ISSUE_TEMPLATE/integration.md index 73e97d70327e..eea91394de59 100644 --- a/.github/ISSUE_TEMPLATE/integration.md +++ b/.github/ISSUE_TEMPLATE/integration.md @@ -20,7 +20,7 @@ BEFORE SUBMITTING AN ISSUE: **Environments:** -- Prettier Version: 2.1.2 +- Prettier Version: 2.2.0 - Running Prettier via: - Runtime: - Operating System: diff --git a/.github/workflows/dev-package-test.yml b/.github/workflows/dev-package-test.yml index 94ca53ae7459..12576bc08cfd 100644 --- a/.github/workflows/dev-package-test.yml +++ b/.github/workflows/dev-package-test.yml @@ -30,12 +30,10 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Checkout - uses: actions/checkout@v2.3.2 - with: - fetch-depth: 1 + uses: actions/checkout@v2.3.4 - name: Setup Node.js - uses: actions/setup-node@v2.1.1 + uses: actions/setup-node@v2.1.2 with: node-version: ${{ matrix.node }} diff --git a/.github/workflows/dev-test.yml b/.github/workflows/dev-test.yml index 383034555d4a..343651405532 100644 --- a/.github/workflows/dev-test.yml +++ b/.github/workflows/dev-test.yml @@ -26,6 +26,10 @@ jobs: - os: "ubuntu-latest" node: "14" ENABLE_CODE_COVERAGE: true + FULL_TEST: true + CHECK_TEST_PARSERS: true + - os: "ubuntu-latest" + node: "15" exclude: - os: "macos-latest" node: "12" @@ -33,28 +37,22 @@ jobs: node: "12" env: ENABLE_CODE_COVERAGE: ${{ matrix.ENABLE_CODE_COVERAGE }} + FULL_TEST: ${{ matrix.FULL_TEST }} + CHECK_TEST_PARSERS: ${{ matrix.CHECK_TEST_PARSERS }} name: Node.js ${{ matrix.node }} on ${{ matrix.os }} runs-on: ${{ matrix.os }} steps: - name: Checkout - uses: actions/checkout@v2.3.2 - with: - fetch-depth: 1 + uses: actions/checkout@v2.3.4 - name: Setup Node.js - uses: actions/setup-node@v2.1.1 + uses: actions/setup-node@v2.1.2 with: node-version: ${{ matrix.node }} - name: Install Dependencies run: yarn install --frozen-lockfile - # TODO: remove this task when/if this fixed - # https://github.com/facebook/jest/issues/9457 - - name: Install jest@24 - if: matrix.ENABLE_CODE_COVERAGE - run: yarn upgrade jest@24 - - name: Run Tests (macOS) if: matrix.os == 'macos-latest' run: yarn test --maxWorkers=4 @@ -64,7 +62,7 @@ jobs: run: yarn test --maxWorkers=2 - name: Upload Coverage - uses: codecov/codecov-action@v1.0.13 + uses: codecov/codecov-action@v1.0.15 if: matrix.ENABLE_CODE_COVERAGE with: token: ${{ secrets.CODECOV_TOKEN }} @@ -78,7 +76,7 @@ jobs: PRETTIER_FALLBACK_RESOLVE: true - name: Upload Coverage (PRETTIER_FALLBACK_RESOLVE) - uses: codecov/codecov-action@v1.0.13 + uses: codecov/codecov-action@v1.0.15 if: matrix.ENABLE_CODE_COVERAGE with: token: ${{ secrets.CODECOV_TOKEN }} diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index c718e7ba0c32..a377c3759377 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -13,12 +13,10 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout - uses: actions/checkout@v2.3.2 - with: - fetch-depth: 1 + uses: actions/checkout@v2.3.4 - name: Setup Node.js - uses: actions/setup-node@v2.1.1 + uses: actions/setup-node@v2.1.2 - name: Install Dependencies run: yarn install --frozen-lockfile diff --git a/.github/workflows/prod-test.yml b/.github/workflows/prod-test.yml index f63cefe5b793..13bdb965a08c 100644 --- a/.github/workflows/prod-test.yml +++ b/.github/workflows/prod-test.yml @@ -13,12 +13,10 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout - uses: actions/checkout@v2.3.2 - with: - fetch-depth: 1 + uses: actions/checkout@v2.3.4 - name: Setup Node.js - uses: actions/setup-node@v2.1.1 + uses: actions/setup-node@v2.1.2 - name: Install Dependencies run: yarn install --frozen-lockfile @@ -59,12 +57,10 @@ jobs: needs: [build] steps: - name: Checkout - uses: actions/checkout@v2.3.2 - with: - fetch-depth: 1 + uses: actions/checkout@v2.3.4 - name: Setup Node.js - uses: actions/setup-node@v2.1.1 + uses: actions/setup-node@v2.1.2 - name: Install Dependencies run: yarn install --frozen-lockfile @@ -79,7 +75,7 @@ jobs: run: yarn lint:dist test: - timeout-minutes: 60 + timeout-minutes: 90 strategy: fail-fast: false matrix: @@ -91,22 +87,28 @@ jobs: - "14" - "12" - "10" + include: + - os: "ubuntu-latest" + node: "14" + FULL_TEST: true + - os: "ubuntu-latest" + node: "15" exclude: - os: "macos-latest" node: "12" - os: "windows-latest" node: "12" + env: + FULL_TEST: ${{ matrix.FULL_TEST }} name: Node.js ${{ matrix.node }} on ${{ matrix.os }} runs-on: ${{ matrix.os }} needs: [build] steps: - name: Checkout - uses: actions/checkout@v2.3.2 - with: - fetch-depth: 1 + uses: actions/checkout@v2.3.4 - name: Setup Node.js - uses: actions/setup-node@v2.1.1 + uses: actions/setup-node@v2.1.2 with: node-version: ${{ matrix.node }} diff --git a/.gitignore b/.gitignore index 73f676245040..1a0eb682a10a 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,9 @@ /coverage .idea package-lock.json +.yarn/* +!.yarn/releases +!.yarn/plugins +!.yarn/sdks +!.yarn/versions +.pnp.* diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index ac74494a227e..510d631cbd69 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -1,4 +1,7 @@ +# Remove this file after year 2020 + - id: prettier name: prettier - entry: prettier --write --list-different --ignore-unknown - language: node + entry: Prettier support for pre-commit has been moved to https://github.com/pre-commit/mirrors-prettier, please use the new repository. + language: fail + pass_filenames: false diff --git a/CHANGELOG.md b/CHANGELOG.md index 6270e1b7c7d4..b1d3f6130f75 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# 2.2.0 + +[diff](https://github.com/prettier/prettier/compare/2.1.2...2.2.0) + +🔗 [Release Notes](https://prettier.io/blog/2020/11/20/2.2.0.html) + # 2.1.2 [diff](https://github.com/prettier/prettier/compare/2.1.1...2.1.2) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 34a84e200a39..f5c227dd7d0d 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -12,9 +12,12 @@ Here's what you need to know about the tests: - The tests use [Jest snapshots](https://facebook.github.io/jest/docs/en/snapshot-testing.html). - You can make changes and run `jest -u` (or `yarn test -u`) to update the snapshots. Then run `git diff` to take a look at what changed. Always update the snapshots when opening a PR. -- You can run `AST_COMPARE=1 DEEP_COMPARE=1 jest` for a more robust test run. - - `AST_COMPARE` That formats each file, re-parses it, and compares the new AST with the original one and makes sure they are semantically equivalent. - - `DEEP_COMPARE` That formats each file, then formats the output again, and checks that the second output is the same as the first. +- You can run `FULL_TEST=1 jest` for a more robust test run. + - "compare AST" That re-parses the output, and compares the new AST with the original one and makes sure they are semantically equivalent. + - "second format" That formats the output again, and checks that the second output is the same as the first. + - "EOL "\r\n"" That replace end of line with `\r\n`, and checks that the output is the same as the first. + - "EOL "\r"" That replace end of line with `\r`, and checks that the output is the same as the first. + - "BOM" That add BOM(`U+FEFF`) to text, and checks that the output is the same as the first, and keeps the BOM. - Each test folder has a `jsfmt.spec.js` that runs the tests. For JavaScript files, generally you can just put `run_spec(__dirname, ["babel", "flow", "typescript"]);` there. This will verify that the output using each parser is the same. You can also pass options as the third argument, like this: `run_spec(__dirname, ["babel"], { trailingComma: "es5" });` - `tests/flow-repo/` contains the Flow test suite, and is not supposed to be edited by hand. To update it, clone the Flow repo next to the Prettier repo and run: `node scripts/sync-flow-tests.js ../flow/tests/`. - If you would like to debug prettier locally, you can either debug it in Node or the browser. The easiest way to debug it in Node, is to create a local test file with some example code you want formatted and either run it in an editor like VS Code or run it directly via `./bin/prettier.js `. The easiest way to debug it in the browser is to build Prettier's website locally (see `website/README.md`). diff --git a/changelog_unreleased/blog-post-intro.md b/changelog_unreleased/BLOG_POST_INTRO_TEMPLATE.md similarity index 100% rename from changelog_unreleased/blog-post-intro.md rename to changelog_unreleased/BLOG_POST_INTRO_TEMPLATE.md diff --git a/changelog_unreleased/TEMPLATE.md b/changelog_unreleased/TEMPLATE.md index 268377304b92..4700d04e13bb 100644 --- a/changelog_unreleased/TEMPLATE.md +++ b/changelog_unreleased/TEMPLATE.md @@ -22,7 +22,7 @@ --> -#### Title ([#XXXX](https://github.com/prettier/prettier/pull/XXXX) by [@user](https://github.com/user)) +#### Title (#XXXX by @user) diff --git a/changelog_unreleased/api/pr-8983.md b/changelog_unreleased/api/pr-8983.md new file mode 100644 index 000000000000..4282ea7a04d2 --- /dev/null +++ b/changelog_unreleased/api/pr-8983.md @@ -0,0 +1,13 @@ +#### [HIGHLIGHT]ESM standalone bundles ([#8983](https://github.com/prettier/prettier/pull/8983) by [@Monchi](https://github.com/Monchi), [@fisker](https://github.com/fisker)) + +Prettier now also comes as ES modules, which can be directly used in modern browsers: + +```js +import prettier from "https://unpkg.com/prettier/esm/standalone.mjs"; +import parserGraphql from "https://unpkg.com/prettier/esm/parser-graphql.mjs"; + +prettier.format("query { }", { + parser: "graphql", + plugins: [parserGraphql], +}); +``` diff --git a/changelog_unreleased/api/pr-9371.md b/changelog_unreleased/api/pr-9371.md new file mode 100644 index 000000000000..22af5459ed4a --- /dev/null +++ b/changelog_unreleased/api/pr-9371.md @@ -0,0 +1,19 @@ +#### Stop inferring the parser to be `json` for `.jsonl` files (#9371 by @fisker) + + +```console +// Prettier stable +$ prettier --check . +Checking formatting... +[error] bad.jsonl: SyntaxError: Unexpected token (2:1) +[error] 1 | '{"type": "t/f", "head": "England", "relation": "invaded", "tail": "United States"}' +[error] > 2 | '{"type": "t/f", "head": "England", "relation": "attacked", "tail": "Baltimore"}' +[error] | ^ +[error] 3 | +All matched files use Prettier code style! + +// Prettier master +$ prettier --check . +Checking formatting... +All matched files use Prettier code style! +``` diff --git a/changelog_unreleased/cli/pr-9298.md b/changelog_unreleased/cli/pr-9298.md new file mode 100644 index 000000000000..d402fa7d563b --- /dev/null +++ b/changelog_unreleased/cli/pr-9298.md @@ -0,0 +1,15 @@ +#### Fix: error on directories and files with numeric names (#9298 by @fisker) + + +```console +$ cat 1/index.js +hello('world') + +// Prettier stable +$ prettier 1 +[error] The "path" argument must be of type string. Received type number (1) + +// Prettier master +$ prettier 1 +hello("world"); +``` diff --git a/changelog_unreleased/css/pr-9209.md b/changelog_unreleased/css/pr-9209.md new file mode 100644 index 000000000000..2a3f7f2442fa --- /dev/null +++ b/changelog_unreleased/css/pr-9209.md @@ -0,0 +1,42 @@ +#### Improve formatting for custom properties (#9209 by @fisker) + +Thanks to [`PostCSS 8.0`](https://github.com/postcss/postcss/releases/tag/8.0.0), we can handle these edge cases on custom properties. + + +```css +/* Input */ +:root { + --empty: ; + --JSON: [1, "2", {"three": {"a":1}}, [4]]; + --javascript: function(rule) { console.log(rule) }; +} + +@supports (--element(".minwidth", { "minWidth": 300 })) { + [--self] { + background: greenyellow; + } +} + +/* Prettier stable */ +SyntaxError: (postcss) CssSyntaxError Missed semicolon (3:20) + 1 | :root { + 2 | --empty: ; +> 3 | --JSON: [1, "2", {"three": {"a":1}}, [4]]; + | ^ + 4 | --javascript: function(rule) { console.log(rule) }; + 5 | } + 6 | + +/* Prettier master */ +:root { + --empty: ; + --JSON: [1, "2", {"three": {"a": 1}}, [4]]; + --javascript: function(rule) {console.log(rule)}; +} + +@supports (--element(".minwidth", {"minWidth": 300})) { + [--self] { + background: greenyellow; + } +} +``` diff --git a/changelog_unreleased/flow/pr-9071.md b/changelog_unreleased/flow/pr-9071.md new file mode 100644 index 000000000000..7859461a55ce --- /dev/null +++ b/changelog_unreleased/flow/pr-9071.md @@ -0,0 +1,18 @@ +#### Switch the `babel` parser to `babel-flow` if the `@flow` pragma is found (#9071 by @fisker) + +In practice, this means that as long as your Flow files have the pragma, it's safe to use the `.js` extension for them. Prettier will correctly parse and print them without any additional configuration. Previously, the pragma was recognized by the parser, but there existed minor correctness issues with the printer. E.g. it's not safe to unquote number keys in Flow. + + +```jsx +// Input (with --parser babel) +// @flow +f({ "2": 2 }) + +// Prettier stable +// @flow +f({ 2: 2 }); + +// Prettier master +// @flow +f({ "2": 2 }); +``` diff --git a/changelog_unreleased/flow/pr-9432.md b/changelog_unreleased/flow/pr-9432.md new file mode 100644 index 000000000000..a677ea5044ae --- /dev/null +++ b/changelog_unreleased/flow/pr-9432.md @@ -0,0 +1,22 @@ +#### Enums with unknown members (#9432 by @gkz) + +Previously, was not supported. Now, the following is formatted: + + +```jsx +// Input +enum E { + A, + B, + ... +} + +// Prettier stable: parse error + +// Prettier master +enum E { + A, + B, + ... +} +``` diff --git a/changelog_unreleased/flow/pr-9457.md b/changelog_unreleased/flow/pr-9457.md new file mode 100644 index 000000000000..7899e4fa5323 --- /dev/null +++ b/changelog_unreleased/flow/pr-9457.md @@ -0,0 +1,21 @@ +#### `this` parameter annotations (#9457 by @dsainati1, #9489 by @fisker) + + +```jsx +// Input +function f(this: string, a: number) { +} + +type T = (this: boolean, a: number) => boolean; + +// Prettier stable +function f(this: string, a: number) {} + +type T = (a: number) => boolean; + +// Prettier master +function f(this: string, a: number) { +} + +type T = (this: boolean, a: number) => boolean; +``` diff --git a/changelog_unreleased/flow/pr-9523.md b/changelog_unreleased/flow/pr-9523.md new file mode 100644 index 000000000000..da75c9926d1f --- /dev/null +++ b/changelog_unreleased/flow/pr-9523.md @@ -0,0 +1,18 @@ +#### Support `BigIntLiteralTypeAnnotation` and `BigIntTypeAnnotation` (#9523 by @fisker) + +Add support for `BigIntLiteralTypeAnnotation` and `BigIntTypeAnnotation` in Flow. + + +```jsx +// Input +const foo: bigint = 1n; +const bar: baz<1n> = 1n; + +// Prettier stable +Error: unknown type: "BigIntTypeAnnotation" + at ... + +// Prettier master +const foo: bigint = 1n; +const bar: baz<1n> = 1n; +``` diff --git a/changelog_unreleased/flow/pr-9543.md b/changelog_unreleased/flow/pr-9543.md new file mode 100644 index 000000000000..bc6582b498fa --- /dev/null +++ b/changelog_unreleased/flow/pr-9543.md @@ -0,0 +1,25 @@ +#### Treat more simple types as non-breaking in generic type annotations (#9543 by @fisker) + + +```jsx +// Input +const foo1: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo = a +const foo2: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo<"STRING"> = a; +const foo3: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo<0> = a; + +// Prettier master +const foo1: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo< + symbol +> = a; +const foo2: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo< + "STRING" +> = a; +const foo3: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo< + 0 +> = a; + +// Prettier stable (typescript and flow parser) +const foo1: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo = a +const foo2: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo<"STRING"> = a; +const foo3: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo<0> = a; +``` diff --git a/changelog_unreleased/flow/pr-9553.md b/changelog_unreleased/flow/pr-9553.md new file mode 100644 index 000000000000..a041ea4fb7f9 --- /dev/null +++ b/changelog_unreleased/flow/pr-9553.md @@ -0,0 +1,22 @@ +#### Fix missing parentheses around `prettier-ignore`d type assertions (#9553 by @fisker) + + +```jsx +// Input +transform( + // prettier-ignore + (pointTransformer: (Point => Point)) +); + +// Prettier stable +transform( + // prettier-ignore + pointTransformer: (Point => Point) +); + +// Prettier master +transform( + // prettier-ignore + (pointTransformer: (Point => Point)) +); +``` diff --git a/changelog_unreleased/flow/pr-9563.md b/changelog_unreleased/flow/pr-9563.md new file mode 100644 index 000000000000..8260bb391b9f --- /dev/null +++ b/changelog_unreleased/flow/pr-9563.md @@ -0,0 +1,22 @@ +#### Improve comment types detection (#9563 by @fisker) + + +```jsx +// Input +foo/*::*/(baz); +class Foo { + bar( data: Array) {} +} + +// Prettier master +foo/*:: */(baz); +class Foo { + bar(data: Array/*:: */) {} +} + +// Prettier stable +foo/*:: */(baz); +class Foo { + bar(data: Array) {} +} +``` diff --git a/changelog_unreleased/handlebars/pr-9403.md b/changelog_unreleased/handlebars/pr-9403.md new file mode 100644 index 000000000000..b8afd996bf8f --- /dev/null +++ b/changelog_unreleased/handlebars/pr-9403.md @@ -0,0 +1,29 @@ +#### Fix unstable newlines after `