Skip to content

Commit 6fcbe6e

Browse files
committed
Merge pull request #81 from rhyzx/preserve-linenumber
Preserve line number for linters
2 parents b71a70a + 6f3270d commit 6fcbe6e

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

lib/parser.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,38 @@ module.exports = function (content) {
7070

7171
var start = node.childNodes[0].__location.start
7272
var end = node.childNodes[node.childNodes.length - 1].__location.end
73+
74+
var result
75+
if (type === 'script') {
76+
result = commentScript(content.slice(0, start)) +
77+
content.slice(start, end) +
78+
commentScript(content.slice(end))
79+
} else {
80+
result = content.slice(start, end).trim()
81+
}
82+
7383
output[type].push({
7484
lang: lang,
7585
scoped: scoped,
76-
content: content.substring(start, end).trim()
86+
content: result
7787
})
7888
})
7989

8090
cb(null, 'module.exports = ' + JSON.stringify(output))
8191
}
8292

93+
function commentScript (content) {
94+
return content.split(/\n\r|\n|\r/g)
95+
.map(function (line) {
96+
if (line.trim() === '') {
97+
return line
98+
} else {
99+
return '// ' + line
100+
}
101+
})
102+
.join('\n')
103+
}
104+
83105
function getAttribute (node, name) {
84106
if (node.attrs) {
85107
var i = node.attrs.length

test/test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ describe('vue-loader', function () {
141141
getFile('test.build.js.map', function (map) {
142142
var smc = new SourceMapConsumer(JSON.parse(map))
143143
getFile('test.build.js', function (code) {
144+
console.log(code)
144145
var line
145146
code.split('\n').some(function (l, i) {
146147
if (l.indexOf('Hello from Component A') > -1) {
@@ -153,7 +154,7 @@ describe('vue-loader', function () {
153154
column: 0
154155
})
155156
expect(pos.source.indexOf('webpack:///test/fixtures/basic.vue') > -1)
156-
expect(pos.line).to.equal(4)
157+
expect(pos.line).to.equal(15)
157158
done()
158159
})
159160
})

0 commit comments

Comments
 (0)