Skip to content

Commit

Permalink
Fix incorrect AST for nesting block with trailing comment (#6)
Browse files Browse the repository at this point in the history
* Fix incorrect AST for nesting block with trailing comment

* fix
  • Loading branch information
ota-meshi authored Jun 17, 2022
1 parent 46bde4a commit 90d62e4
Show file tree
Hide file tree
Showing 7 changed files with 332 additions and 24 deletions.
47 changes: 42 additions & 5 deletions lib/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const parseMediaParams = require("./parse-media-params")
const getSelectorEndIndex = require("./get-selector-end-index")
const getCssLiteralIndices = require("./get-css-literal-indices")
const { getName } = require("./stylus-nodes")
const { findLast } = require("./util")
const { findLast, findIndex } = require("./util")

const debug = require("debug")("postcss-styl:parser")

Expand Down Expand Up @@ -366,6 +366,34 @@ function atrulePostProc(atRule, { postfix, parsedNameAndCondition } = {}) {
}
}

/**
*Get block contents indent
*/
function getBlockContentIndent(sourceCode, bodyStartIndex) {
let hasLineBreak = false
const indentedTokenIndex = findIndex(
sourceCode.text,
(c) => {
if (!hasLineBreak) {
if (c === "\n") {
hasLineBreak = true
}
return false
}
return c.trim()
},
bodyStartIndex,
)
if (indentedTokenIndex >= 0) {
// Has before token
// | .a
// | color red
// | // comment
return sourceCode.getIndentFromIndex(indentedTokenIndex)
}
return null
}

class StylusParser {
constructor(input) {
this.input = input
Expand Down Expand Up @@ -1928,7 +1956,9 @@ class StylusParser {

const rawAfter = parseRawAfter(this.sourceCode, bodyEndIndex, {
blockCommentAsRaw: false,
bodyStartIndex: hasBrace ? null : bodyStartIndex,
maxIndent: hasBrace
? null
: getBlockContentIndent(this.sourceCode, bodyStartIndex),
})

return {
Expand Down Expand Up @@ -1976,12 +2006,19 @@ class StylusParser {
* @returns {number}
*/
function calcBlockEnd(initEndIndex) {
let blockContentIndent = null
if (!hasBrace) {
blockContentIndent = getBlockContentIndent(
sourceCode,
bodyStartIndex,
)
}
const { startIndex, inlineComments } = parseRawAfter(
sourceCode,
initEndIndex,
{
blockCommentAsRaw: false,
bodyStartIndex: hasBrace ? null : bodyStartIndex,
maxIndent: blockContentIndent,
},
)
if (!hasBrace && inlineComments.length) {
Expand All @@ -1991,12 +2028,12 @@ class StylusParser {

// Step 1. calc indent
let minIndent = 0
if (bodyStartIndex < startIndex) {
if (blockContentIndent != null) {
// Has before token
// | .a
// | color red
// | // comment
minIndent = sourceCode.getIndentFromIndex(startIndex - 1)
minIndent = blockContentIndent
} else {
minIndent =
sourceCode.getIndentFromIndex(startIndex - 1) + 1
Expand Down
23 changes: 4 additions & 19 deletions lib/parser/parse-raw-after.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict"

const { tokensToRaws, isSkipToken } = require("./token-utils")
const { findLastIndex, findIndex } = require("./util")
const { findLastIndex } = require("./util")

module.exports = (sourceCode, end, opt = {}) => {
const options = Object.assign({ blockCommentAsRaw: true }, opt)
Expand All @@ -23,28 +23,13 @@ module.exports = (sourceCode, end, opt = {}) => {
token.value !== "{"
? stripStartLineComments(after)
: after
if (options.bodyStartIndex != null || options.maxIndent != null) {
if (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)
}
const maxIndent = options.maxIndent
afterTokens = processComment(sourceCode, afterTokens, maxIndent)
}

startIndex = afterTokens.length ? afterTokens[0].range[0] : startIndex
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.foo
.bar
//bar
//foo
136 changes: 136 additions & 0 deletions tests/fixtures/inline-comment08-at-end-of-rule copy/parsed-win.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
{
"raws": {
"semicolon": false,
"after": "\r\n"
},
"type": "root",
"nodes": [
{
"raws": {
"before": "",
"between": "",
"semicolon": false,
"after": ""
},
"type": "rule",
"nodes": [
{
"raws": {
"before": "\r\n ",
"between": "",
"after": ""
},
"type": "rule",
"nodes": [
{
"raws": {
"before": "\r\n ",
"left": "",
"right": "",
"inline": true
},
"type": "comment",
"source": {
"start": {
"offset": 18,
"line": 3,
"column": 5
},
"input": {
"file": "input.styl"
},
"end": {
"offset": 22,
"line": 3,
"column": 9
}
},
"text": "bar"
}
],
"source": {
"start": {
"offset": 8,
"line": 2,
"column": 3
},
"startChildren": {
"offset": 12,
"line": 2,
"column": 7
},
"input": {
"file": "input.styl"
},
"end": {
"offset": 22,
"line": 3,
"column": 9
}
},
"selector": ".bar",
"pythonic": true
},
{
"raws": {
"before": "\r\n ",
"left": "",
"right": "",
"inline": true
},
"type": "comment",
"source": {
"start": {
"offset": 27,
"line": 4,
"column": 3
},
"input": {
"file": "input.styl"
},
"end": {
"offset": 31,
"line": 4,
"column": 7
}
},
"text": "foo"
}
],
"source": {
"start": {
"offset": 0,
"line": 1,
"column": 1
},
"startChildren": {
"offset": 4,
"line": 1,
"column": 5
},
"input": {
"file": "input.styl"
},
"end": {
"offset": 31,
"line": 4,
"column": 7
}
},
"selector": ".foo",
"pythonic": true
}
],
"source": {
"input": {
"file": "input.styl"
},
"start": {
"offset": 0,
"line": 1,
"column": 1
},
"lang": "stylus",
"syntax": "ok"
}
}
Loading

0 comments on commit 90d62e4

Please sign in to comment.