Skip to content

Commit 9fb260a

Browse files
committed
fix: handle scss/less
Prettier doesn't handle resolving the parser as automatically anymore in v3, so we need to add more logic ourselves fixes #390
1 parent f86171d commit 9fb260a

File tree

5 files changed

+26
-11
lines changed

5 files changed

+26
-11
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# prettier-plugin-svelte changelog
22

3-
## 3.0.0 (Unreleased)
3+
## 3.0.1
4+
5+
- (fix) support less/scss in style tags
6+
7+
## 3.0.0
48

59
- (breaking) requires `prettier` version 3. This may require adjustments to your configuration file, see [the migration guide for more info](https://github.com/sveltejs/prettier-plugin-svelte#how-to-migrate-from-version-2-to-3)
610
- (breaking) requires node version 14 or higher

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "prettier-plugin-svelte",
3-
"version": "2.10.1",
3+
"version": "3.0.1",
44
"description": "Svelte plugin for prettier",
55
"main": "plugin.js",
66
"files": [

src/embed.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import {
1111
getLeadingComment,
1212
isIgnoreDirective,
1313
isInsideQuotedAttribute,
14+
isLess,
1415
isNodeSupportedLanguage,
1516
isPugTemplate,
17+
isScss,
1618
isTypeScript,
1719
printRaw,
1820
} from './print/node-helpers';
@@ -151,7 +153,7 @@ export function embed(path: FastPath, _options: Options) {
151153

152154
const embedType = (
153155
tag: 'script' | 'style' | 'template',
154-
parser: 'typescript' | 'babel-ts' | 'css' | 'pug',
156+
parser: 'typescript' | 'babel-ts' | 'css' | 'scss' | 'less' | 'pug',
155157
isTopLevel: boolean,
156158
) => {
157159
return async (
@@ -180,7 +182,8 @@ export function embed(path: FastPath, _options: Options) {
180182
isTypeScript(node) ? 'typescript' : 'babel-ts',
181183
isTopLevel,
182184
);
183-
const embedStyle = (isTopLevel: boolean) => embedType('style', 'css', isTopLevel);
185+
const embedStyle = (isTopLevel: boolean) =>
186+
embedType('style', isLess(node) ? 'less' : isScss(node) ? 'scss' : 'css', isTopLevel);
184187
const embedPug = () => embedType('template', 'pug', false);
185188

186189
switch (node.type) {
@@ -233,7 +236,7 @@ function getSnippedContent(node: Node) {
233236

234237
async function formatBodyContent(
235238
content: string,
236-
parser: 'typescript' | 'babel-ts' | 'css' | 'pug',
239+
parser: 'typescript' | 'babel-ts' | 'css' | 'scss' | 'less' | 'pug',
237240
textToDoc: (text: string, options: object) => Promise<Doc>,
238241
options: ParserOptions & { pugTabWidth?: number },
239242
) {

src/print/node-helpers.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,7 @@ function isTextNode(node: Node): node is TextNode {
216216
function getAttributeValue(attributeName: string, node: Node) {
217217
const attributes = ((node as ElementNode).attributes ?? []) as AttributeNode[];
218218

219-
const langAttribute = attributes.find(
220-
(attribute) => attribute.name === attributeName,
221-
);
219+
const langAttribute = attributes.find((attribute) => attribute.name === attributeName);
222220

223221
return langAttribute && langAttribute.value;
224222
}
@@ -269,6 +267,16 @@ export function isTypeScript(node: Node) {
269267
return ['typescript', 'ts'].includes(lang);
270268
}
271269

270+
export function isLess(node: Node) {
271+
const lang = getLangAttribute(node) || '';
272+
return ['less'].includes(lang);
273+
}
274+
275+
export function isScss(node: Node) {
276+
const lang = getLangAttribute(node) || '';
277+
return ['sass', 'scss'].includes(lang);
278+
}
279+
272280
export function isPugTemplate(node: Node): boolean {
273281
return node.type === 'Element' && node.name === 'template' && getLangAttribute(node) === 'pug';
274282
}
@@ -623,4 +631,4 @@ function removeAndGetLeadingComments(ast: ASTNode, current: Node): CommentInfo[]
623631
emptyLineAfter: getUnencodedText(newlines[i]).split('\n').length > 2,
624632
}))
625633
.reverse();
626-
}
634+
}

0 commit comments

Comments
 (0)