1- const Task = require ( '../ember-cli/lib/models/task' ) ;
21import * as chalk from 'chalk' ;
2+ import * as fs from 'fs' ;
33import * as glob from 'glob' ;
4+ import * as path from 'path' ;
45import * as ts from 'typescript' ;
56import { requireProjectModule } from '../utilities/require-project-module' ;
67import { CliConfig } from '../models/config' ;
78import { LintCommandOptions } from '../commands/lint' ;
89
10+ const SilentError = require ( 'silent-error' ) ;
11+ const Task = require ( '../ember-cli/lib/models/task' ) ;
12+
913interface CliLintConfig {
1014 files ?: ( string | string [ ] ) ;
1115 project ?: string ;
@@ -30,7 +34,12 @@ export default Task.extend({
3034
3135 const result = lintConfigs
3236 . 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+ }
3443 const files = getFilesToLint ( program , config , Linter ) ;
3544 const lintOptions = {
3645 fix : commandOptions . fix ,
@@ -39,13 +48,21 @@ export default Task.extend({
3948 const lintProgram = commandOptions . typeCheck ? program : undefined ;
4049 const linter = new Linter ( lintOptions , lintProgram ) ;
4150
51+ let lastDirectory : string ;
52+ let configLoad : any ;
4253 files . forEach ( ( file ) => {
43- const sourceFile = program . getSourceFile ( file ) ;
44- if ( ! sourceFile ) {
54+ const fileContents = getFileContents ( file , program ) ;
55+ if ( ! fileContents ) {
4556 return ;
4657 }
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+
4966 linter . lint ( file , fileContents , configLoad . results ) ;
5067 } ) ;
5168
@@ -94,7 +111,7 @@ function getFilesToLint(program: ts.Program, lintConfig: CliLintConfig, Linter:
94111
95112 if ( lintConfig . files !== null ) {
96113 files = Array . isArray ( lintConfig . files ) ? lintConfig . files : [ lintConfig . files ] ;
97- } else {
114+ } else if ( program ) {
98115 files = Linter . getFileNames ( program ) ;
99116 }
100117
@@ -114,3 +131,23 @@ function getFilesToLint(program: ts.Program, lintConfig: CliLintConfig, Linter:
114131
115132 return files ;
116133}
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