Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support insert/require pragma options #354

Merged
merged 1 commit into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -23,6 +23,7 @@ export const languages: Partial<SupportLanguage>[] = [

export const parsers: Record<string, Parser> = {
svelte: {
hasPragma,
parse: (text) => {
try {
return <ASTNode>{ ...require(`svelte/compiler`).parse(text), __isRoot: true };
Expand Down
18 changes: 16 additions & 2 deletions src/print/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ declare module 'prettier' {
}
}

export function hasPragma(text: string) {
return /^\s*<!--\s*@(format|prettier)\W/.test(text);
}

let ignoreNext = false;
let ignoreRange = false;
let svelteOptionsDoc: Doc | undefined;
Expand Down Expand Up @@ -772,7 +776,13 @@ function printTopLevelParts(
delete topLevelPartsByEnd[node.start];
}
}
return path.call(print, 'html');

const result = path.call(print, 'html');
if (options.insertPragma && !hasPragma(options.originalText)) {
return concat([`<!-- @format -->`, hardline, result]);
} else {
return result;
}
}

const parts: Record<SortOrderPart, Doc[]> = {
Expand Down Expand Up @@ -826,7 +836,11 @@ function printTopLevelParts(
trimRight([lastDoc], isLine);
}

return groupConcat([join(hardline, docs)]);
if (options.insertPragma && !hasPragma(options.originalText)) {
return concat([`<!-- @format -->`, hardline, groupConcat(docs)]);
} else {
return groupConcat([join(hardline, docs)]);
}
}

function printAttributeNodeValue(
Expand Down
4 changes: 4 additions & 0 deletions test/formatting/samples/insert-pragma/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<p>
format
me
</p>
3 changes: 3 additions & 0 deletions test/formatting/samples/insert-pragma/options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"insertPragma": true
}
2 changes: 2 additions & 0 deletions test/formatting/samples/insert-pragma/output.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!-- @format -->
<p>format me</p>
5 changes: 5 additions & 0 deletions test/formatting/samples/require-pragma-present/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!-- @format -->
<p>
format
me
</p>
3 changes: 3 additions & 0 deletions test/formatting/samples/require-pragma-present/options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"requirePragma": true
}
2 changes: 2 additions & 0 deletions test/formatting/samples/require-pragma-present/output.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!-- @format -->
<p>format me</p>
2 changes: 2 additions & 0 deletions test/printer/samples/insert-pragma-present.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!-- @format -->
<p>already formatted</p>
3 changes: 3 additions & 0 deletions test/printer/samples/insert-pragma-present.options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"insertPragma": true
}
5 changes: 5 additions & 0 deletions test/printer/samples/require-pragma-missing.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<p>


nope
</p>
3 changes: 3 additions & 0 deletions test/printer/samples/require-pragma-missing.options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"requirePragma": true
}