This repository was archived by the owner on May 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 311
Expand file tree
/
Copy pathlint-factory.ts
More file actions
105 lines (90 loc) · 2.82 KB
/
lint-factory.ts
File metadata and controls
105 lines (90 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { Configuration, Linter, LintResult } from 'tslint';
import { Program, getPreEmitDiagnostics, Diagnostic } from 'typescript';
import * as glob from 'glob';
import { BuildContext } from '../util/interfaces';
import { isObject } from 'util';
export interface LinterOptions {
typeCheck?: boolean;
}
export interface LinterConfig {
[key: string]: any;
}
/**
* Lint a file according to config
* @param {Linter} linter
* @param {LinterConfig} config
* @param {string} filePath
* @param {string} fileContents
*/
export function lint(linter: Linter, config: LinterConfig, filePath: string, fileContents: string): void {
linter.lint(filePath, fileContents, config as any);
}
/**
* Get the linter result
* @param {Linter} linter
* @return {LintResult}
*/
export function getLintResult(linter: Linter): LintResult {
return linter.getResult();
}
/**
* Type check a TS program
* @param {BuildContext} context
* @param {Program} program
* @param {LinterOptions} linterOptions
* @return {Promise<Diagnostic[]>}
*/
export function typeCheck(context: BuildContext, program: Program, linterOptions?: LinterOptions): Promise<Diagnostic[]> {
if (isObject(linterOptions) && linterOptions.typeCheck) {
return Promise.resolve(getPreEmitDiagnostics(program));
}
return Promise.resolve([]);
}
/**
* Create a TS program based on the BuildContext {rootDir} or TS config file path (if provided)
* @param {BuildContext} context
* @param {string} tsConfig
* @return {Program}
*/
export function createProgram(context: BuildContext, tsConfig: string): Program {
return Linter.createProgram(tsConfig, context.rootDir);
}
/**
* Get all files that are sourced in TS config
* @param {BuildContext} context
* @param {Program} program
* @param {Array<string>} excludePatterns
* @return {Array<string>}
*/
export function getFileNames(context: BuildContext, program: Program, excludePatterns?: string[]): string[] {
let files: string[] = Linter.getFileNames(program);
if (excludePatterns) {
const globOptions = { ignore: excludePatterns, nodir: true };
files = files
.map((file: string) => glob.sync(file, globOptions))
.reduce((a: string[], b: string[]) => a.concat(b), []);
}
return files;
}
/**
* Get lint configuration
* @param {string} tsLintConfig
* @param {LinterOptions} linterOptions
* @return {Linter}
*/
export function getTsLintConfig(tsLintConfig: string, linterOptions?: LinterOptions): LinterConfig {
const config = Configuration.loadConfigurationFromPath(tsLintConfig);
Object.assign(config, isObject(linterOptions) ? {linterOptions} : {});
return config;
}
/**
* Create a TS linter
* @param {BuildContext} context
* @param {Program} program
* @return {Linter}
*/
export function createLinter(context: BuildContext, program: Program): Linter {
return new Linter({
fix: false
}, program);
}