@huggingface/prettier-plugin-vertical-align
Advanced tools
Comparing version 0.0.13 to 0.1.0
@@ -1,2 +0,2 @@ | ||
import type { Parser } from "prettier"; | ||
import type { Parser, SupportOptions } from "prettier"; | ||
export declare const parsers: { | ||
@@ -7,1 +7,2 @@ typescript: Parser<any>; | ||
}; | ||
export declare const options: SupportOptions; |
@@ -10,2 +10,20 @@ import tsParsers from "prettier/parser-typescript.js"; | ||
}; | ||
export const options = { | ||
alignInGroups: { | ||
type: "choice", | ||
category: "Global", | ||
default: "never", | ||
choices: [ | ||
{ | ||
value: "never", | ||
description: "Align every property inside an object on the same column.", | ||
}, | ||
{ | ||
value: "always", | ||
description: "Create groups based on blank lines or multi-line values. Properties in separate groups will not share alignment.", | ||
}, | ||
], | ||
description: "Whether all properties in a group should align the same, or it's on a per-group basis.", | ||
}, | ||
}; | ||
// Do not export printers, as prettier does not allow composing printers. | ||
@@ -23,8 +41,8 @@ // Instead we wrap the original printer | ||
...options.printer, | ||
...printer | ||
...printer, | ||
}; | ||
return parser.preprocess?.(text, options) ?? text; | ||
} | ||
}, | ||
}; | ||
} | ||
//# sourceMappingURL=index.js.map |
@@ -23,5 +23,5 @@ import prettier from "prettier"; | ||
// } | ||
// if (node.type === "Program") { | ||
// console.log("node", inspect(node.body, { depth: 10 })); | ||
// } | ||
if (node.type === "Program") { | ||
// console.log("node", inspect(node.body, { depth: 10 })); | ||
} | ||
if (node[keyLengthSymbol]) { | ||
@@ -60,2 +60,4 @@ const keyLength = node[keyLengthSymbol]; | ||
if (isPropertyContainer(node)) { | ||
let groups = []; | ||
let prevLine = -Infinity; | ||
// console.log("node", node); | ||
@@ -66,6 +68,18 @@ // console.log("node", inspect(node, {depth: 10})); | ||
.filter((node) => node.key.loc.start.line === node.key.loc.end.line && !node.shorthand && !node.method); | ||
// Check props are not on the same line (we don't want to add extra spaces in that case) | ||
if (properties.length > 1 && properties[1].loc.start.line !== properties[0].loc.start.line) { | ||
for (const prop of properties) { | ||
const propStart = prop.comments ? prop.comments[0].loc.start.line : prop.loc.start.line; | ||
if (prevLine === propStart) { | ||
// Multiple properties on the same line | ||
return getOriginalPrinter().print(path, options, _print, ...args); | ||
} | ||
if (prevLine === -Infinity || | ||
(options.alignInGroups === "always" && prevLine !== propStart - 1)) { | ||
groups.push([]); | ||
} | ||
groups.at(-1).push(prop); | ||
prevLine = prop.key.loc.start.line; | ||
} | ||
for (const group of groups.filter((group) => group.length > 1)) { | ||
let keyLength = 0; | ||
for (const property of properties) { | ||
for (const property of group) { | ||
keyLength = Math.max(keyLength, property.key.loc.end.column - | ||
@@ -76,3 +90,3 @@ property.key.loc.start.column + | ||
} | ||
for (const property of properties) { | ||
for (const property of group) { | ||
property[keyLengthSymbol] = keyLength; | ||
@@ -79,0 +93,0 @@ } |
{ | ||
"name": "@huggingface/prettier-plugin-vertical-align", | ||
"packageManager": "pnpm@9.11.0", | ||
"version": "0.0.13", | ||
"version": "0.1.0", | ||
"description": "", | ||
@@ -6,0 +6,0 @@ "scripts": { |
@@ -1,2 +0,2 @@ | ||
import type { Parser, Printer } from "prettier"; | ||
import type { Parser, ParserOptions, Printer, SupportOption, SupportOptions } from "prettier"; | ||
import tsParsers from "prettier/parser-typescript.js"; | ||
@@ -13,2 +13,21 @@ import babelParsers from "prettier/parser-babel.js"; | ||
export const options: SupportOptions = { | ||
alignInGroups: { | ||
type: "choice", | ||
category: "Global", | ||
default: "never", | ||
choices: [ | ||
{ | ||
value: "never", | ||
description: "Align every property inside an object on the same column.", | ||
}, | ||
{ | ||
value: "always", | ||
description: | ||
"Create groups based on blank lines or multi-line values. Properties in separate groups will not share alignment.", | ||
}, | ||
], | ||
description: "Whether all properties in a group should align the same, or it's on a per-group basis.", | ||
}, | ||
}; | ||
// Do not export printers, as prettier does not allow composing printers. | ||
@@ -28,7 +47,7 @@ // Instead we wrap the original printer | ||
...(options.printer as Printer), | ||
...printer | ||
...printer, | ||
}; | ||
return parser.preprocess?.(text, options) ?? text; | ||
} | ||
}, | ||
}; | ||
} | ||
} |
@@ -33,5 +33,5 @@ import type { AstPath, Printer } from "prettier"; | ||
// if (node.type === "Program") { | ||
// console.log("node", inspect(node.body, { depth: 10 })); | ||
// } | ||
if (node.type === "Program") { | ||
// console.log("node", inspect(node.body, { depth: 10 })); | ||
} | ||
@@ -76,2 +76,5 @@ if (node[keyLengthSymbol]) { | ||
if (isPropertyContainer(node)) { | ||
let groups: Node[][] = []; | ||
let prevLine = -Infinity; | ||
// console.log("node", node); | ||
@@ -83,6 +86,22 @@ // console.log("node", inspect(node, {depth: 10})); | ||
// Check props are not on the same line (we don't want to add extra spaces in that case) | ||
if (properties.length > 1 && properties[1].loc.start.line !== properties[0].loc.start.line) { | ||
for (const prop of properties) { | ||
const propStart = prop.comments ? prop.comments[0].loc.start.line : prop.loc.start.line; | ||
if (prevLine === propStart) { | ||
// Multiple properties on the same line | ||
return getOriginalPrinter().print(path, options, _print, ...args); | ||
} | ||
if ( | ||
prevLine === -Infinity || | ||
(options.alignInGroups === "always" && prevLine !== propStart - 1) | ||
) { | ||
groups.push([]); | ||
} | ||
groups.at(-1)!.push(prop); | ||
prevLine = prop.key.loc.start.line; | ||
} | ||
for (const group of groups.filter((group) => group.length > 1)) { | ||
let keyLength = 0; | ||
for (const property of properties) { | ||
for (const property of group) { | ||
keyLength = Math.max( | ||
@@ -97,3 +116,3 @@ keyLength, | ||
for (const property of properties) { | ||
for (const property of group) { | ||
property[keyLengthSymbol] = keyLength; | ||
@@ -100,0 +119,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
47310
943