Skip to content

Commit 4f3e76b

Browse files
committed
Fix position for files w/o LF line endings
1 parent cb5d14e commit 4f3e76b

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

Diff for: index.js

+16-19
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var find = require('property-information/find')
66
var ns = require('web-namespaces')
77
var s = require('hastscript/svg')
88
var h = require('hastscript')
9-
var count = require('ccount')
9+
var vfileLocation = require('vfile-location')
1010

1111
module.exports = wrapper
1212

@@ -36,8 +36,7 @@ function wrapper(ast, options) {
3636
return transform(ast, {
3737
schema: settings.space === 'svg' ? svg : html,
3838
file: file,
39-
verbose: settings.verbose,
40-
location: false
39+
verbose: settings.verbose
4140
})
4241
}
4342

@@ -47,7 +46,7 @@ function transform(ast, config) {
4746
var fn = own.call(map, ast.nodeName) ? map[ast.nodeName] : element
4847
var children
4948
var node
50-
var pos
49+
var position
5150

5251
if (fn === element) {
5352
config.schema = ast.namespaceURI === ns.svg ? svg : html
@@ -60,11 +59,11 @@ function transform(ast, config) {
6059
node = fn(ast, children, config)
6160

6261
if (ast.sourceCodeLocation && config.file) {
63-
pos = location(node, ast.sourceCodeLocation, config)
62+
position = location(node, ast.sourceCodeLocation, config)
6463

65-
if (pos) {
64+
if (position) {
6665
config.location = true
67-
node.position = pos
66+
node.position = position
6867
}
6968
}
7069

@@ -75,11 +74,10 @@ function transform(ast, config) {
7574

7675
// Transform children.
7776
function nodes(children, config) {
78-
var length = children.length
7977
var index = -1
8078
var result = []
8179

82-
while (++index < length) {
80+
while (++index < children.length) {
8381
result[index] = transform(children[index], config)
8482
}
8583

@@ -89,21 +87,20 @@ function nodes(children, config) {
8987
// Transform a document.
9088
// Stores `ast.quirksMode` in `node.data.quirksMode`.
9189
function root(ast, children, config) {
92-
var node = {type: 'root', children: children, data: {}}
90+
var node = {
91+
type: 'root',
92+
children: children,
93+
data: {quirksMode: ast.mode === 'quirks' || ast.mode === 'limited-quirks'}
94+
}
9395
var doc
94-
95-
node.data.quirksMode = ast.mode === 'quirks' || ast.mode === 'limited-quirks'
96+
var location
9697

9798
if (config.file && config.location) {
9899
doc = String(config.file)
99-
100+
location = vfileLocation(doc)
100101
node.position = {
101-
start: {line: 1, column: 1, offset: 0},
102-
end: {
103-
line: count(doc, '\n') + 1,
104-
column: doc.length - doc.lastIndexOf('\n'),
105-
offset: doc.length
106-
}
102+
start: location.toPoint(0),
103+
end: location.toPoint(doc.length)
107104
}
108105
}
109106

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@
3030
"types": "types/index.d.ts",
3131
"dependencies": {
3232
"@types/parse5": "^5.0.0",
33-
"ccount": "^1.0.0",
3433
"hastscript": "^6.0.0",
3534
"property-information": "^5.0.0",
3635
"vfile": "^4.0.0",
36+
"vfile-location": "^3.2.0",
3737
"web-namespaces": "^1.0.0"
3838
},
3939
"devDependencies": {

0 commit comments

Comments
 (0)