Skip to content

Commit ed6f3d5

Browse files
authored
fix: wrong AST and stringify in comments (#15)
* add tests * fix: wrong AST and stringify in comments
1 parent ef65c46 commit ed6f3d5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+2264
-116
lines changed

lib/parser/index.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ class StylusParser {
498498
if (rawAfter.after !== rawAfter.stylusAfter) {
499499
rootNode.raws.stylusAfter = rawAfter.stylusAfter
500500
}
501-
this.pushInlineComments(rootNode, rawAfter.inlineComments)
501+
this.pushComments(rootNode, rawAfter.inlineComments)
502502
return rootNode
503503
}
504504

@@ -1241,7 +1241,9 @@ class StylusParser {
12411241
let rawStylusBefore = null
12421242
if (postfix) {
12431243
const { after: postfixBefore, stylusAfter: postfixStylusBefore } =
1244-
parseRawAfter(this.sourceCode, startIndex - 1)
1244+
parseRawAfter(this.sourceCode, startIndex - 1, {
1245+
blockCommentAsRaw: true,
1246+
})
12451247
rawBefore = ""
12461248
rawStylusBefore = ""
12471249
atRuleRaws.postfixBefore = postfixBefore
@@ -1311,7 +1313,7 @@ class StylusParser {
13111313
childNodes.forEach((n, i) =>
13121314
this.process(n, atRule, new ProcessInfo(childNodes, i, info)),
13131315
)
1314-
this.pushInlineComments(atRule, blockAfterInlineComments)
1316+
this.pushComments(atRule, blockAfterInlineComments)
13151317
}
13161318
atrulePostProc(atRule, { postfix, parsedNameAndCondition })
13171319
return atRule
@@ -1528,7 +1530,7 @@ class StylusParser {
15281530
}
15291531
})
15301532

1531-
this.pushInlineComments(atRule, blockAfterInlineComments)
1533+
this.pushComments(atRule, blockAfterInlineComments)
15321534
atrulePostProc(atRule, {})
15331535

15341536
atRule.object = true
@@ -1749,7 +1751,7 @@ class StylusParser {
17491751
blockNode.nodes.forEach((n, i) =>
17501752
this.process(n, rule, new ProcessInfo(blockNode.nodes, i, info)),
17511753
)
1752-
this.pushInlineComments(rule, afterInlineComments)
1754+
this.pushComments(rule, afterInlineComments)
17531755

17541756
// raws.semicolon
17551757
const lastAstNode = findLast(rule.nodes, (n) => n.type !== "comment")
@@ -2045,7 +2047,7 @@ class StylusParser {
20452047
sourceCode,
20462048
initEndIndex,
20472049
{
2048-
blockCommentAsRaw: false,
2050+
blockCommentAsRaw: true,
20492051
maxIndent: blockContentIndent,
20502052
},
20512053
)
@@ -2135,15 +2137,18 @@ class StylusParser {
21352137
end && this.sourceCode.getIndex(end),
21362138
)
21372139
}
2138-
this.pushInlineComments(parent, parsedRawBefore.inlineComments)
2140+
this.pushComments(parent, parsedRawBefore.inlineComments)
21392141
return parsedRawBefore
21402142
}
21412143

