Skip to content

Commit 637d5e1

Browse files
authored
fix: duplicated comment nodes (#12)
1 parent 2624722 commit 637d5e1

File tree

10 files changed

+376
-39
lines changed

10 files changed

+376
-39
lines changed

explorer/src/components/AstExplorer.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,8 @@ function processValue(options, ctx, value) {
297297
? entries.filter(([key]) => key === "source")
298298
: []
299299
entries = entries.filter(
300-
([key]) => key !== "source" && key !== "parent",
300+
([key]) =>
301+
key !== "source" && key !== "parent" && key !== "type",
301302
)
302303
if (typeEntry) entries.unshift(typeEntry)
303304
entries.push(...locEntries)

lib/parser/index.js

Lines changed: 58 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ const { findLast, findIndex } = require("./util")
1818

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

21+
/**
22+
* @typedef {import("postcss").Root} Root
23+
* @typedef {import("postcss").Rule} Rule
24+
* @typedef {import("postcss").AtRule} AtRule
25+
* @typedef {import("postcss").Declaration} Declaration
26+
* @typedef {import("postcss").Comment} Comment
27+
* @typedef {import("postcss").ChildNode} PostCssChildNode
28+
* @typedef {Rule | AtRule | Root} PostCssParentNode
29+
*/
30+
2131
/**
2232
* build raw
2333
* @param {string} value
@@ -76,7 +86,7 @@ class ProcessInfo {
7686

7787
/**
7888
* Checks if the given node is a node that can be assigned a semicolon.
79-
* @param {PostCSSNode}
89+
* @param {PostCssChildNode} node
8090
*/
8191
function isSemiOptNode(node) {
8292
return node.type === "decl" || (node.type === "atrule" && !node.nodes)
@@ -364,6 +374,8 @@ function atrulePostProc(atRule, { postfix, parsedNameAndCondition } = {}) {
364374
) {
365375
atRule.omittedSemi = true
366376
}
377+
378+
adjustEndPosition(atRule)
367379
}
368380

369381
/**
@@ -394,6 +406,20 @@ function getBlockContentIndent(sourceCode, bodyStartIndex) {
394406
return null
395407
}
396408

409+
/**
410+
* Adjust end position.
411+
* If there is a comment at the end, it may calculate the wrong end position, so adjust it.
412+
* @param {Rule | AtRule} node
413+
*/
414+
function adjustEndPosition(node) {
415+
const last = node.last
416+
if (last && last.source && last.source.end) {
417+
if (node.source.end.offset < last.source.end.offset) {
418+
node.source.end = { ...last.source.end }
419+
}
420+
}
421+
}
422+
397423
class StylusParser {
398424
constructor(input) {
399425
this.input = input
@@ -478,7 +504,7 @@ class StylusParser {
478504

479505
/**
480506
* @param {StylusNode} node
481-
* @param {PostCssNode} parent
507+
* @param {PostCssParentNode} parent
482508
* @param {ProcessInfo} info
483509
*/
484510
process(node, parent, info) {
@@ -495,7 +521,7 @@ class StylusParser {
495521

496522
/**
497523
* @param {StylusNode} node
498-
* @param {PostCssNode} parent
524+
* @param {PostCssParentNode} parent
499525
* @param {ProcessInfo} info
500526
*/
501527
group(node, parent, info) {
@@ -506,7 +532,7 @@ class StylusParser {
506532

507533
/**
508534
* @param {StylusNode} node
509-
* @param {PostCssNode} parent
535+
* @param {PostCssParentNode} parent
510536
* @param {ProcessInfo} info
511537
*/
512538
media(node, parent, info) {
@@ -544,7 +570,7 @@ class StylusParser {
544570

545571
/**
546572
* @param {StylusNode} node
547-
* @param {PostCssNode} parent
573+
* @param {PostCssParentNode} parent
548574
* @param {ProcessInfo} info
549575
*/
550576
charset(node, parent, info) {
@@ -553,7 +579,7 @@ class StylusParser {
553579

554580
/**
555581
* @param {StylusNode} node
556-
* @param {PostCssNode} parent
582+
* @param {PostCssParentNode} parent
557583
* @param {ProcessInfo} info
558584
*/
559585
supports(node, parent, info) {
@@ -562,7 +588,7 @@ class StylusParser {
562588

563589
/**
564590
* @param {StylusNode} node
565-
* @param {PostCssNode} parent
591+
* @param {PostCssParentNode} parent
566592
* @param {ProcessInfo} info
567593
*/
568594
import(node, parent, info) {
@@ -571,7 +597,7 @@ class StylusParser {
571597

572598
/**
573599
* @param {StylusNode} node
574-
* @param {PostCssNode} parent
600+
* @param {PostCssParentNode} parent
575601
* @param {ProcessInfo} info
576602
*/
577603
keyframes(node, parent, info) {
@@ -580,7 +606,7 @@ class StylusParser {
580606

581607
/**
582608
* @param {StylusNode} node
583-
* @param {PostCssNode} parent
609+
* @param {PostCssParentNode} parent
584610
* @param {ProcessInfo} info
585611
*/
586612
extend(node, parent, info) {
@@ -589,7 +615,7 @@ class StylusParser {
589615

590616
/**
591617
* @param {StylusNode} node
592-
* @param {PostCssNode} parent
618+
* @param {PostCssParentNode} parent
593619
* @param {ProcessInfo} info
594620
*/
595621
literal(node, parent, info) {
@@ -601,7 +627,7 @@ class StylusParser {
601627

602628
/**
603629
* @param {StylusNode} node
604-
* @param {PostCssNode} parent
630+
* @param {PostCssParentNode} parent
605631
* @param {ProcessInfo} info
606632
*/
607633
atrule(node, parent, info) {
@@ -610,7 +636,7 @@ class StylusParser {
610636

611637
/**
612638
* @param {StylusNode} node
613-
* @param {PostCssNode} parent
639+
* @param {PostCssParentNode} parent
614640
* @param {ProcessInfo} info
615641
*/
616642
ternary(node, parent, info) {
@@ -664,7 +690,7 @@ class StylusParser {
664690

665691
/**
666692
* @param {StylusNode} node
667-
* @param {PostCssNode} parent
693+
* @param {PostCssParentNode} parent
668694
* @param {ProcessInfo} info
669695
*/
670696
selector(node, parent, info) {
@@ -710,7 +736,7 @@ class StylusParser {
710736

711737
/**
712738
* @param {StylusNode} node
713-
* @param {PostCssNode} parent
739+
* @param {PostCssParentNode} parent
714740
* @param {ProcessInfo} info
715741
*/
716742
property(node, parent, info) {
@@ -728,7 +754,7 @@ class StylusParser {
728754

729755
/**
730756
* @param {StylusNode} node
731-
* @param {PostCssNode} parent
757+
* @param {PostCssParentNode} parent
732758
* @param {ProcessInfo} info
733759
*/
734760
// eslint-disable-next-line complexity -- X(
@@ -855,7 +881,7 @@ class StylusParser {
855881

856882
/**
857883
* @param {StylusNode} node
858-
* @param {PostCssNode} parent
884+
* @param {PostCssParentNode} parent
859885
* @param {ProcessInfo} info
860886
*/
861887
ident(node, parent, info) {
@@ -928,7 +954,7 @@ class StylusParser {
928954

929955
/**
930956
* @param {StylusNode} node
931-
* @param {PostCssNode} parent
957+
* @param {PostCssParentNode} parent
932958
* @param {ProcessInfo} info
933959
*/
934960
function(node, parent, info) {
@@ -953,7 +979,7 @@ class StylusParser {
953979

954980
/**
955981
* @param {StylusNode} node
956-
* @param {PostCssNode} parent
982+
* @param {PostCssParentNode} parent
957983
* @param {ProcessInfo} info
958984
*/
959985
call(node, parent, info) {
@@ -971,7 +997,7 @@ class StylusParser {
971997

972998
/**
973999
* @param {StylusNode} node
974-
* @param {PostCssNode} parent
1000+
* @param {PostCssParentNode} parent
9751001
* @param {ProcessInfo} info
9761002
*/
9771003
binop(node, parent, info) {
@@ -985,7 +1011,7 @@ class StylusParser {
9851011

9861012
/**
9871013
* @param {StylusNode} node
988-
* @param {PostCssNode} parent
1014+
* @param {PostCssParentNode} parent
9891015
* @param {ProcessInfo} info
9901016
*/
9911017
each(node, parent, info) {
@@ -1006,7 +1032,7 @@ class StylusParser {
10061032

10071033
/**
10081034
* @param {StylusNode} node
1009-
* @param {PostCssNode} parent
1035+
* @param {PostCssParentNode} parent
10101036
* @param {ProcessInfo} info
10111037
*/
10121038
if(node, parent, info) {
@@ -1048,7 +1074,7 @@ class StylusParser {
10481074

10491075
/**
10501076
* @param {StylusNode} node
1051-
* @param {PostCssNode} parent
1077+
* @param {PostCssParentNode} parent
10521078
* @param {ProcessInfo} info
10531079
*/
10541080
return(node, parent, info) {
@@ -1057,7 +1083,7 @@ class StylusParser {
10571083

10581084
/**
10591085
* @param {StylusNode} node
1060-
* @param {PostCssNode} parent
1086+
* @param {PostCssParentNode} parent
10611087
* @param {ProcessInfo} info
10621088
*/
10631089
comment(node, parent, _info) {
@@ -1102,8 +1128,8 @@ class StylusParser {
11021128
/* eslint-disable complexity -- X( */
11031129
/**
11041130
* @param {StylusNode|number} nodeOrIndex
1105-
* @param {*} infomation
1106-
* @param {PostCssNode} parent
1131+
* @param {*} information
1132+
* @param {PostCssParentNode} parent
11071133
* @param {ProcessInfo} info
11081134
*/
11091135
atruleImpl(
@@ -1351,7 +1377,7 @@ class StylusParser {
13511377

13521378
/**
13531379
* @param {StylusNode} node
1354-
* @param {PostCssNode} parent
1380+
* @param {PostCssParentNode} parent
13551381
* @param {ProcessInfo} info
13561382
*/
13571383
atruleObjectExpressionImpl(node, { object, withinObject }, parent, info) {
@@ -1511,7 +1537,7 @@ class StylusParser {
15111537

15121538
/**
15131539
* @param {StylusNode} node
1514-
* @param {PostCssNode} parent
1540+
* @param {PostCssParentNode} parent
15151541
* @param {ProcessInfo} info
15161542
*/
15171543
atruleCssLiteralImpl(node, parent, _info) {
@@ -1622,8 +1648,8 @@ class StylusParser {
16221648

16231649
/**
16241650
* @param {StylusNode} node
1625-
* @param {*} infomation
1626-
* @param {PostCssNode} parent
1651+
* @param {*} information
1652+
* @param {PostCssParentNode} parent
16271653
* @param {ProcessInfo} info
16281654
*/
16291655
ruleImpl(
@@ -1740,12 +1766,14 @@ class StylusParser {
17401766
delete rule.raws.semicolon
17411767
}
17421768

1769+
adjustEndPosition(rule)
1770+
17431771
return rule
17441772
}
17451773

17461774
/**
17471775
* @param {StylusNode} node
1748-
* @param {PostCssNode} parent
1776+
* @param {PostCssParentNode} parent
17491777
* @param {ProcessInfo} info
17501778
*/
17511779
ruleEmptyExpressionImpl(node, parent, _info) {

lib/parser/stylus-source-code.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ const { findLastIndex } = require("./util")
1515
* @property {number[]} lineStartIndices start indices of lines
1616
* @property {string} text source code
1717
*/
18+
/**
19+
* @typedef {import('postcss').Position} Position
20+
*/
1821

1922
/**
2023
* The source code split into lines according
@@ -323,7 +326,7 @@ class SourceCode {
323326
/**
324327
* Converts a source text index into a (line, column) pair.
325328
* @param {number|object} loc The index or line/column location
326-
* @returns {Object} A {line, column} location object with a 1-indexed column
329+
* @returns {Position} A {line, column} location object with a 1-indexed column
327330
*/
328331
getLoc(loc) {
329332
if (typeof loc === "number") {

lib/parser/util.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
/**
44
* Find last match element
5-
* @param {Array|string} arr array
6-
* @param {function} callback callback
7-
* @returns {*} element
5+
* @template {any[] | string} A
6+
* @param {A} arr array
7+
* @param {(element: A extends (infer U)[] ? U : string) => boolean} callback callback
8+
* @returns {(A extends (infer U)[] ? U : string) | undefined} element
89
*/
910
function findLast(arr, callback) {
1011
const index = findLastIndex(arr, callback)
@@ -16,8 +17,9 @@ function findLast(arr, callback) {
1617

1718
/**
1819
* Find match index
19-
* @param {Array|string} arr array
20-
* @param {function} callback callback
20+
* @template {any[] | string} A
21+
* @param {A} arr array
22+
* @param {(element: A extends (infer U)[] ? U : string) => boolean} callback callback
2123
* @param {number} initIndex find start index
2224
* @returns {number} index
2325
*/
@@ -33,8 +35,9 @@ function findIndex(arr, callback, initIndex = 0) {
3335

3436
/**
3537
* Find last match index
36-
* @param {Array|string} arr array
37-
* @param {function} callback callback
38+
* @template {any[] | string} A
39+
* @param {A} arr array
40+
* @param {(element: A extends (infer U)[] ? U : string) => boolean} callback callback
3841
* @param {number} initIndex find start index
3942
* @returns {number} last index
4043
*/
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.someClass
2+
margin-top 5px // comment
3+
#someId
4+
padding 0

0 commit comments

Comments
 (0)