-
Notifications
You must be signed in to change notification settings - Fork 24
/
index.coffee
95 lines (76 loc) · 2.59 KB
/
index.coffee
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
'use strict'
fs = require 'fs'
through2 = require 'through2'
Args = require 'args-js' # main entry missing in `args-js` package
coffeelint = require 'coffeelint'
configfinder = require 'coffeelint/lib/configfinder'
# `reporter`
reporter = require './lib/reporter'
# common utils
{isLiterate, createPluginError, formatOutput} = require './lib/utils'
coffeelintPlugin = ->
# params for `args-js`
params = [
{optFile: Args.STRING | Args.Optional}
{opt: Args.OBJECT | Args.Optional}
{literate: Args.BOOL | Args.Optional}
{rules: Args.ARRAY | Args.Optional, _default: []}
]
# parse arguments
try
{opt, optFile, literate, rules} = Args params, arguments
catch e
throw createPluginError e
# sadly an `Args.OBJECT` maybe an `Array`
# e.g. `coffeelintPlugin [-> myCustomRule]`
if Array.isArray opt
rules = opt
opt = undefined
# register custom rules
rules.map (rule) ->
if typeof rule isnt 'function'
throw createPluginError(
"Custom rules need to be of type function, not #{typeof rule}"
)
coffeelint.registerRule rule
if toString.call(optFile) is '[object String]'
try
opt = JSON.parse fs.readFileSync(optFile).toString()
catch e
throw createPluginError "Could not load config from file: #{e}"
through2.obj (file, enc, cb) ->
# `file` specific options
fileOpt = opt
fileLiterate = literate
results = null
output = null
# pass along
if file.isNull()
@push file
return cb()
if file.isStream()
@emit 'error', createPluginError 'Streaming not supported'
return cb()
# if `opt` is not already a JSON `Object`,
# get config like `coffeelint` cli does.
fileOpt = configfinder.getConfig file.path if fileOpt is undefined
# if `literate` is not given
# check for file extension like
# `coffeelint`cli does.
fileLiterate = isLiterate(file.path) if fileLiterate is undefined
# get results `Array`
# see http://www.coffeelint.org/#api
# for format
errorReport = coffeelint.getErrorReport()
errorReport.lint(
file.relative,
file.contents.toString(),
fileOpt,
fileLiterate
)
output = formatOutput errorReport, fileOpt, fileLiterate
file.coffeelint = output
@push file
cb()
coffeelintPlugin.reporter = reporter
module.exports = coffeelintPlugin