-
Notifications
You must be signed in to change notification settings - Fork 8.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[tslint] lint typescript code #19105
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
321ed6e
[tslint] lint typescript code
890af3f
[tslint] filter projects when running specific project
e6fb646
[dev/ts] use more explicit types
da98caf
[dev/ts] add note about why using glob
2c0e40d
[dev/ts] rely on ts, use fewer getters
10f53c1
Merge branch 'master' of github.com:elastic/kibana into implement/tslint
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,18 @@ | ||
import { Readable } from 'stream'; | ||
|
||
type LogLevel = 'silent' | 'error' | 'warning' | 'info' | 'debug' | 'verbose'; | ||
|
||
export class ToolingLog extends Readable { | ||
public verbose(...args: any[]): void; | ||
public debug(...args: any[]): void; | ||
public info(...args: any[]): void; | ||
public success(...args: any[]): void; | ||
public warning(...args: any[]): void; | ||
public error(errOrMsg: string | Error): void; | ||
public write(...args: any[]): void; | ||
public indent(spaces: number): void; | ||
public getLevel(): LogLevel; | ||
public setLevel(level: LogLevel): void; | ||
} | ||
|
||
export function createToolingLog(level?: LogLevel): ToolingLog; |
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,6 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"include": [ | ||
"index.d.ts" | ||
], | ||
} |
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 @@ | ||
extends: ../../tslint.yaml | ||
|
||
rules: | ||
max-classes-per-file: false | ||
interface-name: false | ||
variable-name: false | ||
no-empty: false | ||
object-literal-sort-keys: false | ||
member-ordering: false | ||
no-console: false | ||
only-arrow-functions: false | ||
no-shadowed-variable: false | ||
no-empty-interface: false | ||
ordered-imports: false | ||
interface-over-type-literal: false | ||
prettier: false | ||
prefer-const: false | ||
member-access: false | ||
no-unused-variable: false |
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,14 @@ | ||
extends: ../../tslint.yaml | ||
|
||
rules: | ||
max-classes-per-file: false | ||
interface-name: false | ||
variable-name: false | ||
no-empty: false | ||
object-literal-sort-keys: false | ||
member-ordering: false | ||
member-access: false | ||
ordered-imports: false | ||
interface-over-type-literal: false | ||
array-type: false | ||
prefer-const: false |
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,2 @@ | ||
require('../src/babel-register'); | ||
require('../src/dev/tslint').runTslintCli(); |
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
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
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 @@ | ||
export function createFailError(msg: string, exitCode: number): 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,17 @@ | ||
import { run } from './run'; | ||
|
||
import { lintFiles, pickFilesToLint } from './eslint'; | ||
import * as Eslint from './eslint'; | ||
import * as Tslint from './tslint'; | ||
import { getFilesForCommit, checkFileCasing } from './precommit_hook'; | ||
|
||
run(async ({ log }) => { | ||
const files = await getFilesForCommit(); | ||
await checkFileCasing(log, files); | ||
await lintFiles(log, pickFilesToLint(log, files)); | ||
|
||
for (const Linter of [Eslint, Tslint]) { | ||
const filesToLint = Linter.pickFilesToLint(log, files); | ||
if (filesToLint.length > 0) { | ||
await Linter.lintFiles(log, filesToLint); | ||
} | ||
} | ||
}); |
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,3 @@ | ||
export { runTslintCli } from './run_tslint_cli'; | ||
export { lintFiles } from './lint_files'; | ||
export { pickFilesToLint } from './pick_files_to_lint'; |
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,63 @@ | ||
import { run } from 'tslint/lib/runner'; | ||
|
||
import { ToolingLog } from '@kbn/dev-utils'; | ||
import { File } from '../file'; | ||
import { createFailError } from '../run'; | ||
import { getTsProjectForAbsolutePath, Project } from '../typescript'; | ||
|
||
function groupFilesByProject(files: File[]) { | ||
const filesByProject: Map<Project, File[]> = new Map(); | ||
|
||
files.forEach(file => { | ||
const project = getTsProjectForAbsolutePath(file.getAbsolutePath()); | ||
const filesForProject = filesByProject.get(project); | ||
|
||
if (!filesForProject) { | ||
filesByProject.set(project, [file]); | ||
} else { | ||
filesForProject.push(file); | ||
} | ||
}); | ||
|
||
return filesByProject; | ||
} | ||
|
||
/** | ||
* Lints a list of files with eslint. eslint reports are written to the log | ||
* and a FailError is thrown when linting errors occur. | ||
* | ||
* @param {ToolingLog} log | ||
* @param {Array<File>} files | ||
* @return {undefined} | ||
*/ | ||
export async function lintFiles(log: ToolingLog, files: File[]) { | ||
for (const [project, filesInProject] of groupFilesByProject(files)) { | ||
const exitCode = await run( | ||
{ | ||
exclude: [], | ||
files: filesInProject.map(f => f.getAbsolutePath()), | ||
fix: false, | ||
format: 'stylish', | ||
project: project.tsConfigPath, | ||
}, | ||
{ | ||
log(m: string) { | ||
log.write(m); | ||
}, | ||
error(m: string) { | ||
log.error(m); | ||
}, | ||
} | ||
); | ||
|
||
if (exitCode > 0) { | ||
throw createFailError(`[tslint] failure`, 1); | ||
} else { | ||
log.success( | ||
'[tslint/%s] %d files linted successfully', | ||
project.name, | ||
files.length | ||
); | ||
} | ||
} | ||
} |
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 @@ | ||
import { ToolingLog } from '@kbn/dev-utils'; | ||
|
||
import { File } from '../file'; | ||
|
||
export function pickFilesToLint(log: ToolingLog, files: File[]) { | ||
return files.filter(file => file.isTypescript()); | ||
} |
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,29 @@ | ||
const Lint = require('tslint'); | ||
|
||
const FAILURE_STRING = 'File must start with a license header'; | ||
const RULE_NAME = 'require-license-header'; | ||
|
||
exports.Rule = class extends Lint.Rules.AbstractRule { | ||
apply(sourceFile) { | ||
const [headerText] = this.getOptions().ruleArguments; | ||
|
||
if (!headerText) { | ||
throw new Error(`${RULE_NAME} requires a single argument containing the header text`); | ||
} | ||
|
||
if (sourceFile.text.startsWith(headerText)) { | ||
return []; | ||
} | ||
|
||
return [ | ||
new Lint.RuleFailure( | ||
sourceFile, | ||
0, | ||
0, | ||
FAILURE_STRING, | ||
RULE_NAME, | ||
new Lint.Replacement(0, 0, `${headerText}\n\n`) | ||
) | ||
]; | ||
} | ||
}; |
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,69 @@ | ||
import { resolve } from 'path'; | ||
|
||
import { createToolingLog } from '@kbn/dev-utils'; | ||
import chalk from 'chalk'; | ||
import execa from 'execa'; | ||
import getopts from 'getopts'; | ||
import Listr from 'listr'; | ||
|
||
import { Project, PROJECTS } from '../typescript'; | ||
|
||
class LintFailure { | ||
constructor(public project: Project, public error: execa.ExecaError) {} | ||
} | ||
|
||
export function runTslintCli() { | ||
const log = createToolingLog('info'); | ||
log.pipe(process.stdout); | ||
|
||
const opts = getopts(process.argv.slice(2)); | ||
|
||
if (!opts.format) { | ||
process.argv.push('--format', 'stylish'); | ||
} | ||
|
||
const list = new Listr( | ||
PROJECTS.filter(project => { | ||
if (!opts.project) { | ||
return true; | ||
} | ||
|
||
return resolve(opts.project) === project.tsConfigPath; | ||
}).map(project => ({ | ||
task: () => | ||
execa( | ||
'tslint', | ||
[...process.argv.slice(2), '--project', project.tsConfigPath], | ||
{ | ||
cwd: project.directory, | ||
env: chalk.enabled ? { FORCE_COLOR: 'true' } : {}, | ||
stdio: ['ignore', 'pipe', 'pipe'], | ||
} | ||
).catch(error => { | ||
throw new LintFailure(project, error); | ||
}), | ||
title: project.name, | ||
})), | ||
{ | ||
concurrent: true, | ||
exitOnError: false, | ||
} | ||
); | ||
|
||
list.run().catch((error: any) => { | ||
if (!error.errors) { | ||
log.error('Unhandled execption!'); | ||
log.error(error); | ||
process.exit(1); | ||
} | ||
|
||
for (const e of error.errors) { | ||
if (e instanceof LintFailure) { | ||
log.write(''); | ||
log.error(`${e.project.name} failed\n${e.error.stdout}`); | ||
} else { | ||
log.error(e); | ||
} | ||
} | ||
}); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This current implementation doesn't attempt to run tslint at all if there is an error from eslint, so if you have lint failures in both .js and .ts files, you won't know that until you attempt to commit again after fixing the .js files. It should probably run both commands and show the results from each.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, the same is true for file casing issues and eslint. If there are casing issues eslint is never run.
I'll put up a separate PR that updates the precommit hook to clearly log errors from multiple tasks and collect those errors into an appropriate exit code, but I personally think it's out of the scope of this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works for me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#19271