-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
spalger
committed
May 16, 2018
1 parent
45bf3c2
commit 6ec4110
Showing
25 changed files
with
453 additions
and
54 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
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 interface IToolingLog extends Readable { | ||
verbose(...args: any[]): void; | ||
debug(...args: any[]): void; | ||
info(...args: any[]): void; | ||
success(...args: any[]): void; | ||
warning(...args: any[]): void; | ||
error(errOrMsg: string | Error): void; | ||
write(...args: any[]): void; | ||
indent(spaces: number): void; | ||
getLevel(): LogLevel; | ||
setLevel(level: LogLevel): void; | ||
} | ||
|
||
export function createToolingLog(level?: LogLevel): IToolingLog; |
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,18 @@ | ||
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 |
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(); |
File renamed without changes.
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 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,58 @@ | ||
import { dirname, extname, join, relative, resolve } from 'path'; | ||
|
||
import { REPO_ROOT } from './constants'; | ||
|
||
export class File { | ||
private path: string; | ||
private relativePath: string; | ||
private ext: string; | ||
|
||
constructor(path: string) { | ||
this.path = resolve(path); | ||
this.relativePath = relative(REPO_ROOT, this.path); | ||
this.ext = extname(this.path); | ||
} | ||
|
||
public getAbsolutePath() { | ||
return this.path; | ||
} | ||
|
||
public getRelativePath() { | ||
return this.relativePath; | ||
} | ||
|
||
public isJs() { | ||
return this.ext === '.js'; | ||
} | ||
|
||
public isTypescript() { | ||
return this.ext === '.ts' || this.ext === '.tsx'; | ||
} | ||
|
||
public getRelativeParentDirs() { | ||
const parents: string[] = []; | ||
|
||
while (true) { | ||
// NOTE: resolve() produces absolute paths, so we have to use join() | ||
const parent = parents.length | ||
? join(parents[parents.length - 1], '..') | ||
: dirname(this.relativePath); | ||
|
||
if (parent === '..' || parent === '.') { | ||
break; | ||
} else { | ||
parents.push(parent); | ||
} | ||
} | ||
|
||
return parents; | ||
} | ||
|
||
public toString() { | ||
return this.relativePath; | ||
} | ||
|
||
public toJSON() { | ||
return this.relativePath; | ||
} | ||
} |
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,72 @@ | ||
import { CLIEngine } from 'eslint'; | ||
import { run } from 'tslint/lib/runner'; | ||
|
||
import { IToolingLog } from '@kbn/dev-utils'; | ||
import { REPO_ROOT } from '../constants'; | ||
import { File } from '../file'; | ||
import { createFailError } from '../run'; | ||
import { getTsProjects, Project } from './projects'; | ||
|
||
function groupFilesByProject(files: File[]) { | ||
const projects = getTsProjects(); | ||
const filesByProject: Map<Project, File[]> = new Map(); | ||
|
||
files.forEach(file => { | ||
const project = projects.find(p => p.isFileSelected(file)); | ||
|
||
if (!project) { | ||
throw createFailError( | ||
`[tslint] ${file.getRelativePath()} is not a part of any project`, | ||
1 | ||
); | ||
} | ||
|
||
if (filesByProject.has(project)) { | ||
filesByProject.get(project)!.push(file); | ||
} else { | ||
filesByProject.set(project, [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: IToolingLog, 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.getTsConfigPath(), | ||
}, | ||
{ | ||
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.getName(), | ||
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,38 @@ | ||
import { IToolingLog } from '@kbn/dev-utils'; | ||
|
||
import { Minimatch } from 'minimatch'; | ||
import { File } from '../file'; | ||
import { createFailError } from '../run'; | ||
import { getTsProjects } from './projects'; | ||
|
||
export function pickFilesToLint(log: IToolingLog, files: File[]) { | ||
const projects = getTsProjects(); | ||
|
||
const filesNotInProjects: File[] = []; | ||
const filesToLint = files.filter(file => { | ||
if (!file.isTypescript()) { | ||
return false; | ||
} | ||
|
||
const project = projects.find(p => p.isFileSelected(file)); | ||
|
||
if (!project) { | ||
filesNotInProjects.push(file); | ||
log.error( | ||
`[tslint] ${file.getRelativePath()} is not a part of any TypeScript project.` | ||
); | ||
return false; | ||
} | ||
|
||
return true; | ||
}); | ||
|
||
if (filesNotInProjects.length) { | ||
throw createFailError( | ||
`[tslint] Ensure that all files are selected by a tsconfig.json file, and that all tsconfig.json files are litsted in "src/dev/tslint/projects.ts"`, | ||
1 | ||
); | ||
} | ||
|
||
return filesToLint; | ||
} |
Oops, something went wrong.