1
- const Task = require ( '../ember-cli/lib/models/task' ) ;
2
1
import * as chalk from 'chalk' ;
2
+ import * as fs from 'fs' ;
3
3
import * as glob from 'glob' ;
4
+ import * as path from 'path' ;
4
5
import * as ts from 'typescript' ;
5
6
import { requireProjectModule } from '../utilities/require-project-module' ;
6
7
import { CliConfig } from '../models/config' ;
7
8
import { LintCommandOptions } from '../commands/lint' ;
8
9
10
+ const SilentError = require ( 'silent-error' ) ;
11
+ const Task = require ( '../ember-cli/lib/models/task' ) ;
12
+
9
13
interface CliLintConfig {
10
14
files ?: ( string | string [ ] ) ;
11
15
project ?: string ;
@@ -30,7 +34,12 @@ export default Task.extend({
30
34
31
35
const result = lintConfigs
32
36
. map ( ( config ) => {
33
- const program : ts . Program = Linter . createProgram ( config . project ) ;
37
+ let program : ts . Program ;
38
+ if ( config . project ) {
39
+ program = Linter . createProgram ( config . project ) ;
40
+ } else if ( commandOptions . typeCheck ) {
41
+ ui . writeLine ( chalk . yellow ( 'A "project" must be specified to enable type checking.' ) ) ;
42
+ }
34
43
const files = getFilesToLint ( program , config , Linter ) ;
35
44
const lintOptions = {
36
45
fix : commandOptions . fix ,
@@ -39,13 +48,21 @@ export default Task.extend({
39
48
const lintProgram = commandOptions . typeCheck ? program : undefined ;
40
49
const linter = new Linter ( lintOptions , lintProgram ) ;
41
50
51
+ let lastDirectory : string ;
52
+ let configLoad : any ;
42
53
files . forEach ( ( file ) => {
43
- const sourceFile = program . getSourceFile ( file ) ;
44
- if ( ! sourceFile ) {
54
+ const fileContents = getFileContents ( file , program ) ;
55
+ if ( ! fileContents ) {
45
56
return ;
46
57
}
47
- const fileContents = sourceFile . getFullText ( ) ;
48
- const configLoad = Configuration . findConfiguration ( config . tslintConfig , file ) ;
58
+
59
+ // Only check for a new tslint config if path changes
60
+ const currentDirectory = path . dirname ( file ) ;
61
+ if ( currentDirectory !== lastDirectory ) {
62
+ configLoad = Configuration . findConfiguration ( config . tslintConfig , file ) ;
63
+ lastDirectory = currentDirectory ;
64
+ }
65
+
49
66
linter . lint ( file , fileContents , configLoad . results ) ;
50
67
} ) ;
51
68
@@ -94,7 +111,7 @@ function getFilesToLint(program: ts.Program, lintConfig: CliLintConfig, Linter:
94
111
95
112
if ( lintConfig . files !== null ) {
96
113
files = Array . isArray ( lintConfig . files ) ? lintConfig . files : [ lintConfig . files ] ;
97
- } else {
114
+ } else if ( program ) {
98
115
files = Linter . getFileNames ( program ) ;
99
116
}
100
117
@@ -114,3 +131,23 @@ function getFilesToLint(program: ts.Program, lintConfig: CliLintConfig, Linter:
114
131
115
132
return files ;
116
133
}
134
+
135
+ function getFileContents ( file : string , program ?: ts . Program ) : string {
136
+ let contents : string ;
137
+
138
+ if ( program ) {
139
+ const sourceFile = program . getSourceFile ( file ) ;
140
+ if ( sourceFile ) {
141
+ contents = sourceFile . getFullText ( ) ;
142
+ }
143
+ } else {
144
+ // NOTE: The tslint CLI checks for and excludes MPEG transport streams; this does not.
145
+ try {
146
+ contents = fs . readFileSync ( file , 'utf8' ) ;
147
+ } catch ( e ) {
148
+ throw new SilentError ( `Could not read file "${ file } ".` ) ;
149
+ }
150
+ }
151
+
152
+ return contents ;
153
+ }
0 commit comments