@huggingface/prettier-plugin-vertical-align
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -1,3 +0,3 @@ | ||
import tsParsers from "prettier/parser-typescript"; | ||
import babelParsers from "prettier/plugins/babel"; | ||
import tsParsers from "prettier/parser-typescript.js"; | ||
import babelParsers from "prettier/parser-babel.js"; | ||
import { printer } from "./printer.js"; | ||
@@ -4,0 +4,0 @@ import { setOriginalPrinter } from "./original-printer.js"; |
@@ -1,5 +0,5 @@ | ||
// import { inspect } from "node:util"; | ||
import { doc } from "prettier"; | ||
import prettier from "prettier"; | ||
const { doc } = prettier; | ||
import { getOriginalPrinter } from "./original-printer.js"; | ||
const { group, indent, join, line, softline, breakParent, cursor, hardline, hardlineWithoutBreakParent, lineSuffixBoundary, literalline, literallineWithoutBreakParent, trim, addAlignmentToDoc, align, conditionalGroup, dedent, dedentToRoot, fill, ifBreak, indentIfBreak, label, lineSuffix, markAsRoot, } = doc.builders; | ||
const { group } = doc.builders; | ||
const keyLengthSymbol = Symbol("keyLength"); | ||
@@ -11,3 +11,3 @@ const typeAnnotationPrefix = Symbol("typeAnnotation"); | ||
const node = path.node; | ||
if (node.type === "Program") { | ||
if (node.type === "TSPropertySignature") { | ||
// console.log("node", inspect(node, {depth: 10})); | ||
@@ -17,3 +17,3 @@ } | ||
const keyLength = node[keyLengthSymbol]; | ||
const addedLength = keyLength - (node.key.loc.end.column - node.key.loc.start.column); | ||
const addedLength = keyLength - (node.key.loc.end.column - node.key.loc.start.column) - (node.optional ? 1 : 0); | ||
// console.log("keyLength", keyLength); | ||
@@ -29,6 +29,3 @@ switch (node.type) { | ||
node.typeAnnotation[typeAnnotationPrefix] = addedLength; | ||
return group([ | ||
path.call(_print, "key"), | ||
path.call(_print, "typeAnnotation"), | ||
]); | ||
return getOriginalPrinter().print(path, options, _print, ...args); | ||
default: | ||
@@ -40,15 +37,14 @@ throw new Error(`Unexpected node type: ${node.type}`); | ||
const addedLength = node[typeAnnotationPrefix]; | ||
return group([ | ||
": " + " ".repeat(addedLength), | ||
path.call(_print, "typeAnnotation"), | ||
]); | ||
return group([": " + " ".repeat(addedLength), path.call(_print, "typeAnnotation")]); | ||
} | ||
if (isPropertyContainer(node)) { | ||
// console.log("node", inspect(node, {depth: 10})); | ||
const properties = nodeProperties(node).filter(isProperty).filter((node) => node.key.loc.start.line === node.key.loc.end.line); | ||
const properties = nodeProperties(node) | ||
.filter(isProperty) | ||
.filter((node) => node.key.loc.start.line === node.key.loc.end.line && !node.shorthand); | ||
// Check props are not on the same line (we don't want to add extra spaces in that case) | ||
if (properties[1].loc.start.line !== properties[0].loc.start.line) { | ||
if (properties.length > 1 && properties[1].loc.start.line !== properties[0].loc.start.line) { | ||
let keyLength = 0; | ||
for (const property of properties) { | ||
keyLength = Math.max(keyLength, property.key.loc.end.column - property.key.loc.start.column); | ||
keyLength = Math.max(keyLength, property.key.loc.end.column - property.key.loc.start.column + (property.optional ? 1 : 0)); | ||
} | ||
@@ -55,0 +51,0 @@ for (const property of properties) { |
{ | ||
"name": "@huggingface/prettier-plugin-vertical-align", | ||
"version": "0.0.1", | ||
"description": "", | ||
"type": "module", | ||
"main": "dist/index.js", | ||
"types": "src/index.ts", | ||
"files": [ | ||
"src", | ||
"dist", | ||
"tsconfig.json" | ||
], | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"devDependencies": { | ||
"prettier": "^3.3.3", | ||
"typescript": "^5.6.2", | ||
"@types/node": "^22.0.0" | ||
}, | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"build": "tsc -b" | ||
} | ||
} | ||
"name": "@huggingface/prettier-plugin-vertical-align", | ||
"packageManager": "pnpm@9.11.0", | ||
"version": "0.0.2", | ||
"description": "", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"build": "tsc -b" | ||
}, | ||
"type": "module", | ||
"main": "dist/index.js", | ||
"types": "src/index.ts", | ||
"files": [ | ||
"src", | ||
"dist", | ||
"tsconfig.json" | ||
], | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"devDependencies": { | ||
"prettier": "^3.3.3", | ||
"typescript": "^5.6.2", | ||
"@types/node": "^22.0.0" | ||
} | ||
} |
import type { Parser, Printer } from "prettier"; | ||
import tsParsers from "prettier/parser-typescript"; | ||
import babelParsers from "prettier/plugins/babel"; | ||
import tsParsers from "prettier/parser-typescript.js"; | ||
import babelParsers from "prettier/parser-babel.js"; | ||
import { printer } from "./printer.js"; | ||
@@ -5,0 +5,0 @@ import { setOriginalPrinter } from "./original-printer.js"; |
import type { AstPath, Printer } from "prettier"; | ||
// import { inspect } from "node:util"; | ||
import { doc } from "prettier"; | ||
import prettier from "prettier"; | ||
import { inspect } from "node:util"; | ||
const { doc } = prettier; | ||
import { getOriginalPrinter } from "./original-printer.js"; | ||
const { | ||
group, | ||
indent, | ||
join, | ||
line, | ||
softline, | ||
breakParent, | ||
cursor, | ||
hardline, | ||
hardlineWithoutBreakParent, | ||
lineSuffixBoundary, | ||
literalline, | ||
literallineWithoutBreakParent, | ||
trim, | ||
addAlignmentToDoc, | ||
align, | ||
conditionalGroup, | ||
dedent, | ||
dedentToRoot, | ||
fill, | ||
ifBreak, | ||
indentIfBreak, | ||
label, | ||
lineSuffix, | ||
markAsRoot, | ||
} = doc.builders; | ||
const { group } = doc.builders; | ||
type Node = AstPath["node"] | ||
type Node = AstPath["node"]; | ||
const keyLengthSymbol = Symbol("keyLength"); | ||
@@ -38,3 +14,3 @@ const typeAnnotationPrefix = Symbol("typeAnnotation"); | ||
export const printer: Printer = { | ||
print(path, options, _print,...args) { | ||
print(path, options, _print, ...args) { | ||
// const originalPrinter = options.printer as Printer; | ||
@@ -44,10 +20,10 @@ | ||
if (node.type === "Program") { | ||
if (node.type === "TSPropertySignature") { | ||
// console.log("node", inspect(node, {depth: 10})); | ||
} | ||
if (node[keyLengthSymbol]) { | ||
if (node[keyLengthSymbol]) { | ||
const keyLength = node[keyLengthSymbol]; | ||
const addedLength = keyLength - (node.key.loc.end.column - node.key.loc.start.column); | ||
const addedLength = keyLength - (node.key.loc.end.column - node.key.loc.start.column) - (node.optional ? 1 : 0); | ||
// console.log("keyLength", keyLength); | ||
@@ -61,11 +37,8 @@ | ||
path.call(_print, valueField(node)), | ||
]) | ||
]); | ||
case "TSPropertySignature": | ||
node.typeAnnotation[typeAnnotationPrefix] = addedLength; | ||
return group([ | ||
path.call(_print, "key"), | ||
path.call(_print, "typeAnnotation"), | ||
]); | ||
return getOriginalPrinter().print(path, options, _print, ...args); | ||
default: | ||
throw new Error(`Unexpected node type: ${node.type}`); | ||
throw new Error(`Unexpected node type: ${node.type}`); | ||
} | ||
@@ -76,6 +49,3 @@ } | ||
const addedLength = node[typeAnnotationPrefix]; | ||
return group([ | ||
": " + " ".repeat(addedLength), | ||
path.call(_print, "typeAnnotation"), | ||
]); | ||
return group([": " + " ".repeat(addedLength), path.call(_print, "typeAnnotation")]); | ||
} | ||
@@ -85,9 +55,14 @@ | ||
// console.log("node", inspect(node, {depth: 10})); | ||
const properties: Node[] = nodeProperties(node).filter(isProperty).filter((node: Node) => node.key.loc.start.line === node.key.loc.end.line); | ||
const properties: Node[] = nodeProperties(node) | ||
.filter(isProperty) | ||
.filter((node: Node) => node.key.loc.start.line === node.key.loc.end.line && !node.shorthand); | ||
// Check props are not on the same line (we don't want to add extra spaces in that case) | ||
if (properties[1].loc.start.line !== properties[0].loc.start.line) { | ||
if (properties.length > 1 && properties[1].loc.start.line !== properties[0].loc.start.line) { | ||
let keyLength = 0; | ||
for (const property of properties) { | ||
keyLength = Math.max(keyLength, property.key.loc.end.column - property.key.loc.start.column); | ||
keyLength = Math.max( | ||
keyLength, | ||
property.key.loc.end.column - property.key.loc.start.column + (property.optional ? 1 : 0), | ||
); | ||
} | ||
@@ -131,2 +106,2 @@ | ||
throw new Error(`Unexpected node type: ${node.type}`); | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
14451
249