prettier-plugin-stylus
Advanced tools
Comparing version 0.0.1-alpha.3 to 0.0.1-beta.1
@@ -17,5 +17,4 @@ var __create = Object.create; | ||
var import_parser = __toESM(require("stylus/lib/parser.js")); | ||
var import_lexer = __toESM(require("stylus/lib/lexer.js")); | ||
var import_token = __toESM(require("stylus/lib/token.js")); | ||
var import_comment = __toESM(require("stylus/lib/nodes/comment.js")); | ||
var assert = __toESM(require("assert/strict")); | ||
var prettier = __toESM(require("prettier")); | ||
@@ -35,3 +34,16 @@ var import_utils = require("./utils"); | ||
parse: (text) => { | ||
const result = new import_parser.default(text, { cache: false }).parse(); | ||
const parser = new import_parser.default(text, { cache: false }); | ||
const originalComment = parser.lexer.comment.bind(parser.lexer); | ||
parser.lexer.comment = function() { | ||
if (this.str[0] == "/" && this.str[1] == "/") { | ||
let end = this.str.indexOf("\n"); | ||
if (end == -1) | ||
end = this.str.length; | ||
const str = this.str.substr(0, end); | ||
this.skip(end); | ||
return new import_token.default("comment", new import_comment.default(str, false, true)); | ||
} | ||
return originalComment(); | ||
}; | ||
const result = parser.parse(); | ||
result.text = text; | ||
@@ -55,7 +67,7 @@ return result; | ||
} | ||
const printStylus = (path, options, print2) => { | ||
const printStylus = (path, options, print) => { | ||
var _a; | ||
const node = path.getValue(); | ||
const children = (_, prop) => path.map(print2, prop); | ||
const child = (_, prop) => path.call(print2, prop); | ||
const children = (_, prop) => path.map(print, prop); | ||
const child = (_, prop) => path.call(print, prop); | ||
switch (node.nodeName) { | ||
@@ -160,61 +172,5 @@ case "root": | ||
}; | ||
const toSequence = (path) => { | ||
assert.equal(path.stack.length, 1); | ||
let seq2 = []; | ||
const toRemove = {}; | ||
const recurse = (path2) => { | ||
const node = path2.getValue(); | ||
const i = seq2.length; | ||
seq2.push(node); | ||
printStylus(path2, null, recurse); | ||
if (seq2[seq2.length - 1] !== node) { | ||
toRemove[i] = true; | ||
} | ||
}; | ||
recurse(path); | ||
seq2 = seq2.filter((_, i) => !toRemove[i]); | ||
const comments = []; | ||
const lexer = new import_lexer.default(path.stack[0].text); | ||
const originalSkip = lexer.skip.bind(lexer); | ||
lexer.skip = (len) => { | ||
if (lexer.str.slice(0, 2) === "//") { | ||
const comment = new import_comment.default(lexer.str.slice(0, len), false, true); | ||
comment.lineno = lexer.lineno; | ||
comment.column = lexer.column; | ||
comments.push(comment); | ||
} | ||
originalSkip(len); | ||
}; | ||
let token; | ||
while ((token = lexer.advance()) && token.type !== "eos") | ||
; | ||
for (const comment of comments) { | ||
let insertIndex = seq2.findIndex((node) => node.lineno > comment.lineno || node.lineno == comment.lineno && node.column >= comment.column); | ||
if (insertIndex === -1) { | ||
insertIndex = seq2.length; | ||
} | ||
seq2.splice(insertIndex, 0, comment); | ||
} | ||
return seq2; | ||
}; | ||
const seq = []; | ||
const print = (path, options, prettierPrint) => { | ||
if (path.stack.length === 1) { | ||
seq.length = 0; | ||
seq.push(...toSequence(path)); | ||
} | ||
const node = path.getValue(); | ||
const index = seq.indexOf(node); | ||
if (seq[index + 1] instanceof import_comment.default && seq[index + 1].inline) { | ||
return [ | ||
printStylus(path, options, prettierPrint), | ||
" ", | ||
seq[index + 1].str | ||
]; | ||
} | ||
return printStylus(path, options, prettierPrint); | ||
}; | ||
const printers = { | ||
[AST_FORMAT]: { | ||
print: printStylus | ||
} | ||
@@ -221,0 +177,0 @@ }; |
@@ -23,3 +23,2 @@ var __create = Object.create; | ||
__export(utils_exports, { | ||
getCommentSequence: () => getCommentSequence, | ||
isAstRoot: () => isAstRoot, | ||
@@ -31,3 +30,2 @@ isInlineComment: () => isInlineComment, | ||
module.exports = __toCommonJS(utils_exports); | ||
var import_prettier = require("prettier"); | ||
var import_comment = __toESM(require("stylus/lib/nodes/comment.js")); | ||
@@ -46,12 +44,2 @@ function isInlineComment(node) { | ||
} | ||
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.3", | ||
"version": "0.0.1-beta.1", | ||
"description": "Prettier plugin for Stylus", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
9069
221