Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade broccoli-filter #14

Merged
merged 4 commits into from
Mar 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
test/fixture/*
build/*
tmp/*
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "nightmare-mode/node",
"rules": {
"arrow-parens": [2, "always"],
"no-console": 0,
"prefer-arrow-callback": 0,
"prefer-reflect": 0,
Expand Down
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: node_js
node_js:
- '0.10'
- 'stable'
before_install:
- npm install -g broccoli-cli
- npm install -g broccoli-cli
23 changes: 0 additions & 23 deletions Brocfile.js

This file was deleted.

37 changes: 14 additions & 23 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/* eslint global-require: 0, consistent-return: 0 */

const path = require('path');

const Filter = require('broccoli-filter');
const CLIEngine = require('eslint').CLIEngine;

Expand Down Expand Up @@ -71,14 +69,15 @@ function EslintValidationFilter(inputTree, options) {
}
this.options = options || {};
this.eslintOptions = options.options || {};
// set inputTree
this.inputTree = inputTree;
// call base class constructor
Filter.call(this, inputTree);

// set formatter
this.formatter = require(this.options.format || 'eslint/lib/formatters/stylish');
this.formatter = require(this.options.format || 'eslint/lib/formatters/stylish'); // eslint-disable-line global-require

this.cli = new CLIEngine(this.eslintOptions);

this.eslintrc = inputTree;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to assume that inputTree is the srcDir string. Isn't inputTree an object? This breaks the paths in processString later on when we concatenate it to relativePath.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nickiaconis can this be addressed in #21? Pretty sure @teddyzeenny is right.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. All the tests provide a string as the input node, so I overlooked the possibility that this could be a tree instance. Any ideas how to resolve this from a tree? My first thought is to recursively resolve the _inputNodes objects until we get a string, but this doesn't account for merge trees.


