Skip to content

Commit c3150da

Browse files
committed
Fix: the problem about \r\n (fixes #4)
- The `SAXParser` of `parse5` removes `\r` characters. So the `scriptText` had been shorter than original script text.
1 parent 483e006 commit c3150da

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,16 @@ function extractFirstScript(originalText) {
9898
parser.stop()
9999
}
100100
})
101-
parser.on("text", (scriptText, location) => {
101+
parser.on("text", (_, location) => {
102102
if (startToken != null) {
103+
const start = location.startOffset
103104
const countLines = location.line - 1
104105
const lineTerminators = "\n".repeat(countLines)
105-
const spaces = " ".repeat(location.startOffset - countLines)
106+
const spaces = " ".repeat(start - countLines)
107+
const scriptText = originalText.slice(start, location.endOffset)
108+
106109
text = `${spaces}${lineTerminators}${scriptText}`
107-
offset = location.startOffset
110+
offset = start
108111
}
109112
})
110113
parser.end(originalText)

test/fixtures/crlf.vue

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<template>
2+
<p>{{ greeting }} World!</p>
3+
</template>
4+
5+
<script>
6+
/*eslint linebreak-style: [error, windows] */
7+
8+
module.exports = {
9+
data() {
10+
return {greeting: "Hello"}
11+
},
12+
}
13+
</script>
14+
15+
<style scoped>
16+
p {
17+
font-size: 2em;
18+
text-align: center;
19+
}
20+
</style>

test/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,26 @@ describe("About fixtures/lines-around-directive.vue", () => {
175175
assert(actual === expected)
176176
})
177177
})
178+
179+
describe("About fixtures/crlf.vue", () => {
180+
beforeEach(() => {
181+
fs.copySync(ORIGINAL_FIXTURE_DIR, FIXTURE_DIR)
182+
})
183+
afterEach(() => {
184+
fs.removeSync(FIXTURE_DIR)
185+
})
186+
187+
it("should notify no 'indent' error", () => {
188+
const cli = new CLIEngine({
189+
cwd: FIXTURE_DIR,
190+
envs: ["es6", "node"],
191+
parser: PARSER_PATH,
192+
rules: {indent: "error"},
193+
useEslintrc: false,
194+
})
195+
const report = cli.executeOnFiles(["crlf.vue"])
196+
const messages = report.results[0].messages
197+
198+
assert(messages.length === 0)
199+
})
200+
})

0 commit comments

Comments
 (0)