Skip to content

Commit

Permalink
Add support for linting .ts and .tsx files
Browse files Browse the repository at this point in the history
Connects to #3

change-type: minor
  • Loading branch information
LucianBuzzo committed Dec 21, 2017
1 parent d006858 commit 960e2fe
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 23 deletions.
20 changes: 20 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# editorconfig.org
root = true

[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false

[package.json]
indent_style = space
indent_size = 2

[.babelrc]
indent_style = space
indent_size = 2
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
resin-lint
==========

`resin-lint` is a linter based on [coffeelint](https://github.com/clutchski/coffeelint) and
[coffeescope2](https://github.com/za-creature/coffeescope) that detects style errors based on Resin.io coding guidelines.
`resin-lint` is a linter based on [coffeelint](https://github.com/clutchski/coffeelint),
[coffeescope2](https://github.com/za-creature/coffeescope) and [tslint](https://palantir.github.io/tslint/) that detects style errors based on Resin.io coding guidelines.

Overview
--------

`resin-lint` uses Resin's `coffeelint.json`. If a `coffeelint.json` is found in the to-be-linted project
`resin-lint` uses Resin's `coffeelint.json` and `tslint.json`. If a `coffeelint.json` or `tslint.json` is found in the to-be-linted project
directory or its parents then the rules found in it will override the default `resin-lint` ones.
Another way to to override the default resin-lint rules is by specifying a coffeelint configuration
Another way to to override the default resin-lint rules is by specifying a configuration
file with the `-f` parameter.
By default, only `.coffee` files will be linted. `.ts` and `.tsx` files can be
linted by using the `--typescript` parameter.

Usage
-----
Expand Down
49 changes: 49 additions & 0 deletions config/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"extends": "tslint:recommended",
"rules": {
"indent": [ true, "tabs" ],
"jsdoc-format": true,
"whitespace": [
false,
"check-type"
],
"object-literal-sort-keys": false,
"only-arrow-functions": [false],
"quotemark": [true, "single", "avoid-escape", "jsx-double"],
"max-classes-per-file": [false],
"max-line-length": [180],
"member-access": false,
"member-ordering": [false],
"no-consecutive-blank-lines": false,
"no-shadowed-variable": false,
"no-var-requires": true,
"arrow-parens": false,
"no-angle-bracket-type-assertion": false,
"no-console": [false],
"no-empty-interface": false,
"no-string-literal": false,
"object-literal-key-quotes": [true, "as-needed"],
"interface-name": [false],
"interface-over-type-literal": false,
"variable-name": [
true,
"ban-keywords",
"check-format",
"allow-leading-underscore",
"allow-pascal-case"
],
"semicolon": [true, "always", "ignore-bound-class-methods"],
"trailing-comma": [
true,
{
"multiline": {
"objects": "always",
"arrays": "always",
"functions": "always",
"typeLiterals": "ignore"
},
"esSpecCompliant": true
}
]
}
}
66 changes: 49 additions & 17 deletions lib/resin-lint.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ merge = require('merge')
optimist = require('optimist')
coffeelint = require('coffeelint')
reporter = require('coffeelint/lib/reporters/default')
tslint = require('tslint')

CONFIG_PATH = path.join(__dirname, '../config/coffeelint.json')
CS_CONFIG_PATH = path.join(__dirname, '../config/coffeelint.json')
TS_CONFIG_PATH = path.join(__dirname, '../config/tslint.json')

# The linter expects the path to actual source files, for example:
# src/
Expand Down Expand Up @@ -44,35 +46,50 @@ parseJSON = (file) ->
console.error("Could not parse #{file}")
throw err

findCoffeeScriptFiles = (paths = []) ->
findFiles = (extensions, paths = []) ->
files = []
for p in paths
if fs.statSync(p).isDirectory()
files = files.concat(glob.sync("#{p}/**/*.coffee"))
files = files.concat(glob.sync("#{p}/**/*.@(#{extensions.join('|')})"))
else
files.push(p)
files.map((p) -> path.join(p))

lintFiles = (files, config) ->
lintCoffeeFiles = (files, config) ->
errorReport = new coffeelint.getErrorReport()
for file in files
source = read(file)
errorReport.lint(file, source, config)
return errorReport

lintTsFiles = (files, config) ->
parsedConfig = tslint.Configuration.parseConfigFile(config)
linter = new tslint.Linter(
fix: false
formatter: 'stylish'
)
for file in files
source = read(file)
linter.lint(file, source, parsedConfig)
return linter.getResult()


module.exports = (passed_params) ->
try
options = optimist(passed_params)
.usage('Usage: resin-lint [options] [...]')
.describe('f', 'Specify a coffeelint config file to override resin-lint rules')
.describe('p', 'Print default resin-lint coffeelint.json')
.describe('i', 'Ignore coffeelint.json files in project directory and its parents')
.describe('typescript', 'Lint typescript files instead of coffeescript')
.boolean('u', 'Run unused import check')

if options.argv._.length < 1 and not options.argv.p
options.showHelp()
process.exit(1)

configPath = if options.argv.typescript then TS_CONFIG_PATH else CS_CONFIG_PATH

Promise.try ->
if options.argv.u
return Promise.map options.argv._, (dir) ->
Expand All @@ -96,29 +113,44 @@ module.exports = (passed_params) ->
console.log()
.then ->
if options.argv.p
console.log(fs.readFileSync(CONFIG_PATH).toString())
console.log(fs.readFileSync(configPath).toString())
process.exit(0)

config = parseJSON(CONFIG_PATH)
config = parseJSON(configPath)

if options.argv.f
configOverridePath = fs.realpathSync(options.argv.f)

configOverridePath ?= findFile('coffeelint.json') if not options.argv.i
if not options.argv.i
configFileName = if options.argv.typescript then 'tslint.json' else 'coffeelint.json'
configOverridePath ?= findFile(configFileName)
if configOverridePath
# Override default config
configOverride = parseJSON(configOverridePath)
config = merge(config, configOverride)
config = merge.recursive(config, configOverride)

paths = options.argv._
scripts = findCoffeeScriptFiles(paths)

errorReport = lintFiles(scripts, config)
report = new reporter errorReport,
colorize: process.stdout.isTTY
quiet: false
report.publish()
process.on 'exit', ->
process.exit(errorReport.getExitCode())

if options.argv.typescript
scripts = findFiles(['ts', 'tsx'], paths)
errorReport = lintTsFiles(scripts, config)

console.log(errorReport.output)

process.on 'exit', ->
process.exit(errorReport.errorCount > 0 : 1 : 0)

else
scripts = findFiles(['coffee'], paths)
errorReport = lintCoffeeFiles(scripts, config)

report = new reporter errorReport,
colorize: process.stdout.isTTY
quiet: false
report.publish()

process.on 'exit', ->
process.exit(errorReport.getExitCode())

catch err
console.log(err.stack)
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"url": "[email protected]:resin-io/node-resin-lint"
},
"scripts": {
"test": "./bin/resin-lint test/",
"test": "./bin/resin-lint test/ && ./bin/resin-lint test/ --typescript",
"prepublish": "npm run lint lib/",
"lint": "./bin/resin-lint"
},
Expand All @@ -29,7 +29,9 @@
"depcheck": "^0.6.7",
"glob": "^7.0.3",
"merge": "^1.2.0",
"optimist": "^0.6.1"
"optimist": "^0.6.1",
"tslint": "^5.8.0",
"typescript": "^2.6.2"
},
"devDependencies": {}
}
8 changes: 8 additions & 0 deletions test/lintme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const fn = () => ({
a: 1,
b: 2,
});

const { a, b } = fn();

console.log(a, b);

0 comments on commit 960e2fe

Please sign in to comment.