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 22, 2017
1 parent 17cf80d commit faa7406
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 27 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
}
]
}
}
87 changes: 66 additions & 21 deletions lib/resin-lint.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,19 @@ 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')
configurations =
coffeescript:
configPath: path.join(__dirname, '../config/coffeelint.json')
configFileName: 'coffeelint.json'
extensions: ['coffee']
lang: 'coffeescript'
typescript:
configPath: path.join(__dirname, '../config/tslint.json')
configFileName: 'tslint.json'
extensions: ['ts', 'tsx']
lang: 'typescript'

# The linter expects the path to actual source files, for example:
# src/
Expand Down Expand Up @@ -44,29 +55,65 @@ 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
errorReport

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

return errorReport.getExitCode()

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)

errorReport = linter.getResult()

console.log(linter.getResult().output)

return if errorReport.errorCount is 0 then 0 else 1

runLint = (resinLintConfig, paths, config) ->
scripts = findFiles(resinLintConfig.extensions, paths)

if resinLintConfig.lang is 'typescript'
linterExitCode = lintTsFiles(scripts, config)

if resinLintConfig.lang is 'coffeescript'
linterExitCode = lintCoffeeFiles(scripts, config)

process.on 'exit', ->
process.exit(linterExitCode)

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('f', 'Specify a linting config file to override resin-lint rules')
.describe('p', 'Print default resin-lint linting rules')
.describe('i', 'Ignore linting config 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
Expand Down Expand Up @@ -95,30 +142,28 @@ module.exports = (passed_params) ->
console.log('No unused dependencies!')
console.log()
.then ->

resinLintConfiguration = if options.argv.typescript then configurations.typescript else configurations.coffeescript

if options.argv.p
console.log(fs.readFileSync(CONFIG_PATH).toString())
console.log(fs.readFileSync(resinLintConfiguration.configPath).toString())
process.exit(0)

config = parseJSON(CONFIG_PATH)
config = parseJSON(resinLintConfiguration.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
configOverridePath ?= findFile(resinLintConfiguration.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())

runLint(resinLintConfiguration, paths, config)

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 faa7406

Please sign in to comment.