Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix template pug format #483

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions src/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
isInsideQuotedAttribute,
isJSON,
isLess,
isStylus,
isNodeSupportedLanguage,
isPugTemplate,
isScss,
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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<Doc>,
options: ParserOptions & { pugTabWidth?: number },
) {
Expand All @@ -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];
}

Expand Down
7 changes: 6 additions & 1 deletion src/print/node-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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);
Expand Down