-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathlinter-js-standard.js
120 lines (99 loc) · 3.47 KB
/
linter-js-standard.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
118
119
120
// Dependencies
var path = require('path')
var linter = require('./utils/linter')
var allowUnsafeNewFunction = require('loophole').allowUnsafeNewFunction
var markdownSplitter = require('./utils/markdown-splitter')
var htmlSplitter = require('./utils/html-splitter')
var selectStyle = require('./utils/select-style')
var styleSettings = require('./utils/style-settings')
function generateLintPromise (textEditor, filePath, fileContent, settings, lineStart) {
return allowUnsafeNewFunction(function () {
return new Promise((resolve, reject) => {
var boundLinter = linter.bind({
textEditor: textEditor,
filePath: filePath,
lineStart: lineStart || 0
})
var workingDirectory = path.dirname(filePath)
var previousWorkingDirectory = process.cwd()
process.chdir(workingDirectory)
settings.style.lintText(fileContent, settings.opts, (err, results) => {
try {
process.chdir(previousWorkingDirectory)
} catch (error) {
// Ignore error if directory does not exist.
if (error.code !== 'ENOENT') {
throw error
}
}
if (err) {
return reject(err)
}
resolve(boundLinter(results))
})
})
})
}
module.exports = function (textEditor) {
var fileContent = textEditor.getText()
var filePath = textEditor.getPath()
var fileScope = textEditor.getGrammar().scopeName
var config = atom.config.get('linter-js-standard')
if (!filePath) {
return null
}
var opts = styleSettings.checkStyleSettings(filePath, textEditor)
opts.cwd = path.dirname(filePath)
opts.filename = filePath
var style = selectStyle(filePath, {
style: opts.style || config.style,
checkStyleDevDependencies: config.checkStyleDevDependencies,
checkForEslintConfig: config.checkForEslintConfig
})
var settings = { style, opts }
if (!config.lintHtmlFiles && /^text\.html/.test(fileScope)) {
return []
}
if (!config.lintMarkdownFiles && fileScope === 'source.gfm') {
return []
}
// No style selected
if (settings.style.cmd === 'no-style' || typeof settings.style.lintText !== 'function') {
return []
}
var lintPromises = []
if (fileScope === 'source.gfm') {
var fencedCodeBlocks = markdownSplitter(fileContent)
fencedCodeBlocks.forEach(function (block) {
lintPromises.push(generateLintPromise(textEditor, filePath, block.content, settings, block.line))
})
} else if (/^text\.html/.test(fileScope)) {
var scriptCodeBlocks = htmlSplitter(fileContent)
scriptCodeBlocks.forEach(function (block) {
lintPromises.push(generateLintPromise(textEditor, filePath, block.content, settings, block.line))
})
} else {
lintPromises.push(generateLintPromise(textEditor, filePath, fileContent, settings))
}
return Promise.all(lintPromises)
.then(function (values) {
if (textEditor.getText() !== fileContent) {
// When the editor’s contents have been modified since we’ve started
// linting, we can’t be sure that the results are still valid.
// Therefore, we simply return `null` as not to update the results.
return null
}
var occurrences = []
values.forEach(function (value) {
occurrences = occurrences.concat(value)
})
return occurrences
})
.catch(function (err) {
atom.notifications.addError('Something bad happened', {
detail: err.message,
dismissable: true
})
return null
})
}