prettier-plugin-stylus
Advanced tools
Comparing version 0.0.1-alpha.2 to 0.0.1-alpha.3
@@ -21,2 +21,3 @@ var __create = Object.create; | ||
var prettier = __toESM(require("prettier")); | ||
var import_utils = require("./utils"); | ||
const b = prettier.doc.builders; | ||
@@ -54,3 +55,3 @@ const languages = [ | ||
const printStylus = (path, options, print2) => { | ||
var _a, _b; | ||
var _a; | ||
const node = path.getValue(); | ||
@@ -78,7 +79,10 @@ const children = (_, prop) => path.map(print2, prop); | ||
case "property": { | ||
return [children(node, "segments"), " ", child(node, "expr")]; | ||
const value = node.expr.nodes[0]; | ||
const sep = value.nodeName === "ident" && !(0, import_utils.isSingleIdent)(value) ? ": " : " "; | ||
return [children(node, "segments"), sep, child(node, "expr")]; | ||
} | ||
case "expression": { | ||
const content = b.join(" ", children(node, "nodes")); | ||
if (((_a = path.getParentNode()) == null ? void 0 : _a.nodeName) === "selector") { | ||
const parent = path.getParentNode(); | ||
if ((parent == null ? void 0 : parent.nodeName) === "selector" || (parent == null ? void 0 : parent.nodeName) === "keyframes") { | ||
return ["{", content, "}"]; | ||
@@ -91,2 +95,5 @@ } | ||
} | ||
case "unaryop": { | ||
return [node.op, "(", child(node, "expr"), ")"]; | ||
} | ||
case "each": { | ||
@@ -113,12 +120,11 @@ return [ | ||
case "unit": | ||
return `${node.val}${(_b = node.type) != null ? _b : ""}`; | ||
return `${node.val}${(_a = node.type) != null ? _a : ""}`; | ||
case "ident": | ||
if (node.val.isNull) { | ||
if ((0, import_utils.isSingleIdent)(node)) { | ||
return node.string; | ||
} else { | ||
const parent = path.getParentNode(); | ||
if ((parent == null ? void 0 : parent.nodeName) === "params") { | ||
return [node.name, " = ", child(node, "val")]; | ||
if (node.val.nodeName === "function") { | ||
return child(node, "val"); | ||
} | ||
return child(node, "val"); | ||
return [node.name, " = ", child(node, "val")]; | ||
} | ||
@@ -131,2 +137,22 @@ case "literal": | ||
return node.str; | ||
case "rgba": | ||
return node.raw; | ||
case "keyframes": | ||
return [ | ||
"@keyframes ", | ||
children(node, "segments"), | ||
child(node, "block") | ||
]; | ||
case "media": | ||
return ["@media ", child(node, "val"), child(node, "block")]; | ||
case "querylist": | ||
return b.join(", ", children(node, "nodes")); | ||
case "query": | ||
return [ | ||
node.resolvedPredicate ? node.resolvedPredicate + " " : "", | ||
node.resolvedType ? node.resolvedType + " and " : "", | ||
b.join(" and ", children(node, "nodes")) | ||
]; | ||
case "feature": | ||
return ["(", children(node, "segments"), ": ", child(node, "expr"), ")"]; | ||
default: | ||
@@ -152,3 +178,2 @@ console.error(node); | ||
seq2 = seq2.filter((_, i) => !toRemove[i]); | ||
console.log("seq", seq2); | ||
const comments = []; | ||
@@ -174,6 +199,4 @@ const lexer = new import_lexer.default(path.stack[0].text); | ||
} | ||
console.log(insertIndex); | ||
seq2.splice(insertIndex, 0, comment); | ||
} | ||
console.log(seq2); | ||
return seq2; | ||
@@ -189,5 +212,2 @@ }; | ||
const index = seq.indexOf(node); | ||
if (node instanceof import_comment.default) { | ||
console.log("wfasjld", node, index, seq[index + 1]); | ||
} | ||
if (seq[index + 1] instanceof import_comment.default && seq[index + 1].inline) { | ||
@@ -194,0 +214,0 @@ return [ |
@@ -0,5 +1,11 @@ | ||
var __create = Object.create; | ||
var __defProp = Object.defineProperty; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __getProtoOf = Object.getPrototypeOf; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __export = (target, all) => { | ||
for (var name in all) | ||
__defProp(target, name, { get: all[name], enumerable: true }); | ||
}; | ||
var __copyProps = (to, from, except, desc) => { | ||
@@ -13,5 +19,37 @@ if (from && typeof from === "object" || typeof from === "function") { | ||
}; | ||
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod)); | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
var utils_exports = {}; | ||
__export(utils_exports, { | ||
getCommentSequence: () => getCommentSequence, | ||
isAstRoot: () => isAstRoot, | ||
isInlineComment: () => isInlineComment, | ||
isNull: () => isNull, | ||
isSingleIdent: () => isSingleIdent | ||
}); | ||
module.exports = __toCommonJS(utils_exports); | ||
var import_prettier = require("prettier"); | ||
var import_comment = __toESM(require("stylus/lib/nodes/comment.js")); | ||
function isInlineComment(node) { | ||
return node instanceof import_comment.default && node.inline; | ||
} | ||
function isNull(node) { | ||
return node && typeof node === "object" && node.isNull; | ||
} | ||
function isSingleIdent(node) { | ||
return isNull(node.val); | ||
} | ||
function isAstRoot(path) { | ||
return path.stack.length === 1; | ||
} | ||
function getCommentSequence(seq, start = 0) { | ||
let end = start; | ||
while (isInlineComment(seq[end])) { | ||
end++; | ||
} | ||
const comments = seq.slice(start, end).map((c) => [c.str, import_prettier.doc.builders.hardline]); | ||
if (comments.length > 0) { | ||
return comments; | ||
} | ||
} | ||
//# sourceMappingURL=utils.js.map |
{ | ||
"name": "prettier-plugin-stylus", | ||
"version": "0.0.1-alpha.2", | ||
"version": "0.0.1-alpha.3", | ||
"description": "Prettier plugin for Stylus", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -5,7 +5,31 @@ # prettier-plugin-stylus | ||
 | ||
## Usage | ||
```sh | ||
npm i prettier-plugin-stylus | ||
``` | ||
Then add it to your prettierrc file, e.g.: | ||
```json | ||
{ | ||
"plugins": ["prettier-plugin-stylus"], | ||
"trailingComma": "all" | ||
} | ||
``` | ||
Or you can specify it in command line: | ||
```sh | ||
npx prettier --plugin=prettier-plugin-stylus some.styl | ||
``` | ||
## TODO | ||
- [x] support comment | ||
- [x] support keyframes & media query | ||
- [ ] add plugin recommendation in prettier docs | ||
- [ ] Vue SFC (Waiting for upstream, see https://github.com/prettier/prettier/pull/12707) | ||
- [ ] playground |
10640
277
35