Skip to content

Commit

Permalink
fix: handle scss/less
Browse files Browse the repository at this point in the history
Prettier doesn't handle resolving the parser as automatically anymore in v3, so we need to add more logic ourselves
fixes sveltejs#390
  • Loading branch information
dummdidumm committed Jul 26, 2023
1 parent f86171d commit 9fb260a
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# prettier-plugin-svelte changelog

## 3.0.0 (Unreleased)
## 3.0.1

- (fix) support less/scss in style tags

## 3.0.0

- (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)
- (breaking) requires node version 14 or higher
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prettier-plugin-svelte",
"version": "2.10.1",
"version": "3.0.1",
"description": "Svelte plugin for prettier",
"main": "plugin.js",
"files": [
Expand Down
9 changes: 6 additions & 3 deletions src/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import {
getLeadingComment,
isIgnoreDirective,
isInsideQuotedAttribute,
isLess,
isNodeSupportedLanguage,
isPugTemplate,
isScss,
isTypeScript,
printRaw,
} from './print/node-helpers';
Expand Down Expand Up @@ -151,7 +153,7 @@ export function embed(path: FastPath, _options: Options) {

const embedType = (
tag: 'script' | 'style' | 'template',
parser: 'typescript' | 'babel-ts' | 'css' | 'pug',
parser: 'typescript' | 'babel-ts' | 'css' | 'scss' | 'less' | 'pug',
isTopLevel: boolean,
) => {
return async (
Expand Down Expand Up @@ -180,7 +182,8 @@ export function embed(path: FastPath, _options: Options) {
isTypeScript(node) ? 'typescript' : 'babel-ts',
isTopLevel,
);
const embedStyle = (isTopLevel: boolean) => embedType('style', 'css', isTopLevel);
const embedStyle = (isTopLevel: boolean) =>
embedType('style', isLess(node) ? 'less' : isScss(node) ? 'scss' : 'css', isTopLevel);
const embedPug = () => embedType('template', 'pug', false);

switch (node.type) {
Expand Down Expand Up @@ -233,7 +236,7 @@ function getSnippedContent(node: Node) {

async function formatBodyContent(
content: string,
parser: 'typescript' | 'babel-ts' | 'css' | 'pug',
parser: 'typescript' | 'babel-ts' | 'css' | 'scss' | 'less' | 'pug',
textToDoc: (text: string, options: object) => Promise<Doc>,
options: ParserOptions & { pugTabWidth?: number },
) {
Expand Down
16 changes: 12 additions & 4 deletions src/print/node-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,7 @@ function isTextNode(node: Node): node is TextNode {
function getAttributeValue(attributeName: string, node: Node) {
const attributes = ((node as ElementNode).attributes ?? []) as AttributeNode[];

const langAttribute = attributes.find(
(attribute) => attribute.name === attributeName,
);
const langAttribute = attributes.find((attribute) => attribute.name === attributeName);

return langAttribute && langAttribute.value;
}
Expand Down Expand Up @@ -269,6 +267,16 @@ export function isTypeScript(node: Node) {
return ['typescript', 'ts'].includes(lang);
}

export function isLess(node: Node) {
const lang = getLangAttribute(node) || '';
return ['less'].includes(lang);
}

export function isScss(node: Node) {
const lang = getLangAttribute(node) || '';
return ['sass', 'scss'].includes(lang);
}

export function isPugTemplate(node: Node): boolean {
return node.type === 'Element' && node.name === 'template' && getLangAttribute(node) === 'pug';
}
Expand Down Expand Up @@ -623,4 +631,4 @@ function removeAndGetLeadingComments(ast: ASTNode, current: Node): CommentInfo[]
emptyLineAfter: getUnencodedText(newlines[i]).split('\n').length > 2,
}))
.reverse();
}
}

0 comments on commit 9fb260a

Please sign in to comment.