From 6b41b2af0d40d6f708695749a7c2f35c2d68e974 Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Tue, 21 Mar 2023 21:56:19 +0100 Subject: [PATCH] feat: support insert/require pragma options closes #350 --- CHANGELOG.md | 4 ++++ src/index.ts | 3 ++- src/print/index.ts | 18 ++++++++++++++++-- .../samples/insert-pragma/input.html | 4 ++++ .../samples/insert-pragma/options.json | 3 +++ .../samples/insert-pragma/output.html | 2 ++ .../samples/require-pragma-present/input.html | 5 +++++ .../require-pragma-present/options.json | 3 +++ .../samples/require-pragma-present/output.html | 2 ++ .../printer/samples/insert-pragma-present.html | 2 ++ .../samples/insert-pragma-present.options.json | 3 +++ .../samples/require-pragma-missing.html | 5 +++++ .../require-pragma-missing.options.json | 3 +++ 13 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 test/formatting/samples/insert-pragma/input.html create mode 100644 test/formatting/samples/insert-pragma/options.json create mode 100644 test/formatting/samples/insert-pragma/output.html create mode 100644 test/formatting/samples/require-pragma-present/input.html create mode 100644 test/formatting/samples/require-pragma-present/options.json create mode 100644 test/formatting/samples/require-pragma-present/output.html create mode 100644 test/printer/samples/insert-pragma-present.html create mode 100644 test/printer/samples/insert-pragma-present.options.json create mode 100644 test/printer/samples/require-pragma-missing.html create mode 100644 test/printer/samples/require-pragma-missing.options.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 0830db05..837d1aa1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # prettier-plugin-svelte changelog +## 2.10.0 (unreleased) + +- (feat) support `requirePragma` and `insertPragma` options + ## 2.9.0 - (feat) support style modifiers ([#330](https://github.com/sveltejs/prettier-plugin-svelte/issues/330)) diff --git a/src/index.ts b/src/index.ts index 1b84f11c..c7735118 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ import { SupportLanguage, Parser, Printer } from 'prettier'; -import { print } from './print'; +import { hasPragma, print } from './print'; import { ASTNode } from './print/nodes'; import { embed } from './embed'; import { snipScriptAndStyleTagContent } from './lib/snipTagContent'; @@ -23,6 +23,7 @@ export const languages: Partial[] = [ export const parsers: Record = { svelte: { + hasPragma, parse: (text) => { try { return { ...require(`svelte/compiler`).parse(text), __isRoot: true }; diff --git a/src/print/index.ts b/src/print/index.ts index 66514be6..692d19da 100644 --- a/src/print/index.ts +++ b/src/print/index.ts @@ -83,6 +83,10 @@ declare module 'prettier' { } } +export function hasPragma(text: string) { + return /^\s*`, hardline, result]); + } else { + return result; + } } const parts: Record = { @@ -826,7 +836,11 @@ function printTopLevelParts( trimRight([lastDoc], isLine); } - return groupConcat([join(hardline, docs)]); + if (options.insertPragma && !hasPragma(options.originalText)) { + return concat([``, hardline, groupConcat(docs)]); + } else { + return groupConcat([join(hardline, docs)]); + } } function printAttributeNodeValue( diff --git a/test/formatting/samples/insert-pragma/input.html b/test/formatting/samples/insert-pragma/input.html new file mode 100644 index 00000000..77e7173b --- /dev/null +++ b/test/formatting/samples/insert-pragma/input.html @@ -0,0 +1,4 @@ +

+ format + me +

\ No newline at end of file diff --git a/test/formatting/samples/insert-pragma/options.json b/test/formatting/samples/insert-pragma/options.json new file mode 100644 index 00000000..a23c02df --- /dev/null +++ b/test/formatting/samples/insert-pragma/options.json @@ -0,0 +1,3 @@ +{ + "insertPragma": true +} diff --git a/test/formatting/samples/insert-pragma/output.html b/test/formatting/samples/insert-pragma/output.html new file mode 100644 index 00000000..22c5387a --- /dev/null +++ b/test/formatting/samples/insert-pragma/output.html @@ -0,0 +1,2 @@ + +

format me

diff --git a/test/formatting/samples/require-pragma-present/input.html b/test/formatting/samples/require-pragma-present/input.html new file mode 100644 index 00000000..c418ccf1 --- /dev/null +++ b/test/formatting/samples/require-pragma-present/input.html @@ -0,0 +1,5 @@ + +

+ format + me +

\ No newline at end of file diff --git a/test/formatting/samples/require-pragma-present/options.json b/test/formatting/samples/require-pragma-present/options.json new file mode 100644 index 00000000..b6729a6f --- /dev/null +++ b/test/formatting/samples/require-pragma-present/options.json @@ -0,0 +1,3 @@ +{ + "requirePragma": true +} diff --git a/test/formatting/samples/require-pragma-present/output.html b/test/formatting/samples/require-pragma-present/output.html new file mode 100644 index 00000000..22c5387a --- /dev/null +++ b/test/formatting/samples/require-pragma-present/output.html @@ -0,0 +1,2 @@ + +

format me

diff --git a/test/printer/samples/insert-pragma-present.html b/test/printer/samples/insert-pragma-present.html new file mode 100644 index 00000000..fcbef3c5 --- /dev/null +++ b/test/printer/samples/insert-pragma-present.html @@ -0,0 +1,2 @@ + +

already formatted

diff --git a/test/printer/samples/insert-pragma-present.options.json b/test/printer/samples/insert-pragma-present.options.json new file mode 100644 index 00000000..a23c02df --- /dev/null +++ b/test/printer/samples/insert-pragma-present.options.json @@ -0,0 +1,3 @@ +{ + "insertPragma": true +} diff --git a/test/printer/samples/require-pragma-missing.html b/test/printer/samples/require-pragma-missing.html new file mode 100644 index 00000000..bccad2d3 --- /dev/null +++ b/test/printer/samples/require-pragma-missing.html @@ -0,0 +1,5 @@ +

+ + + nope +

\ No newline at end of file diff --git a/test/printer/samples/require-pragma-missing.options.json b/test/printer/samples/require-pragma-missing.options.json new file mode 100644 index 00000000..b6729a6f --- /dev/null +++ b/test/printer/samples/require-pragma-missing.options.json @@ -0,0 +1,3 @@ +{ + "requirePragma": true +}