2142-
pushInlineComments(parent, inlineComments) {
2143-
for (const { token, before, stylusBefore } of inlineComments) {
2144+
pushComments(parent, comments) {
2145+
for (const { token, before, stylusBefore } of comments) {
2146+
const inline = token.type === "inline-comment"
21442147
const startIndex = token.range[0]
21452148
const endIndex = token.range[1] - 1
2146-
const contents = token.value.replace(/^\/\//gu, "")
2149+
const contents = inline
2150+
? token.value.replace(/^\/\//gu, "")
2151+
: token.value.replace(/^\/\*|\*\/$/gu, "")
21472152
const text = contents.trim()
21482153
const commentSource = {
21492154
start: this.sourceCode.getLoc(startIndex),
@@ -2154,7 +2159,7 @@ class StylusParser {
21542159
before,
21552160
left: text ? /^\s*/u.exec(contents)[0] : contents,
21562161
right: text ? /\s*$/u.exec(contents)[0] : "",
2157-
inline: true,
2162+
inline,
21582163
}
21592164
if (before !== stylusBefore) {
21602165
commentRaws.stylusBefore = stylusBefore

lib/parser/parse-atrule-name-and-condition.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ module.exports = (sourceCode, start, maxEnd, { expression } = DEFAULT_OPT) => {
102102
}
103103

104104
if (!params.length) {
105-
between.push(...afterName)
105+
between.unshift(...afterName)
106106
afterName.length = 0
107107
}
108108

lib/parser/parse-raw-after.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
"use strict"
22

3-
const { tokensToRaws, isSkipToken } = require("./token-utils")
3+
const {
4+
tokensToRaws,
5+
isSkipToken,
6+
createCommentTokenInfo,
7+
} = require("./token-utils")
48
const { findLastIndex } = require("./util")
59

6-
module.exports = (sourceCode, end, opt = {}) => {
7-
const options = Object.assign({ blockCommentAsRaw: true }, opt)
10+
module.exports = (sourceCode, end, options) => {
811
const cursor = sourceCode.createBackwardTokenCursor(end)
912

1013
const after = []
@@ -34,23 +37,18 @@ module.exports = (sourceCode, end, opt = {}) => {
3437

3538
startIndex = afterTokens.length ? afterTokens[0].range[0] : startIndex
3639

37-
const before = []
40+
const buffer = []
3841
const inlineComments = []
3942
for (const afterToken of afterTokens) {
4043
if (afterToken.type === "inline-comment") {
41-
const raws = tokensToRaws(before)
42-
inlineComments.push({
43-
token: afterToken,
44-
before: raws.raw,
45-
stylusBefore: raws.stylus,
46-
})
47-
before.length = 0
44+
inlineComments.push(createCommentTokenInfo(buffer, afterToken))
45+
buffer.length = 0
4846
} else {
49-
before.push(afterToken)
47+
buffer.push(afterToken)
5048
}
5149
}
5250

53-
const raws = tokensToRaws(before)
51+
const raws = tokensToRaws(buffer)
5452

5553
return {
5654
after: raws.raw,

lib/parser/parse-raw-before.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
11
"use strict"
22

3-
const { tokensToRaws, isSkipToken } = require("./token-utils")
3+
const {
4+
tokensToRaws,
5+
isSkipToken,
6+
createCommentTokenInfo,
7+
} = require("./token-utils")
48

59
module.exports = (sourceCode, start, end) => {
610
const cursor = sourceCode.createTokenCursor(start, {
711
endLocationIndex: end,
812
})
913

1014
const inlineComments = []
11-
const before = []
15+
const buffer = []
1216

1317
let token = cursor.next()
1418
let endIndex = token ? token.range[0] - 1 : start
1519
while (token && isSkipToken(token)) {
1620
if (token.type === "inline-comment") {
17-
const raws = tokensToRaws(before)
18-
inlineComments.push({
19-
token,
20-
before: raws.raw,
21-
stylusBefore: raws.stylus,
22-
})
23-
before.length = 0
21+
inlineComments.push(createCommentTokenInfo(buffer, token))
22+
buffer.length = 0
2423
} else {
25-
before.push(token)
24+
buffer.push(token)
2625
endIndex = token.range[1] - 1
2726
}
2827
token = cursor.next()
2928
}
3029

31-
const raws = tokensToRaws(before)
30+
const raws = tokensToRaws(buffer)
3231

3332
return {
3433
before: raws.raw,

lib/parser/parse-selector.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,7 @@ module.exports = (sourceCode, selectorLocations) => {
99
const tokens = selectors[selectors.length - 1]
1010
while (tokens.length) {
1111
const token = tokens.pop()
12-
if (
13-
token.type === "whitespace" ||
14-
token.type === "escaped-whitespace" ||
15-
token.type === "linebreak" ||
16-
token.type === "comment" ||
17-
token.type === "inline-comment"
18-
) {
12+
if (isSkipToken(token)) {
1913
between.unshift(token)
2014
} else {
2115
tokens.push(token)

lib/parser/parse-value.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ module.exports = (sourceCode, start, maxEnd, options) => {
193193
const rawsImportant = tokensToRaws(important)
194194

195195
const { semicolon, endIndex } = valueTokens
196+
196197
return {
197198
value: raws.value.replace(/\s+$/u, ""),
198199
important: Boolean(important.length),

lib/parser/token-utils.js

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -43,50 +43,72 @@ module.exports = {
4343
}
4444
return rawStylus.join("")
4545
},
46-
tokensToRaws(tokens) {
47-
const value = []
48-
const rawCss = []
49-
const rawStylus = []
50-
for (const token of tokens) {
51-
if (typeof token === "string") {
52-
rawStylus.push(token)
53-
rawCss.push(token)
54-
value.push(token)
55-
} else {
56-
rawStylus.push(token.value)
57-
if (token.type === "inline-comment") {
58-
rawCss.push(replaceInlineComment(token))
59-
} else if (token.type === "escaped-whitespace") {
60-
const val = replaceEscaped(token)
61-
rawCss.push(val)
62-
value.push(val)
63-
} else {
64-
rawCss.push(token.value)
65-
if (token.type !== "comment") {
66-
value.push(token.value)
67-
}
68-
}
69-
}
70-
}
46+
tokensToRaws,
47+
createCommentTokenInfo(before, commentToken) {
48+
const raws = tokensToRaws(before)
7149
return {
72-
value: value.join(""),
73-
raw: rawCss.join(""),
74-
stylus: rawStylus.join(""),
50+
token: commentToken,
51+
before: raws.raw,
52+
stylusBefore: raws.stylus,
7553
}
7654
},
7755
isEndOfLineToken,
7856
isSkipToken,
57+
isCommentToken,
7958
isWhitespaceToken,
8059
}
8160

61+
/**
62+
* Tokens to raw info
63+
* @param {*} tokens
64+
*/
65+
function tokensToRaws(tokens) {
66+
const value = []
67+
const rawCss = []
68+
const rawStylus = []
69+
for (const token of tokens) {
70+
if (typeof token === "string") {
71+
rawStylus.push(token)
72+
rawCss.push(token)
73+
value.push(token)
74+
} else {
75+
rawStylus.push(token.value)
76+
if (token.type === "inline-comment") {
77+
rawCss.push(replaceInlineComment(token))
78+
} else if (token.type === "escaped-whitespace") {
79+
const val = replaceEscaped(token)
80+
rawCss.push(val)
81+
value.push(val)
82+
} else {
83+
rawCss.push(token.value)
84+
if (token.type !== "comment") {
85+
value.push(token.value)
86+
}
87+
}
88+
}
89+
}
90+
return {
91+
value: value.join(""),
92+
raw: rawCss.join(""),
93+
stylus: rawStylus.join(""),
94+
}
95+
}
96+
8297
/**
8398
* Checks if skip target token
8499
* @param {*} token token
85100
*/
86101
function isSkipToken(token) {
102+
return isWhitespaceToken(token) || isCommentToken(token)
103+
}
104+
105+
/**
106+
* Checks if comment token
107+
* @param {*} token token
108+
*/
109+
function isCommentToken(token) {
87110
return (
88-
isWhitespaceToken(token) ||
89-
(token && (token.type === "comment" || token.type === "inline-comment"))
111+
token && (token.type === "comment" || token.type === "inline-comment")
90112
)
91113
}
92114

0 commit comments

Comments
 (0)