This repository was archived by the owner on Oct 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlinter.js
117 lines (99 loc) · 3.03 KB
/
linter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const walk = require('walk')
const fs = require('fs')
const path = require('path')
const htmlparser = require('htmlparser2')
const cheerio = require('cheerio')
const sassLint = require('sass-lint')
const Reporter = require('./reporter.js')
class Linter {
constructor (options) {
this.lintErrors = []
}
checkPaths (pathsToCheck) {
pathsToCheck.forEach((pathToCheck) => {
if (pathToCheck.substr(-3) === 'vue') {
this.lintFile(pathToCheck, this.walkerEndHandler.bind(this))
} else {
this.checkPath(pathToCheck)
}
})
}
checkPath (arg) {
const walker = walk.walk(arg, { followLinks: false })
walker.on('file', this.walkerFileHandler.bind(this))
walker.on('end', this.walkerEndHandler.bind(this))
walker.on('errors', this.walkerErrorsHandler.bind(this))
}
walkerErrorsHandler (root, nodeStatsArray, next) {
console.error('Linting Error: Invalid sass linting path.')
console.log(nodeStatsArray)
next()
}
walkerFileHandler (root, fileStat, next) {
const filename = `${root}/${fileStat.name}`
if (filename.substr(-3) !== 'vue') {
return next()
}
this.lintFile(path.resolve(root, fileStat.name), next)
}
lintFile (filePath, next) {
fs.readFile(filePath, (error, fileData) => {
if (error) {
return console.error(error)
}
const fileTemplates = this.extractFileTemplates(fileData)
fileTemplates.forEach((template) => {
const fileErrors = sassLint.lintText({
text: template.content,
filename: filePath,
format: template.format
})
if (fileErrors.messages.length) {
fileErrors.messages.forEach((message) => {
message.line += template.lineOffset
})
this.lintErrors = this.lintErrors.concat(fileErrors)
}
})
next()
})
}
walkerEndHandler () {
const reporter = new Reporter()
reporter.report(this.lintErrors)
}
extractFileTemplates (fileData) {
let templates = []
const handler = new htmlparser.DefaultHandler((error, dom) => {
if (error) {
return console.log(error)
}
const $ = cheerio.load(dom)
const scssTemplate = $('style[lang="scss"]').text()
const sassTemplate = $('style[lang="sass"]').text()
if (scssTemplate.length) {
templates.push({
content: scssTemplate,
format: 'scss',
lineOffset: this.getLineOffset(scssTemplate, $.text())
})
}
if (sassTemplate.length) {
templates.push({
content: sassTemplate,
format: 'sass',
lineOffset: this.getLineOffset(sassTemplate, $.text())
})
}
})
var parser = new htmlparser.Parser(handler)
parser.parseComplete(fileData)
return templates
}
getLineOffset (needle, haystack) {
const targetPosition = haystack.indexOf(needle)
const untilTargetPosition = haystack.substring(0, targetPosition)
return untilTargetPosition.split(/\r?\n/).length - 1
}
}
module.exports = Linter