prettier-plugin-astro
Advanced tools
Comparing version 0.1.0-next.1 to 0.1.0-next.2
# prettier-plugin-astro | ||
## 0.1.0-next.2 | ||
### Patch Changes | ||
- f244f83: Move from ava to vitest | ||
- 67341e7: Format directives | ||
- 0ed661a: Fix: Format expression | ||
- 87b0508: Change prettier width from 180 to 80 | ||
- ace9861: Remove node 12 from ci | ||
## 0.1.0-next.1 | ||
@@ -4,0 +14,0 @@ |
@@ -65,3 +65,18 @@ 'use strict'; | ||
const blockElements = [...blockElementsT]; | ||
const selfClosingTags = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr']; | ||
const selfClosingTags = [ | ||
'area', | ||
'base', | ||
'br', | ||
'col', | ||
'embed', | ||
'hr', | ||
'img', | ||
'input', | ||
'link', | ||
'meta', | ||
'param', | ||
'source', | ||
'track', | ||
'wbr', | ||
]; | ||
@@ -87,8 +102,5 @@ const serializeSync = makeSynchronous__default["default"](async (node) => { | ||
return false; | ||
return path.stack.some((node) => (node.type === 'element' && node.name.toLowerCase() === 'pre') || (node.type === 'attribute' && !formattableAttributes.includes(node.name))); | ||
return path.stack.some((node) => (node.type === 'element' && node.name.toLowerCase() === 'pre') || | ||
(node.type === 'attribute' && !formattableAttributes.includes(node.name))); | ||
}; | ||
function getText(node, opts) { | ||
var _a, _b, _c; | ||
return opts.originalText.slice(((_a = node.position) === null || _a === void 0 ? void 0 : _a.start.offset) + 1, (_c = (_b = node.position) === null || _b === void 0 ? void 0 : _b.end) === null || _c === void 0 ? void 0 : _c.offset); | ||
} | ||
function getUnencodedText(node) { | ||
@@ -123,6 +135,13 @@ return node.value; | ||
function isInlineElement(path, opts, node) { | ||
return node && node.type === 'element' && !isBlockElement(node, opts) && !isPreTagContent(path); | ||
return (node && | ||
node.type === 'element' && | ||
!isBlockElement(node, opts) && | ||
!isPreTagContent(path)); | ||
} | ||
function isBlockElement(node, opts) { | ||
return node && node.type === 'element' && opts.htmlWhitespaceSensitivity !== 'strict' && (opts.htmlWhitespaceSensitivity === 'ignore' || blockElements.includes(node.name)); | ||
return (node && | ||
node.type === 'element' && | ||
opts.htmlWhitespaceSensitivity !== 'strict' && | ||
(opts.htmlWhitespaceSensitivity === 'ignore' || | ||
blockElements.includes(node.name))); | ||
} | ||
@@ -197,3 +216,3 @@ function isTextNodeStartingWithLinebreak(node, nrLines = 1) { | ||
let result = ''; | ||
for (var i = 0; i < input.length; i++) { | ||
for (let i = 0; i < input.length; i++) { | ||
result += input[i] | ||
@@ -209,5 +228,5 @@ .replace(/\\\n[ \t]*/g, '') | ||
lines.forEach(function (l) { | ||
var m = l.match(/^(\s+)\S+/); | ||
const m = l.match(/^(\s+)\S+/); | ||
if (m) { | ||
var indent = m[1].length; | ||
const indent = m[1].length; | ||
if (!mindent) { | ||
@@ -223,3 +242,3 @@ mindent = indent; | ||
(function () { | ||
var m = mindent; | ||
const m = mindent; | ||
result = lines | ||
@@ -240,3 +259,4 @@ .map(function (l) { | ||
let defaultMatch; | ||
while ((defaultMatch = /import\s+([^\s]+)\s+from\s+['|"|`]astro\/components\/Markdown\.astro/g.exec(script))) { | ||
while ((defaultMatch = | ||
/import\s+([^\s]+)\s+from\s+['|"|`]astro\/components\/Markdown\.astro/g.exec(script))) { | ||
if (defaultMatch[1]) | ||
@@ -246,6 +266,11 @@ return new Set([defaultMatch[1].trim()]); | ||
let namedMatch; | ||
while ((namedMatch = /import\s+\{\s*([^}]+)\}\s+from\s+['|"|`]astro\/components/g.exec(script))) { | ||
while ((namedMatch = | ||
/import\s+\{\s*([^}]+)\}\s+from\s+['|"|`]astro\/components/g.exec(script))) { | ||
if (namedMatch[1] && !namedMatch[1].includes('Markdown')) | ||
continue; | ||
const rawImports = namedMatch[1].trim().replace(/^\{/, '').replace(/\}$/, '').trim(); | ||
const rawImports = namedMatch[1] | ||
.trim() | ||
.replace(/^\{/, '') | ||
.replace(/\}$/, '') | ||
.trim(); | ||
let importName = 'Markdown'; | ||
@@ -267,3 +292,3 @@ for (const spec of rawImports.split(',')) { | ||
const { builders: { breakParent, dedent, fill, group, hardline, indent, join, line, literalline, softline }, utils: { removeLines, stripTrailingHardline }, } = _doc__default["default"]; | ||
const { builders: { breakParent, dedent, fill, group, hardline, indent, join, line, literalline, softline, }, utils: { removeLines, stripTrailingHardline }, } = _doc__default["default"]; | ||
function printComment(commentPath, options) { | ||
@@ -313,4 +338,5 @@ return commentPath; | ||
} | ||
const isSelfClosingTag = isEmpty && (node.type !== 'element' || selfClosingTags.indexOf(node.name) !== -1); | ||
const attributes = node.attributes ? path.map(print, 'attributes') : []; | ||
const isSelfClosingTag = isEmpty && | ||
(node.type !== 'element' || selfClosingTags.indexOf(node.name) !== -1); | ||
const attributes = path.map(print, 'attributes'); | ||
if (isSelfClosingTag) { | ||
@@ -325,8 +351,11 @@ return group(['<', node.name, indent(group(attributes)), line, `/>`]); | ||
let noHugSeparatorEnd = softline; | ||
let hugStart = shouldHugStart(node, opts); | ||
let hugEnd = shouldHugEnd(node, opts); | ||
const hugStart = shouldHugStart(node, opts); | ||
const hugEnd = shouldHugEnd(node, opts); | ||
let body; | ||
if (isEmpty) { | ||
body = | ||
isInlineElement(path, opts, node) && node.children.length && isTextNodeStartingWithWhitespace(node.children[0]) && !isPreTagContent(path) | ||
isInlineElement(path, opts, node) && | ||
node.children.length && | ||
isTextNodeStartingWithWhitespace(node.children[0]) && | ||
!isPreTagContent(path) | ||
? () => line | ||
@@ -339,3 +368,4 @@ : | ||
} | ||
else if (isInlineElement(path, opts, node) && !isPreTagContent(path)) { | ||
else if (isInlineElement(path, opts, node) && | ||
!isPreTagContent(path)) { | ||
body = () => path.map(print, 'children'); | ||
@@ -346,7 +376,26 @@ } | ||
} | ||
const openingTag = ['<', node.name, indent(group([...attributes, hugStart ? '' : !isPreTagContent(path) && !opts.bracketSameLine ? dedent(softline) : '']))]; | ||
const openingTag = [ | ||
'<', | ||
node.name, | ||
indent(group([ | ||
...attributes, | ||
hugStart | ||
? '' | ||
: !isPreTagContent(path) && !opts.bracketSameLine | ||
? dedent(softline) | ||
: '', | ||
])), | ||
]; | ||
if (hugStart && hugEnd) { | ||
const huggedContent = [softline, group(['>', body(), `</${node.name}`])]; | ||
const huggedContent = [ | ||
softline, | ||
group(['>', body(), `</${node.name}`]), | ||
]; | ||
const omitSoftlineBeforeClosingTag = isEmpty || canOmitSoftlineBeforeClosingTag(path, opts); | ||
return group([...openingTag, isEmpty ? group(huggedContent) : group(indent(huggedContent)), omitSoftlineBeforeClosingTag ? '' : softline, '>']); | ||
return group([ | ||
...openingTag, | ||
isEmpty ? group(huggedContent) : group(indent(huggedContent)), | ||
omitSoftlineBeforeClosingTag ? '' : softline, | ||
'>', | ||
]); | ||
} | ||
@@ -360,3 +409,6 @@ if (isPreTagContent(path)) { | ||
if (!hugStart && firstChild && isTextNode(firstChild)) { | ||
if (isTextNodeStartingWithLinebreak(firstChild) && firstChild !== lastChild && (!isInlineElement(path, opts, node) || isTextNodeEndingWithWhitespace(lastChild))) { | ||
if (isTextNodeStartingWithLinebreak(firstChild) && | ||
firstChild !== lastChild && | ||
(!isInlineElement(path, opts, node) || | ||
isTextNodeEndingWithWhitespace(lastChild))) { | ||
noHugSeparatorStart = hardline; | ||
@@ -379,6 +431,17 @@ noHugSeparatorEnd = hardline; | ||
if (hugStart) { | ||
return group([...openingTag, indent([softline, group(['>', body()])]), noHugSeparatorEnd, `</${node.name}>`]); | ||
return group([ | ||
...openingTag, | ||
indent([softline, group(['>', body()])]), | ||
noHugSeparatorEnd, | ||
`</${node.name}>`, | ||
]); | ||
} | ||
if (hugEnd) { | ||
return group([...openingTag, '>', indent([noHugSeparatorStart, group([body(), `</${node.name}`])]), canOmitSoftlineBeforeClosingTag(path, opts) ? '' : softline, '>']); | ||
return group([ | ||
...openingTag, | ||
'>', | ||
indent([noHugSeparatorStart, group([body(), `</${node.name}`])]), | ||
canOmitSoftlineBeforeClosingTag(path, opts) ? '' : softline, | ||
'>', | ||
]); | ||
} | ||
@@ -388,3 +451,9 @@ if (isEmpty) { | ||
} | ||
return group([...openingTag, '>', indent([noHugSeparatorStart, body()]), noHugSeparatorEnd, `</${node.name}>`]); | ||
return group([ | ||
...openingTag, | ||
'>', | ||
indent([noHugSeparatorStart, body()]), | ||
noHugSeparatorEnd, | ||
`</${node.name}>`, | ||
]); | ||
} | ||
@@ -412,5 +481,2 @@ return ''; | ||
} | ||
case 'directive': { | ||
return [line, node.name]; | ||
} | ||
case 'doctype': { | ||
@@ -428,3 +494,3 @@ return ['<!DOCTYPE html>', hardline]; | ||
const text = getUnencodedText(node); | ||
let textLines = text.split(/[\t\n\f\r ]+/); | ||
const textLines = text.split(/[\t\n\f\r ]+/); | ||
let docs = join(line, textLines).parts.filter((s) => s !== ''); | ||
@@ -457,11 +523,10 @@ if (startsWithLinebreak(text)) { | ||
if (node.type === 'expression') { | ||
const textContent = getText(node, opts); | ||
const textContent = printRaw(node); | ||
let content; | ||
if (node.children.length === 1) { | ||
content = textContent; | ||
} | ||
else { | ||
content = textToDoc(textContent, { ...opts, parser: 'babel-ts', semi: false }); | ||
content = stripTrailingHardline(content); | ||
} | ||
content = textToDoc(forceIntoExpression(textContent), { | ||
...opts, | ||
parser: expressionParser, | ||
semi: false, | ||
}); | ||
content = stripTrailingHardline(content); | ||
return [ | ||
@@ -476,3 +541,7 @@ '{', | ||
const name = node.name.trim(); | ||
let attrNodeValue = textToDoc(forceIntoExpression(value), { ...opts, parser: expressionParser, semi: false }); | ||
let attrNodeValue = textToDoc(forceIntoExpression(value), { | ||
...opts, | ||
parser: expressionParser, | ||
semi: false, | ||
}); | ||
attrNodeValue = stripTrailingHardline(attrNodeValue); | ||
@@ -486,14 +555,36 @@ if (name === value && opts.astroAllowShorthand) { | ||
markdownComponentName = getMarkdownName(node.value); | ||
return [group(['---', hardline, textToDoc(node.value, { ...opts, parser: 'typescript' }), '---', hardline]), hardline]; | ||
return [ | ||
group([ | ||
'---', | ||
hardline, | ||
textToDoc(node.value, { ...opts, parser: 'typescript' }), | ||
'---', | ||
hardline, | ||
]), | ||
hardline, | ||
]; | ||
} | ||
if (node.type === 'element' && node.name === 'script') { | ||
const scriptContent = node.children[0].value; | ||
let formatttedScript = textToDoc(scriptContent, { ...opts, parser: 'typescript' }); | ||
let formatttedScript = textToDoc(scriptContent, { | ||
...opts, | ||
parser: 'typescript', | ||
}); | ||
formatttedScript = stripTrailingHardline(formatttedScript); | ||
const attributes = node.attributes ? path.map(print, 'attributes') : []; | ||
const openingTag = group(['<script', indent(group(attributes)), softline, '>']); | ||
return [openingTag, indent([hardline, formatttedScript]), hardline, '</script>']; | ||
const attributes = path.map(print, 'attributes'); | ||
const openingTag = group([ | ||
'<script', | ||
indent(group(attributes)), | ||
softline, | ||
'>', | ||
]); | ||
return [ | ||
openingTag, | ||
indent([hardline, formatttedScript]), | ||
hardline, | ||
'</script>', | ||
]; | ||
} | ||
if (node.type === 'element' && node.name === 'style') { | ||
let styleTagContent = node.children[0].value.trim(); | ||
const styleTagContent = node.children[0].value.trim(); | ||
const supportedStyleLangValues = ['css', 'scss', 'sass']; | ||
@@ -512,9 +603,20 @@ let parserLang = 'css'; | ||
case 'scss': { | ||
let formattedStyles = textToDoc(styleTagContent, { ...opts, parser: parserLang }); | ||
let formattedStyles = textToDoc(styleTagContent, { | ||
...opts, | ||
parser: parserLang, | ||
}); | ||
formattedStyles = stripTrailingHardline(formattedStyles); | ||
const attributes = node.attributes ? path.map(print, 'attributes') : []; | ||
const directives = node.directives ? path.map(print, 'directives') : []; | ||
const attrAndDirectives = directives.concat(attributes); | ||
const openingTag = group(['<style', indent(group(attrAndDirectives)), softline, '>']); | ||
return [openingTag, indent([hardline, formattedStyles]), hardline, '</style>']; | ||
const attributes = path.map(print, 'attributes'); | ||
const openingTag = group([ | ||
'<style', | ||
indent(group(attributes)), | ||
softline, | ||
'>', | ||
]); | ||
return [ | ||
openingTag, | ||
indent([hardline, formattedStyles]), | ||
hardline, | ||
'</style>', | ||
]; | ||
} | ||
@@ -529,7 +631,17 @@ case 'sass': { | ||
const { result: raw } = manualDedent(styleTagContent); | ||
let formattedSassIndented = sassFormatter.SassFormatter.Format(raw, sassOptions).trim(); | ||
const formattedSassIndented = sassFormatter.SassFormatter.Format(raw, sassOptions).trim(); | ||
const formattedSass = join(hardline, formattedSassIndented.split('\n')); | ||
const attributes = node.attributes ? path.map(print, 'attributes') : []; | ||
const openingTag = group(['<style', indent(group(attributes)), softline, '>']); | ||
return [openingTag, indent(group([hardline, formattedSass])), hardline, '</style>']; | ||
const attributes = path.map(print, 'attributes'); | ||
const openingTag = group([ | ||
'<style', | ||
indent(group(attributes)), | ||
softline, | ||
'>', | ||
]); | ||
return [ | ||
openingTag, | ||
indent(group([hardline, formattedSass])), | ||
hardline, | ||
'</style>', | ||
]; | ||
} | ||
@@ -543,7 +655,20 @@ } | ||
content = contentArr.join('\n'); | ||
let formatttedMarkdown = textToDoc(content, { ...opts, parser: 'markdown' }); | ||
let formatttedMarkdown = textToDoc(content, { | ||
...opts, | ||
parser: 'markdown', | ||
}); | ||
formatttedMarkdown = stripTrailingHardline(formatttedMarkdown); | ||
const attributes = path.map(print, 'attributes'); | ||
const openingTag = group([`<${node.name}`, indent(group(attributes)), softline, '>']); | ||
return [openingTag, indent(group([hardline, formatttedMarkdown])), hardline, `</${node.name}>`]; | ||
const openingTag = group([ | ||
`<${node.name}`, | ||
indent(group(attributes)), | ||
softline, | ||
'>', | ||
]); | ||
return [ | ||
openingTag, | ||
indent(group([hardline, formatttedMarkdown])), | ||
hardline, | ||
`</${node.name}>`, | ||
]; | ||
} | ||
@@ -550,0 +675,0 @@ return null; |
{ | ||
"name": "prettier-plugin-astro", | ||
"version": "0.1.0-next.1", | ||
"version": "0.1.0-next.2", | ||
"type": "commonjs", | ||
@@ -11,3 +11,3 @@ "description": "A Prettier Plugin for formatting Astro files", | ||
"engines": { | ||
"node": "^12.20.0 || ^14.13.1 || >=16.0.0", | ||
"node": "^14.13.1 || >=16.0.0", | ||
"npm": ">=6.14.0" | ||
@@ -33,4 +33,4 @@ }, | ||
"dev": "rollup -c -w", | ||
"test": "ava test/*.test.ts", | ||
"test:w": "ava -w test/*.test.ts", | ||
"test": "vitest run", | ||
"test:w": "vitest", | ||
"release": "yarn build && changeset publish", | ||
@@ -42,3 +42,3 @@ "lint": "eslint .", | ||
"dependencies": { | ||
"@astrojs/compiler": "^0.12.1", | ||
"@astrojs/compiler": "^0.13.1", | ||
"make-synchronous": "0.1.1", | ||
@@ -53,6 +53,8 @@ "prettier": "^2.4.1", | ||
"@types/prettier": "^2.4.1", | ||
"ava": "4.0.1", | ||
"@typescript-eslint/eslint-plugin": "^5.16.0", | ||
"@typescript-eslint/parser": "^5.16.0", | ||
"@vitest/ui": "^0.7.7", | ||
"eslint": "^8.0.0", | ||
"eslint-plugin-ava": "^13.0.0", | ||
"eslint-plugin-node": "^11.1.0", | ||
"eslint-config-prettier": "^8.5.0", | ||
"eslint-plugin-prettier": "^4.0.0", | ||
"eslint-plugin-prettier-doc": "^1.0.1", | ||
@@ -63,4 +65,5 @@ "rollup": "^2.58.0", | ||
"tslib": "^2.3.1", | ||
"typescript": "^4.4.3" | ||
"typescript": "^4.4.3", | ||
"vitest": "^0.7.7" | ||
} | ||
} |
@@ -22,3 +22,3 @@ # Beta Prettier Plugin for [Astro](https://github.com/withastro/astro) | ||
1. `yarn build` | ||
1. Run tests with [`yarn test`](https://github.com/avajs/ava/tree/main/docs) | ||
1. Run tests with [`yarn test`](https://vitest.dev/guide/) | ||
1. Lint code with `yarn lint` | ||
@@ -25,0 +25,0 @@ 1. Format code with `yarn format` |
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
106159
706
17
+ Added@astrojs/compiler@0.13.2(transitive)
- Removed@astrojs/compiler@0.12.1(transitive)
Updated@astrojs/compiler@^0.13.1