prettier-plugin-marko
Advanced tools
Comparing version 1.4.7 to 1.4.8
@@ -246,18 +246,62 @@ "use strict"; | ||
// src/utils/get-original-code.ts | ||
var import_generator = __toESM(require("@babel/generator")); | ||
var generate = import_generator.default.default || import_generator.default; | ||
function getOriginalCodeForNode(opts, node) { | ||
var _a, _b; | ||
const loc = node.loc; | ||
if (!loc) { | ||
return generate(node, { | ||
filename: opts.filepath, | ||
compact: false, | ||
comments: true, | ||
sourceMaps: false | ||
}).code; | ||
} | ||
let start = loc.start; | ||
if ((_a = node.leadingComments) == null ? void 0 : _a.length) { | ||
const commentStart = node.leadingComments[0].loc.start; | ||
if (commentStart.line < start.line || commentStart.line === start.line && commentStart.column < start.column) { | ||
start = commentStart; | ||
} | ||
} | ||
let end = loc.end; | ||
if ((_b = node.trailingComments) == null ? void 0 : _b.length) { | ||
const commentEnd = node.trailingComments[node.trailingComments.length - 1].loc.end; | ||
if (commentEnd.line > end.line || commentEnd.line === end.line && commentEnd.column > end.column) { | ||
end = commentEnd; | ||
} | ||
} | ||
return opts.originalText.slice(locToPos(start, opts), locToPos(end, opts)); | ||
} | ||
function getOriginalCodeForList(opts, sep, list) { | ||
return list.map((node) => getOriginalCodeForNode(opts, node)).join(sep); | ||
} | ||
// src/utils/with-block-if-needed.ts | ||
var { builders: b2 } = import_prettier2.doc; | ||
function withBlockIfNeeded(nodes, opts, getDocs) { | ||
if (nodes.length > 1 || !enclosedNodeTypeReg.test(nodes[0].type) && outerCodeMatches( | ||
import_prettier2.doc.printer.printDocToString(getDocs(), { | ||
function withBlockIfNeeded(nodes, opts, docs) { | ||
let count = 0; | ||
let statement; | ||
for (const node of nodes) { | ||
if (node.type === "EmptyStatement") | ||
continue; | ||
if (++count > 1) | ||
break; | ||
statement = node; | ||
} | ||
if (count > 1 || !enclosedNodeTypeReg.test(statement.type) && outerCodeMatches( | ||
(0, import_prettier2.format)(getOriginalCodeForNode(opts, statement), { | ||
...opts, | ||
printWidth: 0 | ||
}).formatted, | ||
printWidth: 0, | ||
parser: opts.markoScriptParser | ||
}).trim(), | ||
/[\n\r]/y | ||
)) { | ||
return [ | ||
b2.indent([b2.ifBreak(["{", b2.line]), b2.join(b2.hardline, getDocs())]), | ||
b2.indent([b2.ifBreak(["{", b2.line]), b2.join(b2.hardline, docs)]), | ||
b2.ifBreak([b2.line, "}"]) | ||
]; | ||
} | ||
return getDocs(); | ||
return docs; | ||
} | ||
@@ -268,15 +312,16 @@ | ||
var { builders: b3 } = import_prettier3.doc; | ||
function withParensIfNeeded(node, opts, getValDoc) { | ||
function withParensIfNeeded(node, opts, doc6) { | ||
var _a, _b; | ||
if (((_a = node.leadingComments) == null ? void 0 : _a.length) || ((_b = node.trailingComments) == null ? void 0 : _b.length) || !enclosedNodeTypeReg.test(node.type) && outerCodeMatches( | ||
import_prettier3.doc.printer.printDocToString(getValDoc(), { | ||
(0, import_prettier3.format)(`_(${getOriginalCodeForNode(opts, node)})`, { | ||
...opts, | ||
printWidth: 0 | ||
}).formatted, | ||
printWidth: 0, | ||
parser: opts.markoScriptParser | ||
}).replace(/^_\(([\s\S]*)\);?$/m, "$1").trim(), | ||
/\s|>/y, | ||
opts.markoAttrParen | ||
)) { | ||
return ["(", b3.indent([b3.softline, getValDoc()]), b3.softline, ")"]; | ||
return ["(", b3.indent([b3.softline, doc6]), b3.softline, ")"]; | ||
} | ||
return getValDoc(); | ||
return doc6; | ||
} | ||
@@ -316,36 +361,2 @@ | ||
// src/utils/get-original-code.ts | ||
var import_generator = __toESM(require("@babel/generator")); | ||
var generate = import_generator.default.default || import_generator.default; | ||
function getOriginalCodeForNode(opts, node) { | ||
var _a, _b; | ||
const loc = node.loc; | ||
if (!loc) { | ||
return generate(node, { | ||
filename: opts.filepath, | ||
compact: false, | ||
comments: true, | ||
sourceMaps: false | ||
}).code; | ||
} | ||
let start = loc.start; | ||
if ((_a = node.leadingComments) == null ? void 0 : _a.length) { | ||
const commentStart = node.leadingComments[0].loc.start; | ||
if (commentStart.line < start.line || commentStart.line === start.line && commentStart.column < start.column) { | ||
start = commentStart; | ||
} | ||
} | ||
let end = loc.end; | ||
if ((_b = node.trailingComments) == null ? void 0 : _b.length) { | ||
const commentEnd = node.trailingComments[node.trailingComments.length - 1].loc.end; | ||
if (commentEnd.line > end.line || commentEnd.line === end.line && commentEnd.column > end.column) { | ||
end = commentEnd; | ||
} | ||
} | ||
return opts.originalText.slice(locToPos(start, opts), locToPos(end, opts)); | ||
} | ||
function getOriginalCodeForList(opts, sep, list) { | ||
return list.map((node) => getOriginalCodeForNode(opts, node)).join(sep); | ||
} | ||
// src/index.ts | ||
@@ -608,3 +619,6 @@ var defaultFilePath = (0, import_path.resolve)("index.marko"); | ||
b5.softline, | ||
b5.join([",", b5.line], tagPath.map(print, "arguments")), | ||
b5.join( | ||
[",", b5.line], | ||
tagPath.map((it) => print(it), "arguments") | ||
), | ||
opts.trailingComma ? b5.ifBreak(",") : "" | ||
@@ -633,16 +647,16 @@ ]), | ||
const attrsDoc = []; | ||
tagPath.each((childPath) => { | ||
const childNode = childPath.getValue(); | ||
if (import_compiler.types.isMarkoAttribute(childNode) && (childNode.name === "class" || childNode.name === "id")) { | ||
if ((literalTagName === "style" || opts.markoSyntax === "concise") && import_compiler.types.isStringLiteral(childNode.value) && !childNode.modifier && shorthandIdOrClassReg.test(childNode.value.value)) { | ||
const symbol = childNode.name === "class" ? "." : "#"; | ||
doc6[shorthandIndex] += symbol + childNode.value.value.split(/ +/).join(symbol); | ||
tagPath.each((attrPath) => { | ||
const attrNode = attrPath.getValue(); | ||
if (import_compiler.types.isMarkoAttribute(attrNode) && (attrNode.name === "class" || attrNode.name === "id")) { | ||
if ((literalTagName === "style" || opts.markoSyntax === "concise") && import_compiler.types.isStringLiteral(attrNode.value) && !attrNode.modifier && shorthandIdOrClassReg.test(attrNode.value.value)) { | ||
const symbol = attrNode.name === "class" ? "." : "#"; | ||
doc6[shorthandIndex] += symbol + attrNode.value.value.split(/ +/).join(symbol); | ||
} else { | ||
childNode.value.loc = null; | ||
attrsDoc.push(print(childPath)); | ||
attrNode.value.loc = null; | ||
attrsDoc.push(print(attrPath)); | ||
} | ||
} else if (childNode.default) { | ||
doc6.push(print(childPath)); | ||
} else if (attrNode.default) { | ||
doc6.push(print(attrPath)); | ||
} else { | ||
attrsDoc.push(print(childPath)); | ||
attrsDoc.push(print(attrPath)); | ||
} | ||
@@ -676,9 +690,9 @@ }, "attributes"); | ||
tagPath.each( | ||
(child) => { | ||
const node2 = child.getValue(); | ||
if (node2.type === "MarkoText") { | ||
embeddedCode += node2.value; | ||
(childPath) => { | ||
const childNode = childPath.getValue(); | ||
if (childNode.type === "MarkoText") { | ||
embeddedCode += childNode.value; | ||
} else { | ||
embeddedCode += `__EMBEDDED_PLACEHOLDER_${placeholderId++}__`; | ||
placeholders.push(print(child)); | ||
placeholders.push(print(childPath)); | ||
} | ||
@@ -704,7 +718,7 @@ }, | ||
tagPath.each( | ||
(child, i) => { | ||
const childNode = child.getValue(); | ||
(childPath, i) => { | ||
const childNode = childPath.getValue(); | ||
const isText2 = isTextLike(childNode, node); | ||
if (isText2) { | ||
textDocs.push(print(child)); | ||
textDocs.push(print(childPath)); | ||
if (i !== lastIndex) | ||
@@ -727,6 +741,6 @@ return; | ||
textDocs = []; | ||
bodyDocs.push(print(child)); | ||
bodyDocs.push(print(childPath)); | ||
} | ||
} else { | ||
bodyDocs.push(print(child)); | ||
bodyDocs.push(print(childPath)); | ||
} | ||
@@ -775,3 +789,6 @@ }, | ||
b5.softline, | ||
b5.join([",", b5.line], attrPath.map(print, "arguments")), | ||
b5.join( | ||
[",", b5.line], | ||
attrPath.map((it) => print(it), "arguments") | ||
), | ||
opts.trailingComma ? b5.ifBreak(",") : "" | ||
@@ -787,2 +804,14 @@ ]), | ||
if (import_compiler.types.isFunctionExpression(value) && !(value.id || value.async || value.generator)) { | ||
const methodBodyDocs = []; | ||
attrPath.each( | ||
(childPath) => { | ||
var _a2; | ||
if (((_a2 = childPath.getNode()) == null ? void 0 : _a2.type) !== "EmptyStatement") { | ||
methodBodyDocs.push(print(childPath)); | ||
} | ||
}, | ||
"value", | ||
"body", | ||
"body" | ||
); | ||
doc6.push( | ||
@@ -799,14 +828,8 @@ b5.group([ | ||
]), | ||
b5.group([ | ||
methodBodyDocs.length ? b5.group([ | ||
" {", | ||
b5.indent([ | ||
b5.line, | ||
b5.join( | ||
b5.hardline, | ||
attrPath.map(print, "value", "body", "body") | ||
) | ||
]), | ||
b5.indent([b5.line, b5.join(b5.hardline, methodBodyDocs)]), | ||
b5.line, | ||
"}" | ||
]) | ||
]) : " {}" | ||
); | ||
@@ -817,7 +840,3 @@ } else { | ||
b5.group( | ||
enclosedNodeTypeReg.test(node.type) ? attrPath.call(print, "value") : withParensIfNeeded( | ||
value, | ||
opts, | ||
() => attrPath.call(print, "value") | ||
) | ||
withParensIfNeeded(value, opts, attrPath.call(print, "value")) | ||
) | ||
@@ -834,3 +853,3 @@ ); | ||
opts, | ||
() => path.call(print, "value") | ||
path.call(print, "value") | ||
) | ||
@@ -845,3 +864,10 @@ ); | ||
]; | ||
case "MarkoScriptlet": | ||
case "MarkoScriptlet": { | ||
const bodyDocs = []; | ||
path.each((childPath) => { | ||
var _a2; | ||
if (((_a2 = childPath.getNode()) == null ? void 0 : _a2.type) !== "EmptyStatement") { | ||
bodyDocs.push(print(childPath)); | ||
} | ||
}, "body"); | ||
return withLineIfNeeded( | ||
@@ -852,5 +878,6 @@ node.body[0], | ||
node.static ? "static " : "$ ", | ||
withBlockIfNeeded(node.body, opts, () => path.map(print, "body")) | ||
withBlockIfNeeded(node.body, opts, bodyDocs) | ||
]) | ||
); | ||
} | ||
case "MarkoText": { | ||
@@ -935,2 +962,3 @@ const quote = opts.singleQuote ? "'" : '"'; | ||
case "Program": | ||
case "EmptyStatement": | ||
return null; | ||
@@ -937,0 +965,0 @@ default: |
import { types as t } from "@marko/compiler"; | ||
import { doc, Doc, ParserOptions } from "prettier"; | ||
export default function withBlockIfNeeded(nodes: t.Statement[], opts: ParserOptions, getDocs: () => Doc[]): doc.builders.Doc[]; | ||
export default function withBlockIfNeeded(nodes: t.Statement[], opts: ParserOptions, docs: Doc[]): doc.builders.Doc[]; |
import { doc, Doc, ParserOptions } from "prettier"; | ||
import { Node } from "../constants"; | ||
export default function withParensIfNeeded(node: Node, opts: ParserOptions, getValDoc: () => Doc): doc.builders.Doc; | ||
export default function withParensIfNeeded(node: Node, opts: ParserOptions, doc: Doc): doc.builders.Doc; |
{ | ||
"name": "prettier-plugin-marko", | ||
"description": "A prettier plugin for parsing and printing Marko files", | ||
"version": "1.4.7", | ||
"version": "1.4.8", | ||
"author": "Dylan Piercey <dpiercey@ebay.com>", | ||
@@ -6,0 +6,0 @@ "bugs": "https://github.com/marko-js/prettier/issues", |
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
73799
2040