prettier-plugin-astro
Advanced tools
Comparing version 0.10.0 to 0.11.0
@@ -1,13 +0,11 @@ | ||
'use strict'; | ||
import { parse } from '@astrojs/compiler/sync'; | ||
import * as prettierPluginBabel from 'prettier/plugins/babel'; | ||
import 'prettier'; | ||
import { serialize } from '@astrojs/compiler/utils'; | ||
import _doc from 'prettier/doc'; | ||
import { Buffer } from 'node:buffer'; | ||
import { SassFormatter } from 'sass-formatter'; | ||
var sync = require('@astrojs/compiler/sync'); | ||
require('prettier'); | ||
var utils = require('@astrojs/compiler/utils'); | ||
var _doc = require('prettier/doc'); | ||
var node_buffer = require('node:buffer'); | ||
var sassFormatter = require('sass-formatter'); | ||
const options = { | ||
astroAllowShorthand: { | ||
since: '0.0.10', | ||
category: 'Astro', | ||
@@ -111,3 +109,3 @@ type: 'boolean', | ||
} | ||
let raw = node.children.reduce((prev, curr) => prev + utils.serialize(curr), ''); | ||
let raw = node.children.reduce((prev, curr) => prev + serialize(curr), ''); | ||
if (!stripLeadingAndTrailingNewline) { | ||
@@ -328,3 +326,3 @@ return raw; | ||
function print(path, opts, print) { | ||
const node = path.getValue(); | ||
const node = path.node; | ||
if (!node) { | ||
@@ -544,3 +542,3 @@ return ''; | ||
const textLines = text.split(/[\t\n\f\r ]+/); | ||
let docs = join$1(line$1, textLines).parts.filter((s) => s !== ''); | ||
let docs = join$1(line$1, textLines); | ||
if (startsWithLinebreak(text)) { | ||
@@ -563,101 +561,104 @@ docs[0] = hardline$1; | ||
const supportedStyleLangValues = ['css', 'scss', 'sass', 'less']; | ||
function embed(path, print, textToDoc, opts) { | ||
var _a; | ||
const node = path.getValue(); | ||
if (!node) | ||
return null; | ||
if (node.type === 'expression') { | ||
const jsxNode = makeNodeJSXCompatible(node); | ||
const textContent = printRaw(jsxNode); | ||
let content; | ||
content = wrapParserTryCatch(textToDoc, textContent, { | ||
...opts, | ||
parser: expressionParser, | ||
}); | ||
content = stripTrailingHardline(content); | ||
const strings = []; | ||
mapDoc(content, (doc) => { | ||
if (typeof doc === 'string') { | ||
strings.push(doc); | ||
const embed = ((path, options) => { | ||
const parserOption = options; | ||
return async (textToDoc, print) => { | ||
var _a; | ||
const node = path.node; | ||
if (!node) | ||
return undefined; | ||
if (node.type === 'expression') { | ||
const jsxNode = makeNodeJSXCompatible(node); | ||
const textContent = printRaw(jsxNode); | ||
let content; | ||
content = await wrapParserTryCatch(textToDoc, textContent, { | ||
...options, | ||
parser: 'astroExpressionParser', | ||
}); | ||
content = stripTrailingHardline(content); | ||
const strings = []; | ||
mapDoc(content, (doc) => { | ||
if (typeof doc === 'string') { | ||
strings.push(doc); | ||
} | ||
}); | ||
if (strings.every((value) => value.startsWith('//'))) { | ||
return group(['{', content, softline, lineSuffixBoundary, '}']); | ||
} | ||
}); | ||
if (strings.every((value) => value.startsWith('//'))) { | ||
return group(['{', content, softline, lineSuffixBoundary, '}']); | ||
const astroDoc = mapDoc(content, (doc) => { | ||
if (typeof doc === 'string') { | ||
doc = doc.replace(openingBracketReplace, '{'); | ||
doc = doc.replace(closingBracketReplace, '}'); | ||
doc = doc.replace(atSignReplace, '@'); | ||
doc = doc.replace(dotReplace, '.'); | ||
} | ||
return doc; | ||
}); | ||
return group(['{', indent([softline, astroDoc]), softline, lineSuffixBoundary, '}']); | ||
} | ||
const astroDoc = mapDoc(content, (doc) => { | ||
if (typeof doc === 'string') { | ||
doc = doc.replace(openingBracketReplace, '{'); | ||
doc = doc.replace(closingBracketReplace, '}'); | ||
doc = doc.replace(atSignReplace, '@'); | ||
doc = doc.replace(dotReplace, '.'); | ||
if (node.type === 'attribute' && node.kind === 'expression') { | ||
const value = node.value.trim(); | ||
const name = node.name.trim(); | ||
const attrNodeValue = await wrapParserTryCatch(textToDoc, value, { | ||
...options, | ||
parser: 'astroExpressionParser', | ||
}); | ||
if (name === value && options.astroAllowShorthand) { | ||
return [line, '{', attrNodeValue, '}']; | ||
} | ||
return doc; | ||
}); | ||
return group(['{', indent([softline, astroDoc]), softline, lineSuffixBoundary, '}']); | ||
} | ||
if (node.type === 'attribute' && node.kind === 'expression') { | ||
const value = node.value.trim(); | ||
const name = node.name.trim(); | ||
const attrNodeValue = wrapParserTryCatch(textToDoc, value, { | ||
...opts, | ||
parser: expressionParser, | ||
}); | ||
if (name === value && opts.astroAllowShorthand) { | ||
return [line, '{', attrNodeValue, '}']; | ||
return [line, name, '=', '{', attrNodeValue, '}']; | ||
} | ||
return [line, name, '=', '{', attrNodeValue, '}']; | ||
} | ||
if (node.type === 'attribute' && node.kind === 'spread') { | ||
const spreadContent = wrapParserTryCatch(textToDoc, node.name, { | ||
...opts, | ||
parser: expressionParser, | ||
}); | ||
return [line, '{...', spreadContent, '}']; | ||
} | ||
if (node.type === 'frontmatter') { | ||
const frontmatterContent = wrapParserTryCatch(textToDoc, node.value, { | ||
...opts, | ||
parser: 'babel-ts', | ||
}); | ||
return [group(['---', hardline, frontmatterContent, '---', hardline]), hardline]; | ||
} | ||
if (node.type === 'element' && node.name === 'script' && node.children.length) { | ||
const typeAttribute = (_a = node.attributes.find((attr) => attr.name === 'type')) === null || _a === void 0 ? void 0 : _a.value; | ||
let parser = 'babel-ts'; | ||
if (typeAttribute) { | ||
parser = inferParserByTypeAttribute(typeAttribute); | ||
if (node.type === 'attribute' && node.kind === 'spread') { | ||
const spreadContent = await wrapParserTryCatch(textToDoc, node.name, { | ||
...options, | ||
parser: 'astroExpressionParser', | ||
}); | ||
return [line, '{...', spreadContent, '}']; | ||
} | ||
const scriptContent = printRaw(node); | ||
let formattedScript = wrapParserTryCatch(textToDoc, scriptContent, { | ||
...opts, | ||
parser: parser, | ||
}); | ||
formattedScript = stripTrailingHardline(formattedScript); | ||
const isEmpty = /^\s*$/.test(scriptContent); | ||
const attributes = path.map(print, 'attributes'); | ||
const openingTag = group(['<script', indent(group(attributes)), softline, '>']); | ||
return [ | ||
openingTag, | ||
indent([isEmpty ? '' : hardline, formattedScript]), | ||
isEmpty ? '' : hardline, | ||
'</script>', | ||
]; | ||
} | ||
if (node.type === 'element' && node.name === 'style') { | ||
const content = printRaw(node); | ||
let parserLang = 'css'; | ||
if (node.attributes) { | ||
const langAttribute = node.attributes.filter((x) => x.name === 'lang'); | ||
if (langAttribute.length) { | ||
const styleLang = langAttribute[0].value.toLowerCase(); | ||
parserLang = supportedStyleLangValues.includes(styleLang) ? styleLang : undefined; | ||
if (node.type === 'frontmatter') { | ||
const frontmatterContent = await wrapParserTryCatch(textToDoc, node.value, { | ||
...options, | ||
parser: 'babel-ts', | ||
}); | ||
return [group(['---', hardline, frontmatterContent, hardline, '---', hardline]), hardline]; | ||
} | ||
if (node.type === 'element' && node.name === 'script' && node.children.length) { | ||
const typeAttribute = (_a = node.attributes.find((attr) => attr.name === 'type')) === null || _a === void 0 ? void 0 : _a.value; | ||
let parser = 'babel-ts'; | ||
if (typeAttribute) { | ||
parser = inferParserByTypeAttribute(typeAttribute); | ||
} | ||
const scriptContent = printRaw(node); | ||
let formattedScript = await wrapParserTryCatch(textToDoc, scriptContent, { | ||
...options, | ||
parser: parser, | ||
}); | ||
formattedScript = stripTrailingHardline(formattedScript); | ||
const isEmpty = /^\s*$/.test(scriptContent); | ||
const attributes = path.map(print, 'attributes'); | ||
const openingTag = group(['<script', indent(group(attributes)), softline, '>']); | ||
return [ | ||
openingTag, | ||
indent([isEmpty ? '' : hardline, formattedScript]), | ||
isEmpty ? '' : hardline, | ||
'</script>', | ||
]; | ||
} | ||
return embedStyle(parserLang, content, path, print, textToDoc, opts); | ||
} | ||
return null; | ||
} | ||
function wrapParserTryCatch(cb, text, options) { | ||
if (node.type === 'element' && node.name === 'style') { | ||
const content = printRaw(node); | ||
let parserLang = 'css'; | ||
if (node.attributes) { | ||
const langAttribute = node.attributes.filter((x) => x.name === 'lang'); | ||
if (langAttribute.length) { | ||
const styleLang = langAttribute[0].value.toLowerCase(); | ||
parserLang = supportedStyleLangValues.includes(styleLang) ? styleLang : undefined; | ||
} | ||
} | ||
return await embedStyle(parserLang, content, path, print, textToDoc, parserOption); | ||
} | ||
return undefined; | ||
}; | ||
}); | ||
async function wrapParserTryCatch(cb, text, options) { | ||
try { | ||
return cb(text, options); | ||
return await cb(text, options); | ||
} | ||
@@ -669,10 +670,2 @@ catch (e) { | ||
} | ||
function expressionParser(text, parsers, options) { | ||
const expressionContent = `<>{${text}\n}</>`; | ||
const ast = parsers['babel-ts'](expressionContent, parsers, options); | ||
return { | ||
...ast, | ||
program: ast.program.body[0].expression.children[0].expression, | ||
}; | ||
} | ||
function makeNodeJSXCompatible(node) { | ||
@@ -736,3 +729,4 @@ const newNode = { ...node }; | ||
} | ||
function embedStyle(lang, content, path, print, textToDoc, options) { | ||
async function embedStyle(lang, content, path, print, textToDoc, options) { | ||
var _a; | ||
const isEmpty = /^\s*$/.test(content); | ||
@@ -743,3 +737,6 @@ switch (lang) { | ||
case 'scss': { | ||
let formattedStyles = wrapParserTryCatch(textToDoc, content, { ...options, parser: lang }); | ||
let formattedStyles = await wrapParserTryCatch(textToDoc, content, { | ||
...options, | ||
parser: lang, | ||
}); | ||
formattedStyles = stripTrailingHardline(formattedStyles); | ||
@@ -756,3 +753,3 @@ const attributes = path.map(print, 'attributes'); | ||
case 'sass': { | ||
const lineEnding = options.endOfLine.toUpperCase() === 'CRLF' ? 'CRLF' : 'LF'; | ||
const lineEnding = ((_a = options === null || options === void 0 ? void 0 : options.endOfLine) === null || _a === void 0 ? void 0 : _a.toUpperCase()) === 'CRLF' ? 'CRLF' : 'LF'; | ||
const sassOptions = { | ||
@@ -764,3 +761,3 @@ tabSize: options.tabWidth, | ||
const { result: raw } = manualDedent(content); | ||
const formattedSassIndented = sassFormatter.SassFormatter.Format(raw, sassOptions).trim(); | ||
const formattedSassIndented = SassFormatter.Format(raw, sassOptions).trim(); | ||
const formattedSass = join(hardline, formattedSassIndented.split('\n')); | ||
@@ -779,7 +776,7 @@ const attributes = path.map(print, 'attributes'); | ||
if (node) { | ||
return node_buffer.Buffer.from(options.originalText) | ||
return Buffer.from(options.originalText) | ||
.subarray(options.locStart(node), options.locEnd(node)) | ||
.toString(); | ||
} | ||
return null; | ||
return undefined; | ||
} | ||
@@ -789,2 +786,3 @@ } | ||
const babelParser = prettierPluginBabel.parsers['babel-ts']; | ||
const languages = [ | ||
@@ -800,3 +798,3 @@ { | ||
astro: { | ||
parse: (source) => sync.parse(source, { position: true }).ast, | ||
parse: (source) => parse(source, { position: true }).ast, | ||
astFormat: 'astro', | ||
@@ -806,2 +804,15 @@ locStart: (node) => node.position.start.offset, | ||
}, | ||
astroExpressionParser: { | ||
...babelParser, | ||
preprocess(text) { | ||
return `<>{${text}\n}</>`; | ||
}, | ||
parse(text, opts) { | ||
const ast = babelParser.parse(text, opts); | ||
return { | ||
...ast, | ||
program: ast.program.body[0].expression.children[0].expression, | ||
}; | ||
}, | ||
}, | ||
}; | ||
@@ -818,7 +829,3 @@ const printers = { | ||
exports.defaultOptions = defaultOptions; | ||
exports.languages = languages; | ||
exports.options = options; | ||
exports.parsers = parsers; | ||
exports.printers = printers; | ||
export { defaultOptions, languages, options, parsers, printers }; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "prettier-plugin-astro", | ||
"version": "0.10.0", | ||
"type": "commonjs", | ||
"version": "0.11.0", | ||
"type": "module", | ||
"description": "A Prettier Plugin for formatting Astro files", | ||
@@ -15,3 +15,3 @@ "main": "dist/index.js", | ||
}, | ||
"packageManager": "pnpm@7.14.0", | ||
"packageManager": "pnpm@8.6.2", | ||
"homepage": "https://github.com/withastro/prettier-plugin-astro/", | ||
@@ -33,4 +33,4 @@ "issues": { | ||
"dependencies": { | ||
"@astrojs/compiler": "^1.5.0", | ||
"prettier": "^2.8.8", | ||
"@astrojs/compiler": "^1.5.5", | ||
"prettier": "^3.0.0", | ||
"sass-formatter": "^0.7.6" | ||
@@ -43,9 +43,8 @@ }, | ||
"@types/node": "^20.2.5", | ||
"@types/prettier": "^2.7.3", | ||
"@typescript-eslint/eslint-plugin": "^5.59.8", | ||
"@typescript-eslint/parser": "^5.59.8", | ||
"@typescript-eslint/eslint-plugin": "^5.61.0", | ||
"@typescript-eslint/parser": "^5.61.0", | ||
"@vitest/ui": "^0.31.3", | ||
"eslint": "^8.41.0", | ||
"eslint": "^8.44.0", | ||
"eslint-config-prettier": "^8.8.0", | ||
"eslint-plugin-prettier": "^4.2.1", | ||
"eslint-plugin-prettier": "5.0.0-alpha.2", | ||
"eslint-plugin-prettier-doc": "^1.1.0", | ||
@@ -55,3 +54,4 @@ "rollup": "^3.23.0", | ||
"typescript": "^5.0.4", | ||
"vitest": "^0.31.3" | ||
"vitest": "^0.31.3", | ||
"vite": "^4.4.3" | ||
}, | ||
@@ -58,0 +58,0 @@ "scripts": { |
@@ -13,3 +13,3 @@ # [Prettier](https://prettier.io/) Plugin for [Astro](https://astro.build/) | ||
For optimal compatibility with the different package managers and Prettier plugins, we recommend manually specifying your plugins and the parser to use for Astro files in your Prettier config as shown in the example below: | ||
For optimal compatibility with the different package managers and Prettier plugins, we recommend manually specifying the parser to use for Astro files in your Prettier config as shown in the example below: | ||
@@ -16,0 +16,0 @@ ```js |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
103560
810
Yes
+ Addedprettier@3.3.3(transitive)
- Removedprettier@2.8.8(transitive)
Updated@astrojs/compiler@^1.5.5
Updatedprettier@^3.0.0