@@ -42,8 +42,8 @@ define(function (require, exports, module) {
4242 IndexingWorker = brackets . getModule ( "worker/IndexingWorker" ) ;
4343
4444 let prefs = PreferencesManager . getExtensionPrefs ( "ESLint" ) ,
45- projectSpecificOptions = null ,
46- esLintConfigFileErrorMessage = null ;
45+ esLintEnabled = false ,
46+ projectSpecificOptions = null ;
4747
4848 const ESLINT_ERROR_MODULE_LOAD_FAILED = "ESLINT_MODULE_LOAD_FAILED" ,
4949 ESLINT_ERROR_MODULE_NOT_FOUND = "ESLINT_MODULE_NOT_FOUND" ,
@@ -75,7 +75,7 @@ define(function (require, exports, module) {
7575 case ESLINT_ERROR_LINT_FAILED :
7676 errorMessage = Strings . DESCRIPTION_ESLINT_FAILED ; break ;
7777 case ESLINT_ERROR_MODULE_NOT_FOUND :
78- errorMessage = Strings . DESCRIPTION_ESLINT_NOT_FOUND ; break ;
78+ errorMessage = Strings . DESCRIPTION_ESLINT_DO_NPM_INSTALL ; break ;
7979 case ESLINT_ERROR_MODULE_LOAD_FAILED :
8080 errorMessage = Strings . DESCRIPTION_ESLINT_LOAD_FAILED ; break ;
8181 }
@@ -131,7 +131,7 @@ define(function (require, exports, module) {
131131 resolve ( { errors : errors } ) ;
132132 } else if ( esLintResult . isError ) {
133133 resolve ( { errors : _getLintError ( esLintResult . errorCode ) } ) ;
134- } else {
134+ } else if ( ! esLintResult . result ) {
135135 console . error ( "ESLint Unknown result" , esLintResult ) ;
136136 resolve ( ) ;
137137 }
@@ -143,105 +143,50 @@ define(function (require, exports, module) {
143143 * @private
144144 * @type {string }
145145 */
146- const CONFIG_FILE_NAME = ".jshintrc" ;
147-
148- /**
149- * Removes JavaScript comments from a string by replacing
150- * everything between block comments and everything after
151- * single-line comments in a non-greedy way.
152- *
153- * English version of the regex:
154- * match '/*'
155- * then match zero or more instances of any character (incl. \n)
156- * except for instances of '* /' (without a space, obv.)
157- * then match '* /' (again, without a space)
158- *
159- * @param {string } str a string with potential JavaScript comments.
160- * @returns {string } a string without JavaScript comments.
161- */
162- function removeComments ( str ) {
163- str = str || "" ;
164-
165- str = str . replace ( / \/ \* (?: (? ! \* \/ ) [ \s \S ] ) * \* \/ / g, "" ) ;
166- str = str . replace ( / \/ \/ [ ^ \n \r ] * / g, "" ) ; // Everything after '//'
167-
168- return str ;
169- }
146+ const PACKAGE_JSON = "package.json" ;
170147
171148 /**
172- * Reads configuration file in the specified directory. Returns a promise for configuration object.
173- *
174- * @param {string } dir absolute path to a directory.
175- * @param {string } configFileName name of the configuration file (optional)
149+ * Reads package.json and see if eslint is in dependencies or dev dependencies
176150 *
177151 * @returns {Promise } a promise to return configuration object.
178152 */
179- function _readConfig ( dir , configFileName ) {
180- return new Promise ( ( resolve , reject ) => {
181- configFileName = configFileName || CONFIG_FILE_NAME ;
182- const configFilePath = path . join ( dir , configFileName ) ;
183- let displayPath = ProjectManager . makeProjectRelativeIfPossible ( configFilePath ) ;
184- displayPath = Phoenix . app . getDisplayPath ( displayPath ) ;
153+ function _isESLintProject ( ) {
154+ return new Promise ( ( resolve ) => {
155+ const configFilePath = path . join ( ProjectManager . getProjectRoot ( ) . fullPath , PACKAGE_JSON ) ;
185156 DocumentManager . getDocumentForPath ( configFilePath ) . done ( function ( configDoc ) {
186- let config ;
187157 const content = configDoc . getText ( ) ;
188158 try {
189- config = JSON . parse ( removeComments ( content ) ) ;
190- console . log ( "JSHint: loaded config file for project " + configFilePath ) ;
191- } catch ( e ) {
192- console . log ( "JSHint: error parsing " + configFilePath ) ;
193- // just log and return as this is an expected failure for us while the user edits code
194- reject ( "Error parsing JSHint config file: " + displayPath ) ;
195- return ;
196- }
197- // Load any base config defined by "extends".
198- // The same functionality as in
199- // jslints -> cli.js -> loadConfig -> if (config['extends'])...
200- // https://jshint.com/docs/cli/ > Special Options
201- if ( config . extends ) {
202- let extendFile = FileSystem . getFileForPath ( path . join ( dir , config . extends ) ) ;
203- _readConfig ( extendFile . parentPath , extendFile . name ) . then ( baseConfigResult => {
204- delete config . extends ;
205- let mergedConfig = $ . extend ( { } , baseConfigResult , config ) ;
206- if ( config . globals ) {
207- delete config . globals ;
208- }
209- resolve ( mergedConfig ) ;
210- } ) . catch ( ( ) => {
211- let extendDisplayPath = ProjectManager . makeProjectRelativeIfPossible ( extendFile . fullPath ) ;
212- extendDisplayPath = Phoenix . app . getDisplayPath ( extendDisplayPath ) ;
213- reject ( "Error parsing JSHint config file: " + extendDisplayPath ) ;
214- } ) ;
215- }
216- else {
217- resolve ( config ) ;
159+ const config = JSON . parse ( content ) ;
160+ resolve ( config && (
161+ ( config . devDependencies && config . devDependencies . eslint ) ||
162+ ( config . dependencies && config . dependencies . eslint )
163+ ) ) ;
164+ } catch ( err ) {
165+ console . error ( `ESLint Error parsing ${ PACKAGE_JSON } ` , configFilePath , err ) ;
166+ resolve ( false ) ;
218167 }
219168 } ) . fail ( ( err ) => {
220- if ( err === FileSystemError . NOT_FOUND ) {
221- resolve ( null ) ; // no config file is a valid case. we just resolve with null
222- return ;
169+ if ( err !== FileSystemError . NOT_FOUND ) {
170+ console . error ( `ESLint Error reading ${ PACKAGE_JSON } ` , configFilePath , err ) ;
223171 }
224- console . error ( "Error reading JSHint Config File" , configFilePath , err ) ;
225- reject ( "Error reading JSHint Config File" , displayPath ) ;
172+ resolve ( false ) ;
226173 } ) ;
227174 } ) ;
228175 }
229176
230177 function _reloadOptions ( ) {
231178 projectSpecificOptions = null ;
232- _readConfig ( ProjectManager . getProjectRoot ( ) . fullPath , CONFIG_FILE_NAME ) . then ( ( config ) => {
233- projectSpecificOptions = config ;
234- CodeInspection . requestRun ( Strings . JSHINT_NAME ) ;
235- esLintConfigFileErrorMessage = null ;
236- } ) . catch ( ( err ) => {
237- esLintConfigFileErrorMessage = err ;
238- CodeInspection . requestRun ( Strings . JSHINT_NAME ) ;
179+ _isESLintProject ( ProjectManager . getProjectRoot ( ) . fullPath ) . then ( ( shouldESLintEnable ) => {
180+ esLintEnabled = shouldESLintEnable ;
181+ CodeInspection . requestRun ( Strings . ESLINT_NAME ) ;
182+ } ) . catch ( ( ) => {
183+ esLintEnabled = false ;
184+ CodeInspection . requestRun ( Strings . ESLINT_NAME ) ;
239185 } ) ;
240186 }
241187
242- function isESLintConfigActive ( ) {
243- return true ;
244- // return !!(esLintConfigFileErrorMessage || projectSpecificOptions); todo
188+ function isESLintActive ( ) {
189+ return esLintEnabled ;
245190 }
246191
247192 function _isFileInArray ( fileToCheck , fileArray ) {
@@ -257,13 +202,12 @@ define(function (require, exports, module) {
257202 }
258203
259204 function _projectFileChanged ( _evt , entry , added , removed ) {
260- let configFilePath = FileSystem . getFileForPath ( ProjectManager . getProjectRoot ( ) . fullPath + CONFIG_FILE_NAME ) ;
205+ let configFilePath = FileSystem . getFileForPath ( ProjectManager . getProjectRoot ( ) . fullPath + PACKAGE_JSON ) ;
261206 if ( entry && entry . fullPath === configFilePath . fullPath
262207 || _isFileInArray ( configFilePath , added ) ) {
263208 _reloadOptions ( ) ;
264209 } else if ( _isFileInArray ( configFilePath , removed ) ) {
265210 projectSpecificOptions = null ;
266- esLintConfigFileErrorMessage = null ;
267211 }
268212 }
269213
@@ -278,10 +222,9 @@ define(function (require, exports, module) {
278222 name : Strings . ESLINT_NAME ,
279223 scanFileAsync : lintOneFile ,
280224 canInspect : function ( fullPath ) {
281- return ! prefs . get ( PREFS_ESLINT_DISABLED ) && fullPath && ! fullPath . endsWith ( ".min.js" )
282- && isESLintConfigActive ( ) ; // in browsers, jshint is default if no linter present
225+ return ! prefs . get ( PREFS_ESLINT_DISABLED ) && fullPath && ! fullPath . endsWith ( ".min.js" ) && isESLintActive ( ) ;
283226 }
284227 } ) ;
285228
286- exports . isESLintConfigActive = isESLintConfigActive ;
229+ exports . isESLintActive = isESLintActive ;
287230} ) ;
0 commit comments