Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add offset against column&line in parser #107

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = class Parser {
this.prevIndent = undefined
this.step = undefined

this.root.source = { input, start: { column: 1, line: 1 } }
this.root.source = { input, start: { offset: 0 } }
}

atrule(part) {
Expand Down Expand Up @@ -64,7 +64,7 @@ module.exports = class Parser {
let token = part.tokens[0]
let node = new Comment()
this.init(node, part)
node.source.end = { column: token[5], line: token[4] }
node.source.end = { offset: token[2] + token[1].length }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Core CSS parser generates offset together with line/column for backward compatibility
https://github.com/postcss/postcss/blob/main/lib/parser.js#L356-L363

I think we should do it here as well

this.commentText(node, token)
}

Expand Down Expand Up @@ -125,7 +125,7 @@ module.exports = class Parser {
!next.atrule &&
!next.colon &&
next.indent.length > part.indent.length
) {
) {
value.push(['space', next.before + next.indent])
value = value.concat(next.tokens)
this.pos += 1
Expand All @@ -138,9 +138,9 @@ module.exports = class Parser {
let comment = new Comment()
this.current.push(comment)
comment.source = {
end: { column: last[5], line: last[4] },
end: { offset: last[2] + last[1].length },
input: this.input,
start: { column: last[3], line: last[2] }
start: { offset: last[2] }
}
let prev = value[value.length - 1]
if (prev && prev[0] === 'space') {
Expand Down Expand Up @@ -172,8 +172,8 @@ module.exports = class Parser {
this.raw(node, 'value', value, colon)
}

error(msg, line, column) {
throw this.input.error(msg, line, column)
error(msg, offset) {
throw this.input.error(msg, offset)
}

firstSpaces(tokens) {
Expand Down Expand Up @@ -227,7 +227,7 @@ module.exports = class Parser {
}

indentedFirstLine(part) {
this.error('First line should not have indent', part.number, 1)
this.error('First line should not have indent', part.offset)
}

init(node, part) {
Expand All @@ -243,7 +243,7 @@ module.exports = class Parser {
}
node.source = {
input: this.input,
start: { column: part.tokens[0][3], line: part.tokens[0][2] }
start: { offset: part.tokens[0][2] }
}
}

Expand Down Expand Up @@ -292,8 +292,7 @@ module.exports = class Parser {
if (this.tokens[i].length > 3) {
let last = this.tokens[i]
this.root.source.end = {
column: last[5] || last[3],
line: last[4] || last[2]
offset: last[2] + last[1].length
}
break
}
Expand Down Expand Up @@ -351,8 +350,7 @@ module.exports = class Parser {
if (!last) last = altLast

node.source.end = {
column: last[5] || last[3],
line: last[4] || last[2]
offset: last[2] + last[1].length
}
}

Expand All @@ -376,15 +374,15 @@ module.exports = class Parser {
}

unnamedAtrule(token) {
this.error('At-rule without name', token[2], token[3])
this.error('At-rule without name', token[2])
}

unnamedDecl(token) {
this.error('Declaration without name', token[2], token[3])
this.error('Declaration without name', token[2])
}

wrongIndent(expected, real, part) {
let msg = `Expected ${expected} indent, but get ${real}`
this.error(msg, part.number, 1)
this.error(msg, part.offset)
}
}
Loading