javascript - .jshintrc 文件未被读取

标签 javascript cordova ionic-framework jshint

我正在使用 JSHint 在 Ionic 框架项目上检查我的 ES6 代码。我需要配置 list 才能使用 ES6。似乎当我运行 linter 时,通过使用一个小脚本,它不会读取配置文件 .jshintrc 。我遇到了一堆这样的错误:

'arrow function syntax (=>)' is only available in ES6 (use esnext option). ->     $ionicPlatform.ready(() => {

我的 .jshintrc 文件:

{
  "asi": false,
  "boss": true,
  "curly": true,
  "eqeqeq": false,
  "eqnull": true,
  "esnext": true,
  "expr": true,
  "forin": true,
  "immed": true,
  "laxbreak": true,
  "newcap": false,
  "noarg": true,
  "node": true,
  "nonew": true,
  "plusplus": true,
  "quotmark": "single",
  "strict": false,
  "undef": true,
  "unused": true
}

我正在使用包含在 Hooks/before_prepare 中的脚本运行 JSHint

#!/usr/bin/env node

var fs = require('fs');
var path = require('path');
var jshint = require('jshint').JSHINT;
var async = require('async');

var foldersToProcess = [
    'js6/',
    'js6/controllers',
    'js6/controllers/schedule',
];

foldersToProcess.forEach(function(folder) {
    processFiles("www/" + folder);
});

function processFiles(dir, callback) {
    var errorCount = 0;
    fs.readdir(dir, function(err, list) {
        if (err) {
            console.log('processFiles err: ' + err);
            return;
        }
        async.eachSeries(list, function(file, innercallback) {
            file = dir + '/' + file;
            fs.stat(file, function(err, stat) {
                if(!stat.isDirectory()) {
                    if(path.extname(file) === ".js") {
                        lintFile(file, function(hasError) {
                            if(hasError) {
                                errorCount++;
                            }
                            innercallback();
                        });
                    } else {
                        innercallback();
                    }
                } else {
                    innercallback();
                }
            });
        }, function(error) {
            if(errorCount > 0) {
                process.exit(1);
            }
        });
    });
}

function lintFile(file, callback) {
    console.log("Linting " + file);
    fs.readFile(file, function(err, data) {
        if(err) {
            console.log('Error: ' + err);
            return;
        }
        if(jshint(data.toString())) {
            console.log('File ' + file + ' has no errors.');
            console.log('-----------------------------------------');
            callback(false);
        } else {
            console.log('Errors in file ' + file);
            var out = jshint.data(),
            errors = out.errors;
            for(var j = 0; j < errors.length; j++) {
                console.log(errors[j].line + ':' + errors[j].character + ' -> ' + errors[j].reason + ' -> ' +
errors[j].evidence);
            }
            console.log('-----------------------------------------');
            callback(true);
        }
    });
}

文件结构是典型的cordova项目结构,我有一个www文件夹,里面有一个js6文件夹www --> js6 --> controllers --> schedule

最佳答案

改变

if(jshint(data.toString())) {

if(jshint(data.toString(), {esnext:true}})) {

或者你可以先阅读.jshintrc文件的内容,然后在这个地方设置jshint配置。另见 https://github.com/jetma/cordova-hooks/blob/master/before_prepare/02_jshint.js/ .

关于javascript - .jshintrc 文件未被读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30118274/

相关文章:

javascript - .focus() 不关注新元素

javascript - jQuery 验证下拉列表

javascript - 未捕获的语法错误 : Unexpected token < VM105:17Uncaught ReferenceError: System is not defined

ios - 使用 google-plus 插件的 Ionic iOS 构建错误

css - 如何以流畅的布局实现此 CSS?

javascript - 动画谷歌地图折线

javascript - 在单页上应用 Touch Swipe,用于动态加载数据的页面?

javascript - 加载外部 URL 时检测 PhoneGap/Cordova

database - PhoneGap 使用什么数据库

angularjs - 需要包含 Ionic + Angular Charts : Uncaught Error: Chart. js 库