Skip to content

Commit

Permalink
fix: node end offset location (#147)
Browse files Browse the repository at this point in the history
* chore: upgrade postcss deps

* chore: broken postcss-parser-tests usage

- cases are already parsed
- https://github.com/postcss/postcss-parser-tests/blob/main/CHANGELOG.md#87
- postcss/postcss-parser-tests@442f6dc

* fix: offset in sass specific parsing
  • Loading branch information
idoros committed Sep 8, 2023
1 parent c75f4a3 commit 5105e42
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 15 deletions.
18 changes: 15 additions & 3 deletions lib/scss-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ class ScssParser extends Parser {
this.init(node, token[2])
node.raws.inline = true
let pos = this.input.fromOffset(token[3])
node.source.end = { column: pos.col, line: pos.line, offset: token[3] }
node.source.end = {
column: pos.col,
line: pos.line,
offset: token[3] + 1
}

let text = token[1].slice(2)
if (/^\s*$/.test(text)) {
Expand Down Expand Up @@ -106,10 +110,18 @@ class ScssParser extends Parser {
}
if (last[3]) {
let pos = this.input.fromOffset(last[3])
node.source.end = { column: pos.col, line: pos.line, offset: last[3] }
node.source.end = {
column: pos.col,
line: pos.line,
offset: last[3] + 1
}
} else {
let pos = this.input.fromOffset(last[2])
node.source.end = { column: pos.col, line: pos.line, offset: last[2] }
node.source.end = {
column: pos.col,
line: pos.line,
offset: last[2] + 1
}
}

while (tokens[0][0] !== 'word') {
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
}
],
"peerDependencies": {
"postcss": "^8.4.19"
"postcss": "^8.4.29"
},
"devDependencies": {
"@logux/eslint-config": "^51.0.1",
Expand All @@ -65,8 +65,8 @@
"eslint-plugin-perfectionist": "^1.5.1",
"eslint-plugin-prefer-let": "^3.0.1",
"eslint-plugin-promise": "^6.1.1",
"postcss": "^8.4.27",
"postcss-parser-tests": "^8.6.0",
"postcss": "^8.4.29",
"postcss-parser-tests": "^8.8.0",
"uvu": "^0.5.6"
},
"prettier": {
Expand Down
16 changes: 8 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions test/location.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
let { equal } = require('uvu/assert')
let { test } = require('uvu')

let parse = require('../lib/scss-parse')

function checkOffset(source, node, expected) {
let start = node.source.start.offset
let end = node.source.end.offset
equal(source.slice(start, end), expected)
}

test('inline comment', () => {
let source = 'a{//xxx\n}'
let root = parse(source)

checkOffset(source, root.first, 'a{//xxx\n}')
checkOffset(source, root.first.first, '//xxx')
})

test('nested prop with value', () => {
let source = 'a { margin: 0 { left: 10px; }}'
let root = parse(source)

checkOffset(source, root.first, 'a { margin: 0 { left: 10px; }}')
checkOffset(source, root.first.first, 'margin: 0 { left: 10px; }')
checkOffset(source, root.first.first.first, 'left: 10px;')
})

test.run()
2 changes: 1 addition & 1 deletion test/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let parse = require('../lib/scss-parse')
eachTest((name, css, json) => {
test('parses ' + name, () => {
let parsed = jsonify(parse(css, { from: name }))
equal(JSON.parse(parsed), JSON.parse(json))
equal(parsed, json)
})
})

Expand Down

0 comments on commit 5105e42

Please sign in to comment.