diff --git a/src/embed.ts b/src/embed.ts index 195bc62..6f31550 100644 --- a/src/embed.ts +++ b/src/embed.ts @@ -13,6 +13,7 @@ import { isInsideQuotedAttribute, isJSON, isLess, + isStylus, isNodeSupportedLanguage, isPugTemplate, isScss, @@ -196,7 +197,7 @@ export function embed(path: AstPath, _options: Options) { const embedType = ( tag: 'script' | 'style' | 'template', - parser: 'typescript' | 'babel-ts' | 'css' | 'scss' | 'less' | 'pug' | 'json', + parser: 'typescript' | 'babel-ts' | 'css' | 'scss' | 'less' | 'stylus' | 'pug' | 'json', isTopLevel: boolean, ) => { return async ( @@ -226,7 +227,7 @@ export function embed(path: AstPath, _options: Options) { isTopLevel, ); const embedStyle = (isTopLevel: boolean) => - embedType('style', isLess(node) ? 'less' : isScss(node) ? 'scss' : 'css', isTopLevel); + embedType('style', isStylus(node) ? 'stylus' : isLess(node) ? 'less' : isScss(node) ? 'scss' : 'css', isTopLevel); const embedPug = () => embedType('template', 'pug', false); switch (node.type) { @@ -283,7 +284,7 @@ function getSnippedContent(node: Node) { async function formatBodyContent( content: string, - parser: 'typescript' | 'babel-ts' | 'css' | 'scss' | 'less' | 'pug' | 'json', + parser: 'typescript' | 'babel-ts' | 'css' | 'scss' | 'less' | 'stylus' | 'pug' | 'json', textToDoc: (text: string, options: object) => Promise, options: ParserOptions & { pugTabWidth?: number }, ) { @@ -293,17 +294,21 @@ async function formatBodyContent( if (parser === 'pug' && typeof body === 'string') { // Pug returns no docs but a final string. // Therefore prepend the line offsets - const whitespace = options.useTabs - ? '\t' - : ' '.repeat( - options.pugTabWidth && options.pugTabWidth > 0 - ? options.pugTabWidth - : options.tabWidth, - ); - const pugBody = body - .split('\n') - .map((line) => (line ? whitespace + line : line)) - .join('\n'); + let pugBody = body; + if(options.svelteIndentScriptAndStyle){ + const whitespace = options.useTabs + ? '\t' + : ' '.repeat( + options.pugTabWidth && options.pugTabWidth > 0 + ? options.pugTabWidth + : options.tabWidth, + ); + pugBody = body + .split('\n') + .map((line) => (line ? whitespace + line : line)) + .join('\n'); + } + if(!pugBody.endsWith('\n')) pugBody += '\n'; return [hardline, pugBody]; } diff --git a/src/print/node-helpers.ts b/src/print/node-helpers.ts index 4f820fc..1165571 100644 --- a/src/print/node-helpers.ts +++ b/src/print/node-helpers.ts @@ -29,7 +29,7 @@ import { AstPath } from 'prettier'; import { findLastIndex, isASTNode, isPreTagContent } from './helpers'; import { ParserOptions, isBracketSameLine } from '../options'; -const unsupportedLanguages = ['coffee', 'coffeescript', 'styl', 'stylus', 'sass']; +const unsupportedLanguages = ['coffee', 'coffeescript', 'sass']; export function isInlineElement(path: AstPath, options: ParserOptions, node: Node) { return ( @@ -280,6 +280,11 @@ export function isLess(node: Node) { return ['less'].includes(lang); } +export function isStylus(node: Node) { + const lang = getLangAttribute(node) || ''; + return ['stylus'].includes(lang); +} + export function isScss(node: Node) { const lang = getLangAttribute(node) || ''; return ['sass', 'scss'].includes(lang);