Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adds support for linting TypeScript files * Re-implements lintText(), lintTextSync() and lintFiles() methods * Inline the fancy result output logic and update relevant tests
- Loading branch information
1 parent
6f2b8b5
commit 6d9d181
Showing
18 changed files
with
315 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module.exports = { | ||
plugins: ['typescript'], | ||
rules: { | ||
'no-undef': 'off', | ||
'no-use-before-define': 'off', | ||
'import/imports-first': 'off', | ||
'import/first': 'off', | ||
'typescript/adjacent-overload-signatures': 'error', | ||
'typescript/class-name-casing': 'error', | ||
'typescript/explicit-member-accessibility': 'error', | ||
'typescript/interface-name-prefix': 'error', | ||
'typescript/member-delimiter-style': ['error', {delimiter: 'none', requireLast: true, ignoreSingleLine: true}], | ||
'typescript/member-ordering': 'error', | ||
'typescript/no-angle-bracket-type-assertion': 'error', | ||
'typescript/no-empty-interface': 'error', | ||
'typescript/no-namespace': ['error', {allowDefinitionFiles: true}], | ||
'typescript/no-parameter-properties': 'error', | ||
'typescript/no-triple-slash-reference': 'error', | ||
'typescript/no-unused-vars': 'error', | ||
'typescript/no-use-before-define': ['error', {functions: false, classes: false, variables: false, typedefs: false}], | ||
'typescript/prefer-namespace-keyword': 'error', | ||
'typescript/type-annotation-spacing': 'error' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,3 @@ | ||
#!/usr/bin/env node | ||
|
||
'use strict' | ||
|
||
require('./lib/patch-file-result-output')( | ||
require('standard-engine'), console | ||
).cli(require('./options')) | ||
require('./lib/patched-standard-engine').cli(require('./options')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
'use strict' | ||
|
||
module.exports = require('standard-engine').linter(require('./options')) | ||
const Linter = require('./lib/Linter') | ||
module.exports = new Linter(require('./options')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
'use strict' | ||
|
||
const deglob = require('deglob') | ||
const formatter = require('eslint-formatter-pretty') | ||
const hasFlag = require('has-flag') | ||
const engine = require('standard-engine') | ||
|
||
const DEFAULT_PATTERNS = [ | ||
'**/*.js', | ||
'**/*.jsx', | ||
'**/*.d.ts', | ||
'**/*.ts', | ||
'**/*.tsx' | ||
] | ||
|
||
class Linter extends engine.linter { | ||
parseTypescriptOpts (opts, usePackageJson) { | ||
return this.parseOpts(Object.assign({}, opts, {parser: 'typescript-eslint-parser'}), usePackageJson) | ||
} | ||
|
||
lintTextSync (text, opts) { | ||
if (opts.filename && opts.filename.endsWith('.ts')) { | ||
opts = this.parseTypescriptOpts(opts, false) | ||
} else { | ||
opts = this.parseOpts(opts, false) | ||
} | ||
return new this.eslint.CLIEngine(opts.eslintConfig).executeOnText(text, opts.filename) | ||
} | ||
|
||
lintText (text, opts, cb) { | ||
try { | ||
const report = this.lintTextSync(text, opts) | ||
process.nextTick(cb, null, report) | ||
} catch (err) { | ||
process.nextTick(cb, err) | ||
} | ||
} | ||
|
||
lintFiles (patterns, opts, cb) { | ||
if (typeof opts === 'function') return this.lintFiles(patterns, null, opts) | ||
|
||
const regularOpts = this.parseOpts(opts, true) | ||
|
||
if (typeof patterns === 'string') patterns = [patterns] | ||
if (patterns.length === 0) patterns = DEFAULT_PATTERNS | ||
|
||
deglob(patterns, { | ||
ignore: regularOpts.ignore, | ||
cwd: regularOpts.cwd, | ||
useGitIgnore: true, | ||
usePackageJson: false | ||
}, (globErr, allFiles) => { | ||
if (globErr) return cb(globErr) | ||
|
||
const files = allFiles.filter(file => !file.endsWith('.ts')) | ||
const tsFiles = allFiles.filter(file => file.endsWith('.ts')) | ||
|
||
let report | ||
try { | ||
report = new this.eslint.CLIEngine(regularOpts.eslintConfig).executeOnFiles(files) | ||
} catch (err) { | ||
return cb(err) | ||
} | ||
|
||
let tsReport | ||
if (tsFiles.length > 0) { | ||
try { | ||
const tsOpts = this.parseTypescriptOpts(opts, true) | ||
tsReport = new this.eslint.CLIEngine(tsOpts.eslintConfig).executeOnFiles(tsFiles) | ||
} catch (err) { | ||
return cb(err) | ||
} | ||
} | ||
|
||
const combined = tsReport | ||
? { | ||
results: report.results.concat(tsReport.results), | ||
errorCount: report.errorCount + tsReport.errorCount, | ||
warningCount: report.warningCount + tsReport.warningCount, | ||
fixableErrorCount: report.fixableErrorCount + tsReport.fixableErrorCount, | ||
fixableWarningCount: report.fixableWarningCount + tsReport.fixableWarningCount | ||
} | ||
: report | ||
|
||
if (regularOpts.fix) { | ||
this.eslint.CLIEngine.outputFixes(combined) | ||
} | ||
|
||
Object.defineProperty(combined.results, 'forEach', { | ||
enumerable: false, | ||
value () { | ||
const output = formatter(this.slice()) | ||
if (hasFlag('stdin') && regularOpts.fix) { | ||
console.error(output) | ||
} else { | ||
console.log(output) | ||
} | ||
} | ||
}) | ||
|
||
cb(null, combined) | ||
}) | ||
} | ||
} | ||
module.exports = Linter |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict' | ||
|
||
const engine = require('standard-engine') | ||
const Linter = require('./Linter') | ||
|
||
engine.linter = opts => new Linter(opts) | ||
module.exports = engine |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use strict' | ||
|
||
const config = require('../.typescript.eslintrc') | ||
|
||
function mergeTypescriptConfig (eslintConfig) { | ||
for (const plugin of config.plugins) { | ||
eslintConfig.plugins.push(plugin) | ||
} | ||
Object.assign(eslintConfig.rules, config.rules) | ||
|
||
if (!eslintConfig.baseConfig) eslintConfig.baseConfig = {} | ||
const baseConfig = eslintConfig.baseConfig | ||
if (!baseConfig.settings) baseConfig.settings = {} | ||
const settings = baseConfig.settings | ||
if (!settings['import/resolver']) settings['import/resolver'] = {} | ||
const resolver = settings['import/resolver'] | ||
resolver['eslint-import-resolver-typescript'] = true | ||
} | ||
module.exports = mergeTypescriptConfig |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.