this.testGenerator = options.testGenerator;
if (this.testGenerator) {
Expand All @@ -92,29 +91,21 @@ EslintValidationFilter.prototype.constructor = EslintValidationFilter;
EslintValidationFilter.prototype.extensions = ['js'];
EslintValidationFilter.prototype.targetExtension = 'js';

EslintValidationFilter.prototype.write = function write(readTree, destDir) {
return readTree(this.inputTree).then((srcDir) => {
if (!this.eslintrc) {
this.eslintrc = srcDir;
}

return Filter.prototype.write.call(this, readTree, destDir);
});
};

EslintValidationFilter.prototype.processString = function processString(content, relativePath) {
'use strict'; // eslint-disable-line strict

// verify file content
const result = this.cli.executeOnFiles([path.join(this.eslintrc, relativePath)]);
const filteredResults = filterAllIgnoredFileMessages(result);
const output = this.cli.executeOnText(content, this.eslintrc + '/' + relativePath);
const filteredOutput = filterAllIgnoredFileMessages(output);

// if verification has result
if (filteredResults.results.length &&
filteredResults.results[0].messages.length) {
if (filteredOutput.results.length &&
filteredOutput.results[0].messages.length) {

// log formatter output
console.log(this.formatter(filteredResults.results));
console.log(this.formatter(filteredOutput.results));

if (getResultSeverity(filteredResults.results) > 0) {
if (getResultSeverity(filteredOutput.results) > 0) {
if ('throwOnError' in this.internalOptions && this.internalOptions.throwOnError === true) {
// throw error if severe messages exist
throw new Error('severe rule errors');
Expand All @@ -125,8 +116,8 @@ EslintValidationFilter.prototype.processString = function processString(content,
if (this.testGenerator) {
let messages = [];

if (filteredResults.results.length) {
messages = filteredResults.results[0].messages || [];
if (filteredOutput.results.length) {
messages = filteredOutput.results[0].messages || [];
}

return this.testGenerator(relativePath, messages);
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"babelify": "babel lib --out-dir build",
"prepublish": "npm run babelify",
"lint": "eslint .",
"test": "npm run babelify && rm -rf lintbuild/ && ./node_modules/broccoli-cli/bin/broccoli build lintbuild > broccoli-build.out && node node_modules/mocha/bin/mocha lintbuild/test.js"
"babelify-tests": "babel test --ignore fixture --out-dir build/test",
"test": "npm run babelify && npm run babelify-tests && npm run lint && node_modules/mocha/bin/mocha build/test/test.js"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -36,7 +37,7 @@
},
"homepage": "https://github.com/jonathanKingston/broccoli-lint-eslint",
"dependencies": {
"broccoli-filter": "^0.1.12",
"broccoli-filter": "^1.2.3",
"eslint": "^2.3.0"
},
"devDependencies": {
Expand All @@ -45,6 +46,7 @@
"broccoli": "^0.16.9",
"broccoli-cli": "^1.0.0",
"broccoli-merge-trees": "^1.1.1",
"chai": "^3.5.0",
"eslint-config-nightmare-mode": "^2.3.0",
"mocha": "^2.2.4",
"rimraf": "^2.5.1"
Expand Down
17 changes: 0 additions & 17 deletions test/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,21 +1,4 @@
{
"extends": "nightmare-mode/node",
"rules": {
"no-console": 0,
"prefer-arrow-callback": 0,
"prefer-reflect": 0,
"prefer-template": 0,
"no-restricted-syntax": [2, "WithStatement"],
"no-empty-label": 0,
"no-labels": 2,
"space-after-keywords": 0,
"space-before-keywords": 0,
"space-return-throw-case": 0,
"keyword-spacing": 2,
"newline-per-chained-call": 0,
"no-magic-numbers": 0,
"arrow-spacing": 0
},
"env": {
"mocha": true
}
Expand Down
3 changes: 3 additions & 0 deletions test/fixture/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "nightmare-mode/node"
}
7 changes: 7 additions & 0 deletions test/fixture/3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function noNewLineBeforeReturn() {
const foo = 'foo';

console.log(foo);
}

noNewLineBeforeReturn();
35 changes: 35 additions & 0 deletions test/helpers/run-eslint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const broccoli = require('broccoli');
const eslint = require('../../index');

module.exports = function runEslint(path, _options) {
const options = _options || {};
const buildLog = [];
const consoleLog = console.log;

// stub console.log so we can get the formatter's output
console.log = function appendToBuildLog(...args) {
const text = args.join(' ');

buildLog.push(text);
};

// default options
options.format = options.format || 'eslint/lib/formatters/compact';
options.options = options.options || {};
options.options.ignore = options.options.ignore || false;

const tree = eslint(path, options);
const builder = new broccoli.Builder(tree);
const promise = builder.build().then(function builderThen() {
return buildLog.join('\n');
});

promise.finally(function builderCleanup() {
builder.cleanup();

// restore the original console.log
console.log = consoleLog;
});

return promise;
};
60 changes: 46 additions & 14 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,50 @@
const assert = require('assert');
const fs = require('fs');
const rimraf = require('rimraf');
const expect = require('chai').expect;
const runEslint = require('./helpers/run-eslint');
const FIXTURES = 'test/fixture';
const CAMELCASE = '(camelcase)';
const CONSOLE = '(no-console)';
const CUSTOM_RULES = 'testing custom rules';
const DOUBLEQUOTE = 'Strings must use doublequote.';
const FILEPATH = 'fixture/1.js';

afterEach(function afterEach() {
rimraf.sync('temp');
rimraf.sync('broccoli-build.out');
});
describe('EslintValidationFilter', function describeEslintValidationFilter() {
it('should report errors', function shouldReportErrors() {
// lint test fixtures
const promise = runEslint(FIXTURES);

return promise.then(function assertLinting(buildLog) {
expect(buildLog, 'Used eslint validation').to.have.string(CAMELCASE);
expect(buildLog, 'Shows filepath').to.have.string(FILEPATH);
expect(buildLog, 'Used relative config - console not allowed').to.have.string(CONSOLE);
expect(buildLog, 'Used relative config - single quotes').to.not.have.string(DOUBLEQUOTE);
expect(buildLog, 'No custom rules defined').to.not.have.string(CUSTOM_RULES);
});
});

it('should accept rule paths', function shouldAcceptRulePaths() {
// lint test fixtures using a custom rule
const promise = runEslint(FIXTURES, {
options: {
rulePaths: ['conf/rules']
}
});

return promise.then(function assertLinting(buildLog) {
expect(buildLog, 'Used custom rule').to.have.string(CUSTOM_RULES);
});
});

it('should reported errors', function shouldReportErrors() {
const buildLog = fs.readFileSync('broccoli-build.out').toString();
const NOT_FOUND = -1;
it('should accept config file path', function shouldAcceptConfigFile() {
// lint test fixtures using a config file at a non-default path
const promise = runEslint(FIXTURES, {
options: {
configFile: 'conf/eslint.json'
}
});

assert(buildLog.indexOf('Strings must use doublequote.') !== NOT_FOUND, 'Used eslint validation - strings');
assert(buildLog.indexOf('is not in camel case') !== NOT_FOUND, 'Used eslint validation - camel case');
assert(buildLog.indexOf('testing custom rules') !== NOT_FOUND, 'Used custom rulesdir rules');
assert(buildLog.indexOf('fixture/1.js') !== NOT_FOUND, 'Shows filepath');
return promise.then(function assertLinting(buildLog) {
expect(buildLog, 'Used alternate config - console allowed').to.not.have.string(CONSOLE);
expect(buildLog, 'Used alternate config - double quotes').to.have.string(DOUBLEQUOTE);
});
});
});