postcss-styl
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -14,2 +14,4 @@ "use strict" | ||
const parseExpression = require("./parse-expression") | ||
const getSelectorEndIndex = require("./get-selector-end-index") | ||
const getCssLiteralIndices = require("./get-css-literal-indices") | ||
@@ -218,83 +220,2 @@ const debug = require("debug")("postcss-styl:parser") | ||
/** | ||
* Chechs if end of line token | ||
* @param {*} token token | ||
*/ | ||
function isEndOfLineToken(token) { | ||
return ( | ||
!token || token.type === "inline-comment" || token.type === "linebreak" | ||
) | ||
} | ||
/** | ||
* Chechs if skip target token | ||
* @param {*} token token | ||
*/ | ||
function isSkipToken(token) { | ||
return ( | ||
token && | ||
(token.type === "whitespace" || | ||
token.type === "comment" || | ||
token.type === "inline-comment" || | ||
token.type === "linebreak") | ||
) | ||
} | ||
/** | ||
* Get the end index of selector | ||
* @param {*} node | ||
* @param {*} sourceCode | ||
*/ | ||
function getSelectorEndIndex(node, block, sourceCode) { | ||
const blockBeforeIndex = block.nodes[0] | ||
? sourceCode.getIndex(block.nodes[0]) - 1 | ||
: sourceCode.getIndex(block) | ||
const cursor = sourceCode.createTokenCursor(sourceCode.getIndex(node), { | ||
endLocationIndex: blockBeforeIndex, | ||
scope: true, | ||
}) | ||
let token = cursor.next() | ||
if (!token) { | ||
return blockBeforeIndex | ||
} | ||
let endIndex = token.range[1] - 1 | ||
let comma = false | ||
while (token) { | ||
if (cursor.scopeLevel === 0) { | ||
if (!comma && isEndOfLineToken(token)) { | ||
// end | ||
if (token.type === "linebreak") { | ||
// between.push(token) | ||
return token.range[0] - 1 | ||
} | ||
return token.range[1] - 1 | ||
} | ||
if (token.value === "{") { | ||
endIndex = token.range[0] - 1 | ||
let end = true | ||
token = cursor.next() | ||
while (token) { | ||
if (isEndOfLineToken(token) || token.value === "}") { | ||
break | ||
} | ||
if (!isSkipToken(token)) { | ||
end = false | ||
break | ||
} | ||
token = cursor.next() | ||
} | ||
if (end) { | ||
// end | ||
return endIndex | ||
} | ||
} | ||
} | ||
comma = token.value === "," | ||
endIndex = token.range[1] - 1 | ||
token = cursor.next() | ||
} | ||
return endIndex | ||
} | ||
class StylusParser { | ||
@@ -495,3 +416,3 @@ constructor(input) { | ||
ternary(node, parent, info) { | ||
this.atruleImpl(node, {}, parent, info).ternary = true | ||
this.atruleImpl(node, {}, parent, info).expression = true | ||
} | ||
@@ -540,5 +461,4 @@ | ||
beforeBlockNodeEndIndex: getSelectorEndIndex( | ||
node, | ||
node.block, | ||
this.sourceCode | ||
this.sourceCode, | ||
node | ||
), | ||
@@ -584,2 +504,3 @@ blockNode: node.block, | ||
if (nodeName === "call") { | ||
// fn() | ||
this.atruleImpl(node, {}, parent, info).call = true | ||
@@ -589,7 +510,7 @@ return | ||
if (nodeName === "binop") { | ||
this.atruleImpl(node, {}, parent, info).binop = true | ||
this.atruleImpl(node, {}, parent, info).expression = true | ||
return | ||
} | ||
if (nodeName === "member") { | ||
this.atruleImpl(node, {}, parent, info).member = true | ||
this.atruleImpl(node, {}, parent, info).expression = true | ||
return | ||
@@ -634,3 +555,3 @@ } | ||
} else if (valNodeName === "binop") { | ||
this.atruleImpl(node, {}, parent, info).binop = true | ||
this.atruleImpl(node, {}, parent, info).expression = true | ||
} else { | ||
@@ -694,3 +615,3 @@ // TODO: Unknown ident val type | ||
info | ||
).binop = true | ||
).expression = true | ||
} | ||
@@ -716,8 +637,3 @@ | ||
} | ||
this.atruleImpl( | ||
node, | ||
{ blockNode: node.block, postfix }, | ||
parent, | ||
info | ||
).for = true | ||
this.atruleImpl(node, { blockNode: node.block, postfix }, parent, info) | ||
} | ||
@@ -738,3 +654,3 @@ | ||
new ProcessInfo(nodes, 0, info, parent) | ||
).if = true | ||
) | ||
@@ -747,3 +663,3 @@ node.elses.forEach((el, i) => { | ||
new ProcessInfo(nodes, i + 1, info, parent) | ||
).else = true | ||
) | ||
}) | ||
@@ -1110,11 +1026,9 @@ } | ||
const startIndex = this.sourceCode.getIndex(node) | ||
let cssStartIndex = startIndex | ||
for (const token of this.sourceCode.genTokens(startIndex)) { | ||
if (token.value === "{") { | ||
cssStartIndex = token.range[0] + 1 | ||
break | ||
} | ||
} | ||
const { start: cssOpenIndex, end: endIndex } = getCssLiteralIndices( | ||
this.sourceCode, | ||
node | ||
) | ||
const cssStartIndex = cssOpenIndex + 1 | ||
const cssStartLoc = this.sourceCode.getLoc(cssStartIndex) | ||
const endIndex = cssStartIndex + node.string.length + 1 | ||
const cssEndIndex = endIndex - 1 | ||
@@ -1538,3 +1452,6 @@ const parsedNameAndCondition = parseAtRuleNameAndCondition( | ||
const endIndex = this.getBlockEndIndex(bodyStartIndex, parent, info) | ||
const bodyText = this.sourceCode.getText(bodyStartIndex, endIndex) | ||
const bodyText = this.sourceCode.text.slice( | ||
bodyStartIndex, | ||
endIndex + 1 | ||
) | ||
@@ -1541,0 +1458,0 @@ let bodyEndIndex = endIndex |
"use strict" | ||
const { isSkipToken, isEndOfLineToken } = require("./token-utils") | ||
/** | ||
@@ -12,12 +14,12 @@ * Get location if given node is interpolation expression. | ||
const startIndex = sourceCode.getIndexFromStylusNode(node) | ||
cursor = sourceCode.createTokenCursor(startIndex) | ||
cursor = sourceCode.createBackwardTokenCursor(startIndex) | ||
} else { | ||
const first = node.nodes[0] | ||
const startIndex = sourceCode.getStartIndex(first) | ||
cursor = sourceCode.createTokenCursor(startIndex) | ||
cursor.prev() // skip first token | ||
cursor = sourceCode.createBackwardTokenCursor(startIndex) | ||
cursor.next() // skip first token | ||
} | ||
let token = null | ||
while ((token = cursor.prev())) { | ||
if (isSkipToken(token)) { | ||
while ((token = cursor.next())) { | ||
if (isSkipToken(token) && !isEndOfLineToken(token)) { | ||
continue | ||
@@ -38,7 +40,4 @@ } | ||
const expressionCursor = sourceCode.createTokenCursor( | ||
interpolationStartIndex, | ||
{ | ||
scope: true, | ||
} | ||
const expressionCursor = sourceCode.createScopeTokenCursor( | ||
interpolationStartIndex | ||
) | ||
@@ -61,10 +60,2 @@ // skip brace | ||
return null | ||
/** | ||
* Chechs if skip target token | ||
* @param {*} t token | ||
*/ | ||
function isSkipToken(t) { | ||
return t && (t.type === "whitespace" || t.type === "comment") | ||
} | ||
} | ||
@@ -179,3 +170,3 @@ | ||
: this.getStartIndex(sourceCode, base) | ||
const cursor = sourceCode.createTokenCursor(idx) | ||
const cursor = sourceCode.createBackwardTokenCursor(idx) | ||
const wordsStack = [...words] | ||
@@ -208,3 +199,3 @@ const lowersStack = [...lowers] | ||
let token = null | ||
while ((token = cursor.prev())) { | ||
while ((token = cursor.next())) { | ||
const { value } = token | ||
@@ -211,0 +202,0 @@ if (check(value)) { |
"use strict" | ||
const { tokensToRaw } = require("./tokens-to-raw") | ||
const { tokensToRaws, isEndOfLineToken, isSkipToken } = require("./token-utils") | ||
// eslint-disable-next-line complexity | ||
module.exports = (sourceCode, start, maxEnd) => { | ||
const cursor = sourceCode.createTokenCursor(start, { | ||
const cursor = sourceCode.createScopeTokenCursor(start, { | ||
endLocationIndex: maxEnd, | ||
scope: true, | ||
}) | ||
@@ -104,13 +103,13 @@ let token = cursor.next() | ||
const raw = tokensToRaw(params) | ||
const rawsBetween = tokensToRaw(between) | ||
const rawsAfterName = tokensToRaw(afterName) | ||
const raws = tokensToRaws(params) | ||
const rawsBetween = tokensToRaws(between) | ||
const rawsAfterName = tokensToRaws(afterName) | ||
return { | ||
name, | ||
params: raw.value, | ||
params: raws.value, | ||
raw: { | ||
afterName: rawsAfterName.raw, | ||
css: raw.raw, | ||
stylus: raw.stylus, | ||
css: raws.raw, | ||
stylus: raws.stylus, | ||
between: rawsBetween.raw, | ||
@@ -124,25 +123,1 @@ stylusBetween: rawsBetween.stylus, | ||
} | ||
/** | ||
* Chechs if skip target token | ||
* @param {*} token token | ||
*/ | ||
function isSkipToken(token) { | ||
return ( | ||
token && | ||
(token.type === "whitespace" || | ||
token.type === "comment" || | ||
token.type === "inline-comment" || | ||
token.type === "linebreak") | ||
) | ||
} | ||
/** | ||
* Chechs if end of line token | ||
* @param {*} token token | ||
*/ | ||
function isEndOfLineToken(token) { | ||
return ( | ||
!token || token.type === "inline-comment" || token.type === "linebreak" | ||
) | ||
} |
"use strict" | ||
const { tokensToRaw } = require("./tokens-to-raw") | ||
const { tokensToRaws } = require("./token-utils") | ||
@@ -9,5 +9,3 @@ /** | ||
module.exports = (sourceCode, start) => { | ||
const cursor = sourceCode.createTokenCursor(start, { | ||
scope: true, | ||
}) | ||
const cursor = sourceCode.createScopeTokenCursor(start) | ||
let token = cursor.next() | ||
@@ -44,14 +42,14 @@ if (token.value !== "{") { | ||
const expression = params.slice(1, -1) | ||
const raw = tokensToRaw(params) | ||
const rawExpression = tokensToRaw(expression) | ||
const raws = tokensToRaws(params) | ||
const rawsExpression = tokensToRaws(expression) | ||
return { | ||
params: raw.value, | ||
expression: rawExpression.value, | ||
params: raws.value, | ||
expression: rawsExpression.value, | ||
raw: { | ||
css: raw.raw, | ||
stylus: raw.stylus, | ||
css: raws.raw, | ||
stylus: raws.stylus, | ||
semicolon, | ||
expression: rawExpression.raw, | ||
stylusExpression: rawExpression.stylus, | ||
expression: rawsExpression.raw, | ||
stylusExpression: rawsExpression.stylus, | ||
}, | ||
@@ -58,0 +56,0 @@ endIndex, |
"use strict" | ||
const { tokensToRaw } = require("./tokens-to-raw") | ||
const { tokensToRaws } = require("./token-utils") | ||
const parseAtRuleNameAndCondition = require("./parse-atrule-name-and-condition") | ||
@@ -47,3 +47,3 @@ | ||
const raw = tokensToRaw(body) | ||
const raws = tokensToRaws(body) | ||
@@ -53,3 +53,3 @@ return { | ||
params, | ||
body: raw.value, | ||
body: raws.value, | ||
raw: { | ||
@@ -65,4 +65,4 @@ afterName, | ||
body: { | ||
css: raw.raw, | ||
stylus: raw.stylus, | ||
css: raws.raw, | ||
stylus: raws.stylus, | ||
}, | ||
@@ -69,0 +69,0 @@ }, |
"use strict" | ||
const { isWhitespaceToken } = require("./token-utils") | ||
/** | ||
@@ -40,9 +42,1 @@ * Extract own semicolon tokens | ||
} | ||
/** | ||
* Chechs if whitespace token | ||
* @param {*} token token | ||
*/ | ||
function isWhitespaceToken(token) { | ||
return token && (token.type === "whitespace" || token.type === "linebreak") | ||
} |
"use strict" | ||
const { isSkipToken } = require("./token-utils") | ||
/** | ||
@@ -9,3 +11,3 @@ * Extract prop name tokens | ||
function extractTokens(sourceCode, start) { | ||
const cursor = sourceCode.createTokenCursor(start, { scope: true }) | ||
const cursor = sourceCode.createScopeTokenCursor(start) | ||
let token = cursor.next() | ||
@@ -61,15 +63,1 @@ const tokens = [] | ||
} | ||
/** | ||
* Chechs if skip target token | ||
* @param {*} token token | ||
*/ | ||
function isSkipToken(token) { | ||
return ( | ||
token && | ||
(token.type === "comment" || | ||
token.type === "inline-comment" || | ||
token.type === "whitespace" || | ||
token.type === "linebreak") | ||
) | ||
} |
"use strict" | ||
const { tokensToRaw } = require("./tokens-to-raw") | ||
const { tokensToRaws, isSkipToken } = require("./token-utils") | ||
module.exports = (sourceCode, end, opt = {}) => { | ||
const options = Object.assign({ blockCommentIsRaw: true }, opt) | ||
const cursor = sourceCode.createTokenCursor(end) | ||
const cursor = sourceCode.createBackwardTokenCursor(end) | ||
const after = [] | ||
let token = cursor.prev() | ||
let token = cursor.next() | ||
let startIndex = token ? token.range[1] : end | ||
while (token && isRawToken(token, options)) { | ||
after.unshift(token) | ||
token = cursor.prev() | ||
token = cursor.next() | ||
} | ||
@@ -21,7 +21,7 @@ | ||
const raw = tokensToRaw(afterTokens) | ||
const raws = tokensToRaws(afterTokens) | ||
return { | ||
after: raw.raw, | ||
stylusAfter: raw.stylus, | ||
after: raws.raw, | ||
stylusAfter: raws.stylus, | ||
startIndex, | ||
@@ -37,9 +37,9 @@ } | ||
function isRawToken(token, options) { | ||
return ( | ||
token && | ||
(token.type === "whitespace" || | ||
token.type === "linebreak" || | ||
token.type === "inline-comment" || | ||
(options.blockCommentIsRaw && token.type === "comment")) | ||
) | ||
if (isSkipToken(token)) { | ||
if (options.blockCommentIsRaw) { | ||
return true | ||
} | ||
return token.type !== "comment" | ||
} | ||
return false | ||
} | ||
@@ -46,0 +46,0 @@ |
"use strict" | ||
const { tokensToRaw } = require("./tokens-to-raw") | ||
const { tokensToRaws, isSkipToken } = require("./token-utils") | ||
@@ -14,3 +14,3 @@ module.exports = (sourceCode, start, end) => { | ||
let endIndex = token ? token.range[0] - 1 : start | ||
while (token && isRawToken(token)) { | ||
while (token && isSkipToken(token)) { | ||
before.push(token) | ||
@@ -21,23 +21,9 @@ endIndex = token.range[1] - 1 | ||
const raw = tokensToRaw(before) | ||
const raws = tokensToRaws(before) | ||
return { | ||
before: raw.raw, | ||
stylusBefore: raw.stylus, | ||
before: raws.raw, | ||
stylusBefore: raws.stylus, | ||
endIndex, | ||
} | ||
} | ||
/** | ||
* Chechs if raw target token | ||
* @param {*} token token | ||
*/ | ||
function isRawToken(token) { | ||
return ( | ||
token && | ||
(token.type === "whitespace" || | ||
token.type === "comment" || | ||
token.type === "inline-comment" || | ||
token.type === "linebreak") | ||
) | ||
} |
"use strict" | ||
const { tokensToRaw } = require("./tokens-to-raw") | ||
const { tokensToRaws } = require("./token-utils") | ||
@@ -36,3 +36,3 @@ module.exports = (sourceCode, selectorLocations) => { | ||
(r, arr) => { | ||
const { value, raw, stylus } = tokensToRaw(arr) | ||
const { value, raw, stylus } = tokensToRaws(arr) | ||
r.value.push(value) | ||
@@ -46,3 +46,3 @@ r.raw.push(raw) | ||
const rawsBetween = tokensToRaw(between) | ||
const rawsBetween = tokensToRaws(between) | ||
@@ -49,0 +49,0 @@ return { |
"use strict" | ||
const { tokensToRaw, tokensToCss, tokensToStylus } = require("./tokens-to-raw") | ||
const { | ||
tokensToRaws, | ||
tokensToRawCss, | ||
tokensToRawStylus, | ||
isEndOfLineToken, | ||
isSkipToken, | ||
isWhitespaceToken, | ||
} = require("./token-utils") | ||
@@ -12,5 +19,4 @@ /** | ||
const minEnd = (options && options.minEnd) || -1 | ||
const cursor = sourceCode.createTokenCursor(start, { | ||
const cursor = sourceCode.createScopeTokenCursor(start, { | ||
endLocationIndex: maxEnd, | ||
scope: true, | ||
}) | ||
@@ -165,14 +171,14 @@ | ||
const raw = tokensToRaw(tokens) | ||
const rawsImportant = tokensToRaw(important) | ||
const raws = tokensToRaws(tokens) | ||
const rawsImportant = tokensToRaws(important) | ||
const { semicolon, endIndex } = valueTokens | ||
return { | ||
value: raw.value.replace(/\s+$/u, ""), | ||
value: raws.value.replace(/\s+$/u, ""), | ||
important: Boolean(important.length), | ||
raw: { | ||
css: raw.raw, | ||
stylus: raw.stylus, | ||
between: tokensToCss(betweenCss), | ||
stylusBetween: tokensToStylus(between), | ||
css: raws.raw, | ||
stylus: raws.stylus, | ||
between: tokensToRawCss(betweenCss), | ||
stylusBetween: tokensToRawStylus(between), | ||
important: | ||
@@ -188,30 +194,1 @@ rawsImportant.stylus !== " !important" | ||
} | ||
/** | ||
* Chechs if skip target token | ||
* @param {*} token token | ||
*/ | ||
function isSkipToken(token) { | ||
return ( | ||
isWhitespaceToken(token) || | ||
(token && (token.type === "comment" || token.type === "inline-comment")) | ||
) | ||
} | ||
/** | ||
* Chechs if whitespace token | ||
* @param {*} token token | ||
*/ | ||
function isWhitespaceToken(token) { | ||
return token && (token.type === "whitespace" || token.type === "linebreak") | ||
} | ||
/** | ||
* Chechs if end of line token | ||
* @param {*} token token | ||
*/ | ||
function isEndOfLineToken(token) { | ||
return ( | ||
!token || token.type === "inline-comment" || token.type === "linebreak" | ||
) | ||
} |
@@ -8,3 +8,3 @@ "use strict" | ||
const { getStartIndex } = require("./locations") | ||
const tokenCursorFactory = require("./token-cursor/factory") | ||
const cursors = require("./token-cursor/cursors") | ||
@@ -254,6 +254,34 @@ /** | ||
return tokenCursorFactory(tokens, startIndex, options) | ||
return cursors.forward(tokens, startIndex, options) | ||
} | ||
/** | ||
* Generate the scoped token cursor. | ||
* @param {number} startLocationIndex The index of start position of text | ||
* @param {object} [options] The options | ||
* @property {number} [endLocationIndex] The index of end position of text | ||
* @returns {object} the token cursor | ||
*/ | ||
createScopeTokenCursor(startLocationIndex, options) { | ||
const { tokens } = this | ||
const startIndex = this.getTokenIndex(startLocationIndex) | ||
return cursors.scope(tokens, startIndex, options) | ||
} | ||
/** | ||
* Generate the backword tokens cursor. | ||
* @param {number} startLocationIndex The index of start position of text | ||
* @param {object} [options] The options | ||
* @property {number} [startLocationIndex] The index of end position of text | ||
* @returns {object} the token cursor | ||
*/ | ||
createBackwardTokenCursor(endLocationIndex, options) { | ||
const { tokens } = this | ||
const endIndex = this.getTokenIndex(endLocationIndex) | ||
return cursors.backward(tokens, endIndex, options) | ||
} | ||
/** | ||
* Generate the tokens. | ||
@@ -274,29 +302,3 @@ * @param {number} startLocationIndex The index of start position of text | ||
/** | ||
* Returns the index of the first token in the array where predicate is true, and -1 | ||
* otherwise. | ||
* @param {number} startLocationIndex The index of start position of text | ||
* @param {function} predicate find calls predicate once for each token of the array, in ascending | ||
* order, until it finds one where predicate returns true. If such an token is found, | ||
* findTokenIndex immediately returns that token index. Otherwise, findTokenIndex returns -1. | ||
* @returns {number} the tokens index | ||
*/ | ||
findTokenIndex(locationIndex, predicate) { | ||
const { tokens } = this | ||
for ( | ||
let index = this.getTokenIndex(locationIndex); | ||
index < tokens.length; | ||
index++ | ||
) { | ||
if (predicate(tokens[index], index)) { | ||
return index | ||
} | ||
} | ||
return -1 | ||
} | ||
getText(start, end) { | ||
if (end == null) { | ||
return this.text.slice(this.getIndex(start)) | ||
} | ||
return this.text.slice(this.getIndex(start), this.getIndex(end) + 1) | ||
@@ -303,0 +305,0 @@ } |
"use strict" | ||
module.exports = class Cursor { | ||
constructor(tokens, startIndex) { | ||
constructor(tokens, startIndex, nextUntil) { | ||
this.tokens = tokens | ||
this.startIndex = startIndex | ||
this.nextUntil = nextUntil || Function.prototype | ||
} | ||
// eslint-disable-next-line class-methods-use-this | ||
prevUntil() { | ||
// noop | ||
} | ||
// eslint-disable-next-line class-methods-use-this | ||
nextUntil() { | ||
// noop | ||
} | ||
prev() { | ||
const { tokens, curIndex } = this | ||
const prev = curIndex == null ? this.startIndex : curIndex - 1 | ||
if (prev < 0) { | ||
return null | ||
} | ||
const token = tokens[prev] | ||
if (this.prevUntil(token)) { | ||
return null | ||
} | ||
this.curIndex = prev | ||
return (this.currToken = token) | ||
} | ||
next() { | ||
const { tokens, curIndex } = this | ||
const next = curIndex == null ? this.startIndex : curIndex + 1 | ||
if (next >= tokens.length) { | ||
if (next >= tokens.length || next < 0) { | ||
return null | ||
@@ -38,0 +15,0 @@ } |
@@ -34,18 +34,2 @@ "use strict" | ||
prev() { | ||
const { currToken } = this | ||
if (currToken) { | ||
if (LEFT_PAREN_KINDS[currToken.value] === this.scope.parenKind) { | ||
this.scope = this.scope.parent | ||
} else if (RIGHT_PAREN_KINDS[currToken.value]) { | ||
this.scope = { | ||
parent: this.scope, | ||
level: this.scope.level + 1, | ||
parenKind: RIGHT_PAREN_KINDS[currToken.value], | ||
} | ||
} | ||
} | ||
return super.prev() | ||
} | ||
next() { | ||
@@ -52,0 +36,0 @@ const { currToken } = this |
{ | ||
"name": "postcss-styl", | ||
"version": "0.0.1", | ||
"description": "PostCSS parser plugin for converting Stylus syntax to PostCSS nodes.", | ||
"version": "0.0.2", | ||
"description": "PostCSS parser plugin for converting Stylus syntax to PostCSS AST.", | ||
"main": "lib/index.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
@@ -9,3 +9,3 @@ # postcss-styl | ||
[PostCSS] parser plugin for converting [Stylus] syntax to [PostCSS] nodes. | ||
[PostCSS] parser plugin for converting [Stylus] syntax to [PostCSS] AST. | ||
@@ -24,3 +24,3 @@ ::: | ||
### Stylus to PostCSS Nodes | ||
### Stylus to PostCSS AST | ||
@@ -27,0 +27,0 @@ The main use of this plugin is to apply the [Stylus] syntax to linter using [PostCSS]. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32
120096
3798