From 0dbeb774ba9ea618ff1e92b63ab31e5caf3003dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Mon, 13 Sep 2021 20:19:10 +0200 Subject: feat(fmt): add support for configuration file (#11944) This commit adds support for configuration file for "deno fmt" subcommand. It is also respected by LSP when formatting files. Example configuration: { "fmt": { "files": { "include": ["src/"], "exclude": ["src/testdata/"] }, "options": { "useTabs": true, "lineWidth": 80, "indentWidth": 4, "singleQuote": true, "textWrap": "preserve" } } } --- cli/tests/testdata/fmt/deno.jsonc | 15 +++++++ cli/tests/testdata/fmt/deno.malformed.jsonc | 12 ++++++ cli/tests/testdata/fmt/deno.malformed2.jsonc | 12 ++++++ cli/tests/testdata/fmt/fmt_with_config.out | 1 + cli/tests/testdata/fmt/fmt_with_config/a.ts | 46 ++++++++++++++++++++++ cli/tests/testdata/fmt/fmt_with_config/b.ts | 15 +++++++ cli/tests/testdata/fmt/fmt_with_config/c.md | 17 ++++++++ .../testdata/fmt/fmt_with_config_and_flags.out | 1 + .../testdata/fmt/fmt_with_malformed_config.out | 4 ++ .../testdata/fmt/fmt_with_malformed_config2.out | 4 ++ cli/tests/testdata/fmt/formatted1.js | 5 --- cli/tests/testdata/fmt/formatted2.ts | 5 --- cli/tests/testdata/fmt/formatted3.md | 17 -------- cli/tests/testdata/fmt/formatted4.jsonc | 4 -- cli/tests/testdata/fmt/regular/formatted1.js | 5 +++ cli/tests/testdata/fmt/regular/formatted2.ts | 5 +++ cli/tests/testdata/fmt/regular/formatted3.md | 17 ++++++++ cli/tests/testdata/fmt/regular/formatted4.jsonc | 4 ++ cli/tests/testdata/lsp/deno.fmt.jsonc | 11 ++++++ 19 files changed, 169 insertions(+), 31 deletions(-) create mode 100644 cli/tests/testdata/fmt/deno.jsonc create mode 100644 cli/tests/testdata/fmt/deno.malformed.jsonc create mode 100644 cli/tests/testdata/fmt/deno.malformed2.jsonc create mode 100644 cli/tests/testdata/fmt/fmt_with_config.out create mode 100644 cli/tests/testdata/fmt/fmt_with_config/a.ts create mode 100644 cli/tests/testdata/fmt/fmt_with_config/b.ts create mode 100644 cli/tests/testdata/fmt/fmt_with_config/c.md create mode 100644 cli/tests/testdata/fmt/fmt_with_config_and_flags.out create mode 100644 cli/tests/testdata/fmt/fmt_with_malformed_config.out create mode 100644 cli/tests/testdata/fmt/fmt_with_malformed_config2.out delete mode 100644 cli/tests/testdata/fmt/formatted1.js delete mode 100644 cli/tests/testdata/fmt/formatted2.ts delete mode 100644 cli/tests/testdata/fmt/formatted3.md delete mode 100644 cli/tests/testdata/fmt/formatted4.jsonc create mode 100644 cli/tests/testdata/fmt/regular/formatted1.js create mode 100644 cli/tests/testdata/fmt/regular/formatted2.ts create mode 100644 cli/tests/testdata/fmt/regular/formatted3.md create mode 100644 cli/tests/testdata/fmt/regular/formatted4.jsonc create mode 100644 cli/tests/testdata/lsp/deno.fmt.jsonc (limited to 'cli/tests/testdata') diff --git a/cli/tests/testdata/fmt/deno.jsonc b/cli/tests/testdata/fmt/deno.jsonc new file mode 100644 index 000000000..0e682cf36 --- /dev/null +++ b/cli/tests/testdata/fmt/deno.jsonc @@ -0,0 +1,15 @@ +{ + "fmt": { + "files": { + "include": ["fmt/fmt_with_config/"], + "exclude": ["fmt/fmt_with_config/b.ts"] + }, + "options": { + "useTabs": true, + "lineWidth": 40, + "indentWidth": 8, + "singleQuote": true, + "proseWrap": "always" + } + } +} diff --git a/cli/tests/testdata/fmt/deno.malformed.jsonc b/cli/tests/testdata/fmt/deno.malformed.jsonc new file mode 100644 index 000000000..667480bdc --- /dev/null +++ b/cli/tests/testdata/fmt/deno.malformed.jsonc @@ -0,0 +1,12 @@ +{ + "fmt": { + "files": { + "include": ["fmt/fmt_with_config/"], + "exclude": ["fmt/fmt_with_config/b.ts"] + }, + "dont_know_this_field": {}, + "options": { + "useTabs": true + } + } +} diff --git a/cli/tests/testdata/fmt/deno.malformed2.jsonc b/cli/tests/testdata/fmt/deno.malformed2.jsonc new file mode 100644 index 000000000..2210dc0c7 --- /dev/null +++ b/cli/tests/testdata/fmt/deno.malformed2.jsonc @@ -0,0 +1,12 @@ +{ + "fmt": { + "files": { + "include": ["fmt/fmt_with_config/"], + "exclude": ["fmt/fmt_with_config/b.ts"], + "dont_know_this_field": {} + }, + "options": { + "useTabs": true + } + } +} diff --git a/cli/tests/testdata/fmt/fmt_with_config.out b/cli/tests/testdata/fmt/fmt_with_config.out new file mode 100644 index 000000000..158c556c2 --- /dev/null +++ b/cli/tests/testdata/fmt/fmt_with_config.out @@ -0,0 +1 @@ +Checked 2 files diff --git a/cli/tests/testdata/fmt/fmt_with_config/a.ts b/cli/tests/testdata/fmt/fmt_with_config/a.ts new file mode 100644 index 000000000..e0f32647b --- /dev/null +++ b/cli/tests/testdata/fmt/fmt_with_config/a.ts @@ -0,0 +1,46 @@ +unitTest( + { perms: { net: true } }, + async function responseClone() { + const response = + await fetch( + 'http://localhost:4545/fixture.json', + ); + const response1 = + response.clone(); + assert( + response !== + response1, + ); + assertEquals( + response.status, + response1 + .status, + ); + assertEquals( + response.statusText, + response1 + .statusText, + ); + const u8a = + new Uint8Array( + await response + .arrayBuffer(), + ); + const u8a1 = + new Uint8Array( + await response1 + .arrayBuffer(), + ); + for ( + let i = 0; + i < + u8a.byteLength; + i++ + ) { + assertEquals( + u8a[i], + u8a1[i], + ); + } + }, +); diff --git a/cli/tests/testdata/fmt/fmt_with_config/b.ts b/cli/tests/testdata/fmt/fmt_with_config/b.ts new file mode 100644 index 000000000..5c37d51d2 --- /dev/null +++ b/cli/tests/testdata/fmt/fmt_with_config/b.ts @@ -0,0 +1,15 @@ +// This file should be excluded from formatting +unitTest( + { perms: { net: true } }, + async function fetchBodyUsedCancelStream() { + const response = await fetch( + "http://localhost:4545/fixture.json", + ); + assert(response.body !== null); + + assertEquals(response.bodyUsed, false); + const promise = response.body.cancel(); + assertEquals(response.bodyUsed, true); + await promise; + }, +); \ No newline at end of file diff --git a/cli/tests/testdata/fmt/fmt_with_config/c.md b/cli/tests/testdata/fmt/fmt_with_config/c.md new file mode 100644 index 000000000..012f7e3d4 --- /dev/null +++ b/cli/tests/testdata/fmt/fmt_with_config/c.md @@ -0,0 +1,17 @@ +## Permissions + +Deno is secure by default. Therefore, +unless you specifically enable it, a +program run with Deno has no file, +network, or environment access. Access +to security sensitive functionality +requires that permisisons have been +granted to an executing script through +command line flags, or a runtime +permission prompt. + +For the following example `mod.ts` has +been granted read-only access to the +file system. It cannot write to the file +system, or perform any other security +sensitive functions. diff --git a/cli/tests/testdata/fmt/fmt_with_config_and_flags.out b/cli/tests/testdata/fmt/fmt_with_config_and_flags.out new file mode 100644 index 000000000..c05ac45a1 --- /dev/null +++ b/cli/tests/testdata/fmt/fmt_with_config_and_flags.out @@ -0,0 +1 @@ +Checked 1 file diff --git a/cli/tests/testdata/fmt/fmt_with_malformed_config.out b/cli/tests/testdata/fmt/fmt_with_malformed_config.out new file mode 100644 index 000000000..1a55613ef --- /dev/null +++ b/cli/tests/testdata/fmt/fmt_with_malformed_config.out @@ -0,0 +1,4 @@ +error: Failed to parse "fmt" configuration + +Caused by: + unknown field `dont_know_this_field`, expected `options` or `files` diff --git a/cli/tests/testdata/fmt/fmt_with_malformed_config2.out b/cli/tests/testdata/fmt/fmt_with_malformed_config2.out new file mode 100644 index 000000000..948b6b5b8 --- /dev/null +++ b/cli/tests/testdata/fmt/fmt_with_malformed_config2.out @@ -0,0 +1,4 @@ +error: Failed to parse "fmt" configuration + +Caused by: + unknown field `dont_know_this_field`, expected `include` or `exclude` diff --git a/cli/tests/testdata/fmt/formatted1.js b/cli/tests/testdata/fmt/formatted1.js deleted file mode 100644 index 587aa5b96..000000000 --- a/cli/tests/testdata/fmt/formatted1.js +++ /dev/null @@ -1,5 +0,0 @@ -function foo() { - return 42; -} - -foo(); diff --git a/cli/tests/testdata/fmt/formatted2.ts b/cli/tests/testdata/fmt/formatted2.ts deleted file mode 100644 index 4a8036806..000000000 --- a/cli/tests/testdata/fmt/formatted2.ts +++ /dev/null @@ -1,5 +0,0 @@ -function bar(): number { - return 42; -} - -bar(); diff --git a/cli/tests/testdata/fmt/formatted3.md b/cli/tests/testdata/fmt/formatted3.md deleted file mode 100644 index e6e616584..000000000 --- a/cli/tests/testdata/fmt/formatted3.md +++ /dev/null @@ -1,17 +0,0 @@ -# Hello - -```js -function foo() { - return 42; -} - -foo(); -``` - -```ts -function bar(): number { - return 42; -} - -bar(); -``` diff --git a/cli/tests/testdata/fmt/formatted4.jsonc b/cli/tests/testdata/fmt/formatted4.jsonc deleted file mode 100644 index f0f72a6ed..000000000 --- a/cli/tests/testdata/fmt/formatted4.jsonc +++ /dev/null @@ -1,4 +0,0 @@ -{ - // Comment - "key": "value" -} diff --git a/cli/tests/testdata/fmt/regular/formatted1.js b/cli/tests/testdata/fmt/regular/formatted1.js new file mode 100644 index 000000000..587aa5b96 --- /dev/null +++ b/cli/tests/testdata/fmt/regular/formatted1.js @@ -0,0 +1,5 @@ +function foo() { + return 42; +} + +foo(); diff --git a/cli/tests/testdata/fmt/regular/formatted2.ts b/cli/tests/testdata/fmt/regular/formatted2.ts new file mode 100644 index 000000000..4a8036806 --- /dev/null +++ b/cli/tests/testdata/fmt/regular/formatted2.ts @@ -0,0 +1,5 @@ +function bar(): number { + return 42; +} + +bar(); diff --git a/cli/tests/testdata/fmt/regular/formatted3.md b/cli/tests/testdata/fmt/regular/formatted3.md new file mode 100644 index 000000000..e6e616584 --- /dev/null +++ b/cli/tests/testdata/fmt/regular/formatted3.md @@ -0,0 +1,17 @@ +# Hello + +```js +function foo() { + return 42; +} + +foo(); +``` + +```ts +function bar(): number { + return 42; +} + +bar(); +``` diff --git a/cli/tests/testdata/fmt/regular/formatted4.jsonc b/cli/tests/testdata/fmt/regular/formatted4.jsonc new file mode 100644 index 000000000..f0f72a6ed --- /dev/null +++ b/cli/tests/testdata/fmt/regular/formatted4.jsonc @@ -0,0 +1,4 @@ +{ + // Comment + "key": "value" +} diff --git a/cli/tests/testdata/lsp/deno.fmt.jsonc b/cli/tests/testdata/lsp/deno.fmt.jsonc new file mode 100644 index 000000000..a0a851731 --- /dev/null +++ b/cli/tests/testdata/lsp/deno.fmt.jsonc @@ -0,0 +1,11 @@ +{ + "fmt": { + "options": { + "useTabs": true, + "lineWidth": 40, + "indentWidth": 8, + "singleQuote": true, + "proseWrap": "always" + } + } +} -- cgit v1.2.3