Skip to content

Commit

Permalink
feat: support insert/require pragma options (#354)
Browse files Browse the repository at this point in the history
closes #350
  • Loading branch information
dummdidumm authored Mar 21, 2023
1 parent 2e9599b commit 2fbd812
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 3 deletions.
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
}

0 comments on commit 2fbd812

Please sign in to comment.