Skip to content

Commit 2fbd812

Browse files
authored
feat: support insert/require pragma options (#354)
closes #350
1 parent 2e9599b commit 2fbd812

13 files changed

+54
-3
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# prettier-plugin-svelte changelog
22

3+
## 2.10.0 (unreleased)
4+
5+
- (feat) support `requirePragma` and `insertPragma` options
6+
37
## 2.9.0
48

59
- (feat) support style modifiers ([#330](https://github.com/sveltejs/prettier-plugin-svelte/issues/330))

src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { SupportLanguage, Parser, Printer } from 'prettier';
2-
import { print } from './print';
2+
import { hasPragma, print } from './print';
33
import { ASTNode } from './print/nodes';
44
import { embed } from './embed';
55
import { snipScriptAndStyleTagContent } from './lib/snipTagContent';
@@ -23,6 +23,7 @@ export const languages: Partial<SupportLanguage>[] = [
2323

2424
export const parsers: Record<string, Parser> = {
2525
svelte: {
26+
hasPragma,
2627
parse: (text) => {
2728
try {
2829
return <ASTNode>{ ...require(`svelte/compiler`).parse(text), __isRoot: true };

src/print/index.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ declare module 'prettier' {
8383
}
8484
}
8585

86+
export function hasPragma(text: string) {
87+
return /^\s*<!--\s*@(format|prettier)\W/.test(text);
88+
}
89+
8690
let ignoreNext = false;
8791
let ignoreRange = false;
8892
let svelteOptionsDoc: Doc | undefined;
@@ -772,7 +776,13 @@ function printTopLevelParts(
772776
delete topLevelPartsByEnd[node.start];
773777
}
774778
}
775-
return path.call(print, 'html');
779+
780+
const result = path.call(print, 'html');
781+
if (options.insertPragma && !hasPragma(options.originalText)) {
782+
return concat([`<!-- @format -->`, hardline, result]);
783+
} else {
784+
return result;
785+
}
776786
}
777787

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

829-
return groupConcat([join(hardline, docs)]);
839+
if (options.insertPragma && !hasPragma(options.originalText)) {
840+
return concat([`<!-- @format -->`, hardline, groupConcat(docs)]);
841+
} else {
842+
return groupConcat([join(hardline, docs)]);
843+
}
830844
}
831845

832846
function printAttributeNodeValue(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<p>
2+
format
3+
me
4+
</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"insertPragma": true
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<!-- @format -->
2+
<p>format me</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!-- @format -->
2+
<p>
3+
format
4+
me
5+
</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"requirePragma": true
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<!-- @format -->
2+
<p>format me</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<!-- @format -->
2+
<p>already formatted</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"insertPragma": true
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<p>
2+
3+
4+
nope
5+
</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"requirePragma": true
3+
}

0 commit comments

Comments
 (0)