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

Restructuring with linting and coverage #17

Merged
merged 11 commits into from
Aug 2, 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
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
root = true

[*]

# Change these settings to your own preference
indent_style = space
indent_size = 2

# We recommend you to keep these unchanged
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
insert_final_newline = false
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules
npm-debug.log
npm-debug.log
.nyc_output
coverage
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
sudo: false
language: node_js
node_js:
- '4'
- 4
- "stable"

after_success: "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls"
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ $ npm install stylelint-webpack-plugin
In your webpack configuration

```js
var styleLintPlugin = require('stylelint-webpack-plugin');
var StyleLintPlugin = require('stylelint-webpack-plugin');


module.exports = {
// ...
plugins: [
new styleLintPlugin(),
new StyleLintPlugin(),
],
// ...
}
Expand All @@ -47,7 +47,7 @@ See [stylelint options](http://stylelint.io/user-guide/node-api/#options), for t
// Default settings
module.exports = {
plugins: [
new styleLintPlugin({
new StyleLintPlugin({
configFile: '.stylelintrc',
context: 'inherits from webpack',
files: '**/*.s?(a|c)ss',
Expand Down
76 changes: 23 additions & 53 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,44 @@
'use strict';

// Dependencies
var loaderUtils = require('loader-utils');
var assign = require('object-assign');
var path = require('path');
var assign = require('object-assign');
var formatter = require('stylelint/dist/formatters/stringFormatter').default;
var chalk = require('chalk');
var arrify = require('arrify');

// Modules
var linter = require('./lib/linter');
var runCompilation = require('./lib/run-compilation');

function apply(options, compiler) {
var context = options.context || compiler.context;
var errors = [];

options = Object.assign({}, options, {
// TODO: make it work with arrays
files: options.files.map(function(file) {
files: options.files.map(function (file) {
return path.join(context, '/', file);
})
});

function runCompilation(compilation, done) {
linter(options).then(function(lint) {
if (lint.errored) {
errors = lint.results.filter(function(f) {
return f.errored;
}).map(function(f) {
return f.source; // send error instead
});

(options.quiet !== true) && console.log(chalk.yellow(options.formatter(lint.results)));
}

if (options.failOnError && errors.length) {
done(new Error('Failed because of a stylelint error.\n'));
} else {
done();
}
}).catch(function(e) {
if (options.failOnError && errors.length) {
done(new Error('Failed because of a stylelint error.\n'));
} else {
done();
}
console.log(chalk.red(e));
});

compilation.plugin && compilation.plugin('compilation', function(compilation) {
errors.forEach(function(err) {
compilation.errors.push(err);
});
});
}

compiler.plugin('run', runCompilation);
compiler.plugin('watch-run', runCompilation);
compiler.plugin('run', runCompilation.bind(this, options));
compiler.plugin('watch-run', runCompilation.bind(this, options));
}

// makes it easier to pass and check options to the plugin thank you webpack doc
// [https://webpack.github.io/docs/plugins.html#the-compiler-instance]
module.exports = function(options) {
options = options || {};
// Default Glob is any directory level of scss and/or sass file,
// under webpack's context and specificity changed via globbing patterns
options.files = options.files || '**/*.s?(c|a)ss';
!Array.isArray(options.files) && (options.files = [options.files]);
options.configFile = options.configFile || '.stylelintrc';
options.formatter = options.formatter || formatter;
options.quiet = options.quiet || false;

module.exports = function (options) {
return {
apply: apply.bind(this, options)
apply: apply.bind(this, buildOptions(options))
};
};
};

function buildOptions(options) {
return assign({
configFile: '.stylelintrc',
formatter: formatter,
quiet: false
}, options, {
// Default Glob is any directory level of scss and/or sass file,
// under webpack's context and specificity changed via globbing patterns
files: arrify(options.files || '**/*.s?(c|a)ss')
});
}
12 changes: 4 additions & 8 deletions lib/linter.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
'use strict';

// Dependencies
var stylelint = require('stylelint');

function lint(options) {
return new Promise(function(resolve, reject) {
stylelint.lint(options).then(function(data) {
resolve(data);
}).catch(function(e) {
reject(e);
});
});
return stylelint.lint(options);
}

module.exports = lint;
module.exports = lint;
53 changes: 53 additions & 0 deletions lib/run-compilation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';

var chalk = require('chalk');
var linter = require('./linter');

/**
* Function bound to the plugin `apply` method to run the linter and report any
* errors (and their source file locations)
* @param options - from the apply method, the options passed in
* @param compilation - the compiler object
* @param done - callback
*/
module.exports = function runCompilation(options, compilation, done) {
var errors = [];

linter(options)
.then(function (lint) {
if (lint.errored) {
errors = lint.results
.filter(function (f) {
return f.errored;
})
.map(function (f) {
return f.source; // send error instead
});

if (!options.quiet) {
console.log(chalk.yellow(options.formatter(lint.results)));
}
}

if (options.failOnError && errors.length) {
done(new Error('Failed because of a stylelint error.\n'));
} else {
done();
}
})
.catch(function (err) {
if (options.failOnError && errors.length) {
done(new Error('Failed because of a stylelint error.\n'));
} else {
done();
}
console.log(chalk.red(err));
});

// eslint-disable-next-line no-unused-expressions
compilation.plugin && compilation.plugin('compilation', function (compilation) {
errors.forEach(function (err) {
compilation.errors.push(err);
});
});
};
34 changes: 28 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,48 @@
"url": "https://github.com/vieron/stylelint-webpack-plugin/issues"
},
"homepage": "https://github.com/vieron/stylelint-webpack-plugin#readme",
"peerDependencies": {
"stylelint": "^7.0.1"
},
"dependencies": {
"arrify": "^1.0.1",
"chalk": "^1.1.1",
"extract-text-webpack-plugin": "^1.0.1",
"loader-utils": "~0.2.10",
"object-assign": "~4.0.1",
"stylelint": "^6.0.1",
"stylelint": "^7.0.1",
"webpack": "^1.12.10"
},
"devDependencies": {
"chai": "^3.4.1",
"chai-as-promised": "^5.2.0",
"coveralls": "^2.11.12",
"memory-fs": "^0.3.0",
"mocha": "^2.3.4"
},
"peerDependencies": {
"stylelint": "^6.0.1"
"mocha": "^2.3.4",
"nyc": "^7.1.0",
"xo": "^0.16.0"
},
"scripts": {
"test": "mocha --harmony --full-trace --check-leaks",
"pretest": "xo",
"test": "nyc mocha",
"preversion": "npm run test",
"version": "git add -A ."
},
"nyc": {
"reporter": [
"lcov",
"text-summary"
]
},
"xo": {
"space": true,
"envs": [
"node",
"mocha"
],
"globals": [
"getPath",
"expect"
]
}
}
11 changes: 5 additions & 6 deletions test/.stylelintrc
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,32 @@
"declaration-colon-newline-after": "always-multi-line",
"declaration-colon-space-after": "always-single-line",
"declaration-colon-space-before": "never",
"font-family-name-quotes": "double-where-recommended",
"font-family-name-quotes": "always-unless-keyword",
"function-calc-no-unspaced-operator": true,
"function-comma-newline-after": "always-multi-line",
"function-comma-space-after": "always-single-line",
"function-comma-space-before": "never",
"function-linear-gradient-no-nonstandard-direction": true,
"function-parentheses-newline-inside": "always-multi-line",
"function-parentheses-space-inside": "never-single-line",
"function-url-quotes": "double",
"function-whitespace-after": "always",
"indentation": 2,
"length-zero-no-unit": true,
"max-empty-lines": 1,
"media-feature-colon-space-after": "always",
"media-feature-colon-space-before": "never",
"media-feature-no-missing-punctuation": true,
"media-feature-parentheses-space-inside": "never",
"media-feature-range-operator-space-after": "always",
"media-feature-range-operator-space-before": "always",
"media-query-list-comma-newline-after": "always-multi-line",
"media-query-list-comma-space-after": "always-single-line",
"media-query-list-comma-space-before": "never",
"media-query-parentheses-space-inside": "never",
"no-eol-whitespace": true,
"no-invalid-double-slash-comments": true,
"no-missing-eof-newline": true,
"no-missing-end-of-source-newline": true,
"number-leading-zero": "always",
"number-no-trailing-zeros": true,
"number-zero-length-no-unit": true,
"rule-non-nested-empty-line-before": [ "always-multi-line", {
"ignore": ["after-comment"],
} ],
Expand All @@ -71,4 +70,4 @@
"value-list-comma-space-after": "always-single-line",
"value-list-comma-space-before": "never",
},
}
}
3 changes: 3 additions & 0 deletions test/fixtures/test1/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require(getPath('./../../../node_modules/file-loader/index') + '!./test.scss');

console.log('test1');
File renamed without changes.
3 changes: 3 additions & 0 deletions test/fixtures/test2/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require(getPath('./../../../node_modules/file-loader/index') + '!./test.scss');

console.log('test2');
File renamed without changes.
3 changes: 3 additions & 0 deletions test/fixtures/test3/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require(getPath('./../../../node_modules/file-loader/index') + '!./test.scss');

console.log('test3');
File renamed without changes.
3 changes: 3 additions & 0 deletions test/fixtures/test4/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require(getPath('./../../../node_modules/file-loader/index') + '!./test.scss');

console.log('test4');
File renamed without changes.
3 changes: 3 additions & 0 deletions test/fixtures/test5/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require(getPath('./../../../node_modules/file-loader/index') + '!./test.scss');

console.log('test5');
File renamed without changes.
3 changes: 3 additions & 0 deletions test/fixtures/test6/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require(getPath('./../../../node_modules/file-loader/index') + '!./test.scss');

console.log('test6');
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions test/fixtures/test7/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require(getPath('./../../../node_modules/file-loader/index') + '!./test.scss');

console.log('test6');
File renamed without changes.
10 changes: 10 additions & 0 deletions test/helpers/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

var chai = require('chai');

global.expect = chai.expect;

chai.use(require('chai-as-promised'));

// get path from the './test' directory
global.getPath = require('path').join.bind(this, __dirname, '..');
Loading