diff --git a/lib/formatter/codeframe.js b/lib/formatter/codeframe.js new file mode 100644 index 00000000..52d483fb --- /dev/null +++ b/lib/formatter/codeframe.js @@ -0,0 +1,66 @@ +/** + * @file codeFrame formatter for check results + * @author Gavin + */ + +const fs = require('fs'); +const chalk = require('chalk'); +const codeFrame = require('babel-code-frame'); + +const LEVEL_MAP = { + 1: 'waring', + 2: 'error' +}; + +const formatFilePath = (filePath, line, column) => { + if (line && column) { + filePath += `:${line}:${column}`; + } + return chalk.green(filePath); +}; + +const formatLevel = severity => { + const text = LEVEL_MAP[severity]; + return severity === 1 ? chalk.yellow(text) : chalk.red(text); +}; + +const formatError = (path, sourceCode, error) => { + const {line, column, severity} = error; + const filePath = formatFilePath(path, line, column); + const level = formatLevel(severity); + const topMessage = `${level}: ${error.message} (${error.rule}) at ${filePath}:\n`; + return `${topMessage}${codeFrame(sourceCode, error.line, error.column)}`; +}; + +module.exports = function (json) { + let errors = 0; + let warings = 0; + let files = 0; + let output = []; + + if (json.length) { + + json.forEach(file => { + const sourceCode = fs.readFileSync(file.path, {encoding: 'utf-8'}); + const path = file.relative; + const length = file.errors.length; + output.push(`fecs Linter found ${length} errors in file ${formatFilePath(path)}:\n`); + files++; + output = output.concat(file.errors.map(error => { + if (error.severity === 1) { + warings++; + } + else if (error.severity === 2) { + errors++; + } + return `${formatError(path, sourceCode, error)}\n\n`; + })); + }); + + // last result + output.push(`Result: ${errors} errors and ${warings} warings in ${files} files. `); + output.push('Please fix them.'); + } + + process.stdout.write('\n' + output.join('') + '\n'); +}; diff --git a/package-lock.json b/package-lock.json index 85f9ed3c..b461bee1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -271,9 +271,9 @@ "resolved": "https://registry.npmjs.org/babel-code-frame/-/babel-code-frame-6.26.0.tgz", "integrity": "sha1-Y/1D99weO7fONZR9uP42mj9Yx0s=", "requires": { - "chalk": "^1.1.3", - "esutils": "^2.0.2", - "js-tokens": "^3.0.2" + "chalk": "1.1.3", + "esutils": "2.0.2", + "js-tokens": "3.0.2" }, "dependencies": { "ansi-styles": { @@ -286,11 +286,11 @@ "resolved": "https://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz", "integrity": "sha1-qBFcVeSnAv5NFQq9OHKCKn4J/Jg=", "requires": { - "ansi-styles": "^2.2.1", - "escape-string-regexp": "^1.0.2", - "has-ansi": "^2.0.0", - "strip-ansi": "^3.0.0", - "supports-color": "^2.0.0" + "ansi-styles": "2.2.1", + "escape-string-regexp": "1.0.5", + "has-ansi": "2.0.0", + "strip-ansi": "3.0.1", + "supports-color": "2.0.0" } }, "supports-color": { @@ -900,38 +900,38 @@ }, "csshint": { "version": "0.3.3", - "resolved": "http://registry.npm.taobao.org/csshint/download/csshint-0.3.3.tgz", + "resolved": "https://registry.npmjs.org/csshint/-/csshint-0.3.3.tgz", "integrity": "sha1-0/mnd5n1DnlBXUAyBStg9S+AtOM=", "requires": { - "chalk": "^1.1.3", - "edp-core": "^1.0.32", - "js-yaml": "^3.6.1", - "manis": "^0.3.0", - "object-assign": "^4.1.0", - "postcss": "^5.2.0", - "strip-json-comments": "^2.0.1" + "chalk": "1.1.3", + "edp-core": "1.0.33", + "js-yaml": "3.12.0", + "manis": "0.3.4", + "object-assign": "4.1.1", + "postcss": "5.2.18", + "strip-json-comments": "2.0.1" }, "dependencies": { "ansi-styles": { "version": "2.2.1", - "resolved": "http://registry.npm.taobao.org/ansi-styles/download/ansi-styles-2.2.1.tgz", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-2.2.1.tgz", "integrity": "sha1-tDLdM1i2NM914eRmQ2gkBTPB3b4=" }, "chalk": { "version": "1.1.3", - "resolved": "http://registry.npm.taobao.org/chalk/download/chalk-1.1.3.tgz", + "resolved": "http://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz", "integrity": "sha1-qBFcVeSnAv5NFQq9OHKCKn4J/Jg=", "requires": { - "ansi-styles": "^2.2.1", - "escape-string-regexp": "^1.0.2", - "has-ansi": "^2.0.0", - "strip-ansi": "^3.0.0", - "supports-color": "^2.0.0" + "ansi-styles": "2.2.1", + "escape-string-regexp": "1.0.5", + "has-ansi": "2.0.0", + "strip-ansi": "3.0.1", + "supports-color": "2.0.0" } }, "supports-color": { "version": "2.0.0", - "resolved": "http://registry.npm.taobao.org/supports-color/download/supports-color-2.0.0.tgz", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-2.0.0.tgz", "integrity": "sha1-U10EXOa2Nj+kARcIRimZXp3zJMc=" } } @@ -1047,16 +1047,16 @@ }, "doctrine2": { "version": "1.2.2", - "resolved": "http://registry.npm.taobao.org/doctrine2/download/doctrine2-1.2.2.tgz", + "resolved": "https://registry.npmjs.org/doctrine2/-/doctrine2-1.2.2.tgz", "integrity": "sha1-ZCL8Te+nVXKYwqZcxvJ4njlQDUY=", "requires": { - "esutils": "^1.1.6", - "isarray": "^1.0.0" + "esutils": "1.1.6", + "isarray": "1.0.0" }, "dependencies": { "esutils": { "version": "1.1.6", - "resolved": "http://registry.npm.taobao.org/esutils/download/esutils-1.1.6.tgz", + "resolved": "https://registry.npmjs.org/esutils/-/esutils-1.1.6.tgz", "integrity": "sha1-wBzKqa5LiXxtDD4hCuUvPHqEQ3U=" } } @@ -1461,16 +1461,16 @@ }, "esformatter-fecs": { "version": "0.3.1", - "resolved": "http://registry.npm.taobao.org/esformatter-fecs/download/esformatter-fecs-0.3.1.tgz", + "resolved": "https://registry.npmjs.org/esformatter-fecs/-/esformatter-fecs-0.3.1.tgz", "integrity": "sha1-rRknKnfydH3zvBwWP6zdZAKxJY4=", "requires": { - "rocambole": "^0.7.0", - "rocambole-indent": "^2.0.4", - "rocambole-linebreak": "^1.0.1", - "rocambole-node": "^1.0.0", - "rocambole-token": "^1.2.1", - "rocambole-whitespace": "^1.0.0", - "unquoted-property-validator": "^1.0.0" + "rocambole": "0.7.0", + "rocambole-indent": "2.0.4", + "rocambole-linebreak": "1.0.2", + "rocambole-node": "1.0.0", + "rocambole-token": "1.2.1", + "rocambole-whitespace": "1.0.0", + "unquoted-property-validator": "1.0.2" } }, "esformatter-limit-linebreaks": { @@ -2130,7 +2130,7 @@ "resolved": "https://registry.npmjs.org/has-ansi/-/has-ansi-2.0.0.tgz", "integrity": "sha1-NPUEnOHs3ysGSa8+8k5F7TVBbZE=", "requires": { - "ansi-regex": "^2.0.0" + "ansi-regex": "2.1.1" } }, "has-flag": { @@ -2165,33 +2165,71 @@ "resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-2.7.1.tgz", "integrity": "sha512-7T/BxH19zbcCTa8XkMlbK5lTo1WtgkFi3GvdWEyNuc4Vex7/9Dqbnpsf4JMydcfj9HCg4zUWFTL3Za6lapg5/w==" }, - "html-code-gen": { - "version": "0.4.0", - "resolved": "http://registry.npm.taobao.org/html-code-gen/download/html-code-gen-0.4.0.tgz", - "integrity": "sha1-vmjsa2EJE71SPgOj+z2vU5ZfTN4=" - }, - "html-nest-rule": { - "version": "0.0.3", - "resolved": "http://registry.npm.taobao.org/html-nest-rule/download/html-nest-rule-0.0.3.tgz", - "integrity": "sha1-hqudjj5SD2p9hFgj/zVsJGg23qQ=" - }, "htmlcs": { "version": "0.3.0", - "resolved": "http://registry.npm.taobao.org/htmlcs/download/htmlcs-0.3.0.tgz", + "resolved": "https://registry.npmjs.org/htmlcs/-/htmlcs-0.3.0.tgz", "integrity": "sha1-XgK3PST9zElt8UJDmk73/mPTLSs=", "requires": { - "css-select": "^1.1.0", - "cssbeautify": "^0.3.1", - "differ-cli": "^0.3.0", - "domelementtype": "^1.1.3", - "exit": "^0.1.2", - "html-code-gen": "^0.4.0", - "html-nest-rule": "^0.0.3", - "htmlparser2": "^3.8.2", - "jformatter": "^1.0.13", - "manis": "^0.3.0", - "walk": "^2.3.9", - "yargs": "^3.29.0" + "css-select": "1.2.0", + "cssbeautify": "0.3.1", + "differ-cli": "0.3.0", + "domelementtype": "1.3.0", + "exit": "0.1.2", + "html-code-gen": "0.4.0", + "html-nest-rule": "0.0.3", + "htmlparser2": "3.9.2", + "jformatter": "1.0.13", + "manis": "0.3.4", + "walk": "2.3.14", + "yargs": "3.32.0" + }, + "dependencies": { + "esprima": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/esprima/-/esprima-1.0.4.tgz", + "integrity": "sha1-n1V+CPw7TSbs6d00+Pv0drYlha0=" + }, + "html-code-gen": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/html-code-gen/-/html-code-gen-0.4.0.tgz", + "integrity": "sha1-vmjsa2EJE71SPgOj+z2vU5ZfTN4=" + }, + "html-nest-rule": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/html-nest-rule/-/html-nest-rule-0.0.3.tgz", + "integrity": "sha512-7HHyRVCKm5f0iVsAx3CrLfH/VQIrVL0CAT0tRqpmSDz9i7v7puhJ3Rw1HdlF2UGmjAqmqyG7JCXKYADuWqWdwA==" + }, + "jformatter": { + "version": "1.0.13", + "resolved": "https://registry.npmjs.org/jformatter/-/jformatter-1.0.13.tgz", + "integrity": "sha1-9Xi/64q4RUBs6Pev88V/0DcPl6E=", + "requires": { + "commander": "2.16.0", + "differ-cli": "0.2.2", + "rocambole": "0.3.6" + }, + "dependencies": { + "differ-cli": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/differ-cli/-/differ-cli-0.2.2.tgz", + "integrity": "sha1-UW+xsYX0ukRr40w0G+OFUbYMe4Y=", + "requires": { + "cli-color": "0.3.3", + "commander": "2.16.0", + "q": "1.5.1", + "universal-diff": "2.0.2" + } + } + } + }, + "rocambole": { + "version": "0.3.6", + "resolved": "https://registry.npmjs.org/rocambole/-/rocambole-0.3.6.tgz", + "integrity": "sha1-Teu/WUMUS8e2AG2Vvo+swLdDUqc=", + "requires": { + "esprima": "1.0.4" + } + } } }, "htmlparser2": { @@ -2587,10 +2625,9 @@ }, "jasmine-reporters": { "version": "git://github.com/larrymyers/jasmine-reporters.git#2c7242dc11c15c2f156169bc704798568b8cb50d", - "from": "git://github.com/larrymyers/jasmine-reporters.git#2c7242dc11c15c2f156169bc704798568b8cb50d", "dev": true, "requires": { - "mkdirp": "~0.3.5" + "mkdirp": "0.3.5" }, "dependencies": { "mkdirp": { @@ -2601,42 +2638,6 @@ } } }, - "jformatter": { - "version": "1.0.13", - "resolved": "http://registry.npm.taobao.org/jformatter/download/jformatter-1.0.13.tgz", - "integrity": "sha1-9Xi/64q4RUBs6Pev88V/0DcPl6E=", - "requires": { - "commander": "^2.5.0", - "differ-cli": "0.2.2", - "rocambole": "0.3.6" - }, - "dependencies": { - "differ-cli": { - "version": "0.2.2", - "resolved": "http://registry.npm.taobao.org/differ-cli/download/differ-cli-0.2.2.tgz", - "integrity": "sha1-UW+xsYX0ukRr40w0G+OFUbYMe4Y=", - "requires": { - "cli-color": "^0.3.2", - "commander": "^2.3.0", - "q": "^1.0.1", - "universal-diff": "^2.0.1" - } - }, - "esprima": { - "version": "1.0.4", - "resolved": "http://registry.npm.taobao.org/esprima/download/esprima-1.0.4.tgz", - "integrity": "sha1-n1V+CPw7TSbs6d00+Pv0drYlha0=" - }, - "rocambole": { - "version": "0.3.6", - "resolved": "http://registry.npm.taobao.org/rocambole/download/rocambole-0.3.6.tgz", - "integrity": "sha1-Teu/WUMUS8e2AG2Vvo+swLdDUqc=", - "requires": { - "esprima": "~1.0" - } - } - } - }, "js-base64": { "version": "2.4.9", "resolved": "http://registry.npm.taobao.org/js-base64/download/js-base64-2.4.9.tgz", @@ -2796,40 +2797,40 @@ }, "lesslint": { "version": "1.0.2", - "resolved": "http://registry.npm.taobao.org/lesslint/download/lesslint-1.0.2.tgz", + "resolved": "https://registry.npmjs.org/lesslint/-/lesslint-1.0.2.tgz", "integrity": "sha1-i5Bs+OkKXjsXhQSh4tj8qpxE2Tw=", "requires": { - "chalk": "^1.1.3", - "edp-core": "^1.0.32", - "js-yaml": "^3.6.1", - "less": "^2.7.1", - "manis": "^0.3.0", - "postcss": "^5.2.0", - "postcss-less": "^0.14.0", - "postcss-values-parser": "^0.1.7", - "strip-json-comments": "^2.0.1" + "chalk": "1.1.3", + "edp-core": "1.0.33", + "js-yaml": "3.12.0", + "less": "2.7.3", + "manis": "0.3.4", + "postcss": "5.2.18", + "postcss-less": "0.14.0", + "postcss-values-parser": "0.1.8", + "strip-json-comments": "2.0.1" }, "dependencies": { "ansi-styles": { "version": "2.2.1", - "resolved": "http://registry.npm.taobao.org/ansi-styles/download/ansi-styles-2.2.1.tgz", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-2.2.1.tgz", "integrity": "sha1-tDLdM1i2NM914eRmQ2gkBTPB3b4=" }, "chalk": { "version": "1.1.3", - "resolved": "http://registry.npm.taobao.org/chalk/download/chalk-1.1.3.tgz", + "resolved": "http://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz", "integrity": "sha1-qBFcVeSnAv5NFQq9OHKCKn4J/Jg=", "requires": { - "ansi-styles": "^2.2.1", - "escape-string-regexp": "^1.0.2", - "has-ansi": "^2.0.0", - "strip-ansi": "^3.0.0", - "supports-color": "^2.0.0" + "ansi-styles": "2.2.1", + "escape-string-regexp": "1.0.5", + "has-ansi": "2.0.0", + "strip-ansi": "3.0.1", + "supports-color": "2.0.0" } }, "supports-color": { "version": "2.0.0", - "resolved": "http://registry.npm.taobao.org/supports-color/download/supports-color-2.0.0.tgz", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-2.0.0.tgz", "integrity": "sha1-U10EXOa2Nj+kARcIRimZXp3zJMc=" } } @@ -4017,7 +4018,7 @@ "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz", "integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=", "requires": { - "ansi-regex": "^2.0.0" + "ansi-regex": "2.1.1" } }, "strip-bom": { @@ -4570,4 +4571,4 @@ } } } -} +} \ No newline at end of file diff --git a/package.json b/package.json index 9f35653a..480a0feb 100755 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ }, "homepage": "https://github.com/ecomfe/fecs", "dependencies": { + "babel-code-frame": "^6.26.0", "babel-eslint": "^8.0.1", "chalk": "^2.1.0", "csscomb": "^4.2.0",