postcss-styl
Advanced tools
| "use strict" | ||
| /** | ||
| * Find last match element | ||
| * @param {Array|string} arr array | ||
| * @param {function} callback callback | ||
| * @returns {*} element | ||
| */ | ||
| function findLast(arr, callback) { | ||
| const index = findLastIndex(arr, callback) | ||
| if (index > -1) { | ||
| return arr[index] | ||
| } | ||
| return undefined | ||
| } | ||
| /** | ||
| * Find match index | ||
| * @param {Array|string} arr array | ||
| * @param {function} callback callback | ||
| * @param {number} initIndex find start index | ||
| * @returns {number} index | ||
| */ | ||
| function findIndex(arr, callback, initIndex = 0) { | ||
| for (let index = initIndex; index < arr.length; index++) { | ||
| const element = arr[index] | ||
| if (callback(element)) { | ||
| return index | ||
| } | ||
| } | ||
| return -1 | ||
| } | ||
| /** | ||
| * Find last match index | ||
| * @param {Array|string} arr array | ||
| * @param {function} callback callback | ||
| * @param {number} initIndex find start index | ||
| * @returns {number} last index | ||
| */ | ||
| function findLastIndex(arr, callback, initIndex = arr.length - 1) { | ||
| for (let index = initIndex; index >= 0; index--) { | ||
| const element = arr[index] | ||
| if (callback(element)) { | ||
| return index | ||
| } | ||
| } | ||
| return -1 | ||
| } | ||
| module.exports = { | ||
| findLast, | ||
| findIndex, | ||
| findLastIndex, | ||
| } |
| "use strict" | ||
| const { tokensToRaws, isSkipToken } = require("./token-utils") | ||
| const { findLastIndex, findIndex } = require("./util") | ||
| module.exports = (sourceCode, end, opt = {}) => { | ||
| const options = Object.assign({ blockCommentIsRaw: true }, opt) | ||
| const options = Object.assign({ blockCommentAsRaw: true }, opt) | ||
| const cursor = sourceCode.createBackwardTokenCursor(end) | ||
@@ -18,3 +19,3 @@ | ||
| const afterTokens = | ||
| let afterTokens = | ||
| token && | ||
@@ -26,2 +27,26 @@ token.value !== ";" && | ||
| : after | ||
| if (options.bodyStartIndex != null || options.maxIndent != null) { | ||
| // Process for | ||
| // | .a | ||
| // | // comment | ||
| // | // comment | ||
| let maxIndent = options.maxIndent | ||
| if (maxIndent == null) { | ||
| const firstTokenIndex = findIndex( | ||
| sourceCode.text, | ||
| (c) => c.trim(), | ||
| options.bodyStartIndex, | ||
| ) | ||
| if (firstTokenIndex >= 0) { | ||
| // Comments beyond the indentation of the first token are child node comments. | ||
| maxIndent = sourceCode.getIndentFromIndex(firstTokenIndex) | ||
| } | ||
| } | ||
| if (maxIndent != null) { | ||
| afterTokens = processComment(sourceCode, afterTokens, maxIndent) | ||
| } | ||
| } | ||
| startIndex = afterTokens.length ? afterTokens[0].range[0] : startIndex | ||
@@ -56,3 +81,3 @@ | ||
| /** | ||
| * Chechs if raw target token | ||
| * Checks if raw target token | ||
| * @param {*} token token | ||
@@ -63,3 +88,3 @@ * @param {*} options options | ||
| if (isSkipToken(token)) { | ||
| if (options.blockCommentIsRaw) { | ||
| if (options.blockCommentAsRaw) { | ||
| return true | ||
@@ -73,2 +98,20 @@ } | ||
| /** | ||
| * Process for comments | ||
| * Comments beyond the indentation of the first token are child node comments. | ||
| */ | ||
| function processComment(sourceCode, tokens, maxIndent) { | ||
| const nonTargetCommentIndex = findLastIndex(tokens, (token) => { | ||
| if (token.type !== "inline-comment" && token.type !== "comment") { | ||
| return false | ||
| } | ||
| const tokenIndent = sourceCode.getIndentFromIndex(token.range[0]) | ||
| return maxIndent < tokenIndent | ||
| }) | ||
| if (nonTargetCommentIndex >= 0) { | ||
| return tokens.slice(nonTargetCommentIndex + 1) | ||
| } | ||
| return tokens | ||
| } | ||
| /** | ||
| * Get the first-line comments | ||
@@ -75,0 +118,0 @@ */ |
@@ -10,2 +10,3 @@ "use strict" | ||
| const { getName } = require("./stylus-nodes") | ||
| const { findLastIndex } = require("./util") | ||
@@ -129,2 +130,19 @@ /** | ||
| /** | ||
| * Calc indent for given index | ||
| * @param {string} text text | ||
| * @param {number} index find start index | ||
| * @returns {number} indent | ||
| */ | ||
| function calcIndent(text, index) { | ||
| const match = findLastIndex(text, (c) => c === "\n", index) | ||
| const lineStartIndex = match > -1 ? match + 1 : 0 | ||
| for (let i = lineStartIndex; i < text.length; i++) { | ||
| if (text[i].trim()) { | ||
| return i - lineStartIndex | ||
| } | ||
| } | ||
| return text.length - lineStartIndex | ||
| } | ||
| class LocationMap { | ||
@@ -354,2 +372,6 @@ constructor() { | ||
| getIndentFromIndex(index) { | ||
| return calcIndent(this.text, index) | ||
| } | ||
| _storeHackLocations(original, hacked) { | ||
@@ -356,0 +378,0 @@ const locationMap = (this.locationMap = new LocationMap()) |
@@ -69,3 +69,3 @@ "use strict" | ||
| /** | ||
| * Chechs if skip target token | ||
| * Checks if skip target token | ||
| * @param {*} token token | ||
@@ -81,3 +81,3 @@ */ | ||
| /** | ||
| * Chechs if whitespace token | ||
| * Checks if whitespace token | ||
| * @param {*} token token | ||
@@ -90,3 +90,3 @@ */ | ||
| /** | ||
| * Chechs if end of line token | ||
| * Checks if end of line token | ||
| * @param {*} token token | ||
@@ -93,0 +93,0 @@ */ |
+92
-21
@@ -23,6 +23,3 @@ "use strict" | ||
| } | ||
| if ( | ||
| !node.nodes || | ||
| !node.nodes.some((n) => n.type !== "comment" || !n.raws.inline) | ||
| ) { | ||
| if (!node.nodes || !node.nodes.length) { | ||
| // empty | ||
@@ -111,5 +108,19 @@ return false | ||
| function isNeedAdjustIndent(node) { | ||
| if (node.postfix || node.type === "comment") { | ||
| if (node.postfix) { | ||
| return false | ||
| } | ||
| if (node.type === "comment") { | ||
| const { parent } = node | ||
| if (!parent || !parent.nodes) { | ||
| return true | ||
| } | ||
| if ( | ||
| isPythonic(parent) && | ||
| parent.nodes.every((sibling) => sibling.type === "comment") | ||
| ) { | ||
| // All comments | ||
| return parent.nodes[parent.nodes.length - 1] === node // The last comment requires indentation. | ||
| } | ||
| return false | ||
| } | ||
| let { parent } = node | ||
@@ -141,3 +152,3 @@ if (parent && parent.postfix) { | ||
| */ | ||
| function adjustIndent(indent, targetBefore) { | ||
| function adjustIndentBefore(indent, targetBefore) { | ||
| const targetBeforeLines = toLines(targetBefore) | ||
@@ -155,3 +166,3 @@ | ||
| /** | ||
| * Remove commnets on selector | ||
| * Remove comments on selector | ||
| * @param {string} selector | ||
@@ -176,4 +187,4 @@ * @returns {string} removed comments | ||
| .map((line, index) => { | ||
| const trimed = line.trim() | ||
| if (!trimed) { | ||
| const trimmed = line.trim() | ||
| if (!trimmed) { | ||
| // ignore blank line | ||
@@ -185,3 +196,3 @@ return line | ||
| // If it is not pythonic, adjust the indentation other than comma delimiter. | ||
| if (/^,/u.test(trimed)) { | ||
| if (/^,/u.test(trimmed)) { | ||
| return line | ||
@@ -197,6 +208,38 @@ } | ||
| } | ||
| if (index === 0) { | ||
| // first line | ||
| return line.replace(/^\s*/u, "") | ||
| } | ||
| return line.replace(/^\s*/u, indent) | ||
| }) | ||
| .join("") | ||
| } | ||
| return index === 0 | ||
| ? line.replace(/^\s*/u, "") | ||
| : line.replace(/^\s*/u, indent) | ||
| /** | ||
| * Adjust params indent | ||
| * @param {node} _node | ||
| * @param {string} selectors | ||
| * @param {string} indent | ||
| * @returns {string} indented selectors | ||
| */ | ||
| function adjustParamsIndent(_node, selectors, indent) { | ||
| const lines = toLines(selectors) | ||
| return lines | ||
| .map((line, index) => { | ||
| const trimmed = line.trim() | ||
| if (!trimmed) { | ||
| // ignore blank line | ||
| return line | ||
| } | ||
| if (index === 0) { | ||
| // first line | ||
| return line.replace(/^\s*/u, "") | ||
| } | ||
| if (index + 1 < lines.length && line.startsWith(indent)) { | ||
| // not last line and have the required indentation. | ||
| return line | ||
| } | ||
| return line.replace(/^\s*/u, indent) | ||
| }) | ||
@@ -209,6 +252,11 @@ .join("") | ||
| const value = this.rawValuePlain(node, prop) | ||
| if (prop === "selector" && /\r\n|\r|\n/u.test(value)) { | ||
| if ( | ||
| (prop === "selector" || prop === "params") && | ||
| /\r\n|\r|\n/u.test(value) | ||
| ) { | ||
| const indent = this.getIndent(node) | ||
| return adjustSelectorsIndent(node, value, indent) | ||
| return prop === "selector" | ||
| ? adjustSelectorsIndent(node, value, indent) | ||
| : adjustParamsIndent(node, value, indent) | ||
| } | ||
@@ -223,3 +271,3 @@ return value | ||
| if (isNeedAdjustIndent(node)) { | ||
| return adjustIndent(this.getIndent(node), raw) | ||
| return adjustIndentBefore(this.getIndent(node), raw) | ||
| } | ||
@@ -232,6 +280,28 @@ } | ||
| block(node, start) { | ||
| if (!isPythonic(node) && !node.postfix) { | ||
| super.block(node, start) | ||
| if (isPythonic(node) || node.postfix) { | ||
| this._pythonicBlock(node, start) | ||
| return | ||
| } | ||
| if ( | ||
| node.nodes && | ||
| node.last && | ||
| node.last.type === "comment" && | ||
| node.last.raws.inline | ||
| ) { | ||
| let after = this.raw(node, "after") | ||
| if (!after.includes("\n")) { | ||
| after += `\n${this.getIndent(node)}` | ||
| let between = this.raw(node, "between", "beforeOpen") | ||
| this.builder(`${start + between}{`, node, "start") | ||
| this.body(node) | ||
| this.builder(after) | ||
| this.builder("}", node, "end") | ||
| return | ||
| } | ||
| } | ||
| super.block(node, start) | ||
| } | ||
| _pythonicBlock(node, start) { | ||
| const between = !node.postfix | ||
@@ -409,5 +479,6 @@ ? this.raw(node, "between", "beforeOpen") | ||
| if (isNeedAdjustIndent(node)) { | ||
| const firstSibling = parentTarget.nodes.find( | ||
| (n) => n.type !== "comment", | ||
| ) | ||
| const firstSibling = | ||
| parentTarget.nodes.find((n) => n.type !== "comment") || | ||
| // all comments | ||
| parentTarget.nodes[0] | ||
| if (firstSibling !== childTarget) { | ||
@@ -414,0 +485,0 @@ return this.getIndent(firstSibling) |
+1
-1
| { | ||
| "name": "postcss-styl", | ||
| "version": "0.10.0", | ||
| "version": "0.11.0", | ||
| "description": "PostCSS parser plugin for converting Stylus syntax to PostCSS AST.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
+1
-1
@@ -26,3 +26,3 @@ # postcss-styl | ||
| You can use this [PostCSS] plugin to apply [Stylus] syntax to [stylelint]. | ||
| **You can use it more easily by using it with [stylelint-plugin-stylus](https://github.com/stylus/stylelint-stylus).** | ||
| **You can use it more easily by using it with [stylelint-plugin-stylus](https://github.com/stylus/stylelint-plugin-stylus).** | ||
@@ -29,0 +29,0 @@ For example, this [PostCSS] plugin is used as follows: |
Sorry, the diff of this file is too big to display
146038
4.95%29
3.57%4407
4.88%