Skip to content
This repository has been archived by the owner on Nov 1, 2020. It is now read-only.

Commit

Permalink
Use simenb eslint config
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Jul 25, 2016
1 parent f5dd224 commit 10f4dcf
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 89 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"extends": "standard"
"extends": "simenb-base"
}
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
"babel-register": "^6.4.3",
"coveralls": "^2.11.6",
"csslint": "^1.0.2",
"eslint": "^3.1.1",
"eslint-config-airbnb-base": "^5.0.0",
"eslint-config-simenb-base": "^1.1.0",
"eslint-plugin-import": "^1.11.1",
"in-publish": "^2.0.0",
"mocha": "^2.4.5",
"rimraf": "^2.5.1",
"standard": "^7.0.0"
"rimraf": "^2.5.1"
},
"files": [
"index.js"
Expand All @@ -44,7 +47,7 @@
"compile": "babel stylish.js -o index.js",
"compile-test": "babel test.js -o test-es5.js",
"cover": "babel-istanbul cover -x test-es5.js _mocha test-es5",
"lint": "standard",
"lint": "eslint .",
"precover": "npm run lint && npm run compile-test",
"prelint": "npm run clean",
"prepublish": "not-in-install && npm run compile || echo this is npm install",
Expand Down
82 changes: 41 additions & 41 deletions stylish.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import path from 'path'
import path from 'path';

import chalk from 'chalk'
import table from 'text-table'
import logSymbols from 'log-symbols'
import pluralize from 'pluralize'
import chalk from 'chalk';
import table from 'text-table';
import logSymbols from 'log-symbols';
import pluralize from 'pluralize';

export default {
id: 'stylish',
Expand All @@ -12,77 +12,77 @@ export default {
totalErrors: 0,
totalWarnings: 0,

startFormat: function () {
this.totalErrors = 0
this.totalWarnings = 0
startFormat() {
this.totalErrors = 0;
this.totalWarnings = 0;

return ''
return '';
},

endFormat: function () {
const totalViolations = this.totalErrors + this.totalWarnings
let output = '\n\n'
endFormat() {
const totalViolations = this.totalErrors + this.totalWarnings;
let output = '\n\n';

if (totalViolations === 0) {
return '\nNo violations'
return '\nNo violations';
}

if (this.totalErrors > 0) {
output += ` ${logSymbols.error} ${pluralize('error', this.totalErrors, true)}\n`
output += ` ${logSymbols.error} ${pluralize('error', this.totalErrors, true)}\n`;
}

if (this.totalWarnings > 0) {
output += ` ${logSymbols.warning} ${pluralize('warning', this.totalWarnings, true)}\n`
output += ` ${logSymbols.warning} ${pluralize('warning', this.totalWarnings, true)}\n`;
}

return output
return output;
},

formatResults: function (results, filename, options) {
const {messages} = results
let output = []
let underlinedFilename
const {absoluteFilePathsForFormatters} = (options || {})
formatResults(results, filename, options) {
const { messages } = results;
const output = [];
let underlinedFilename;
const { absoluteFilePathsForFormatters } = (options || {});

if (messages.length > 0) {
if (filename) {
if (absoluteFilePathsForFormatters) {
underlinedFilename = chalk.underline(filename)
underlinedFilename = chalk.underline(filename);
} else {
const relateFilename = path.relative(process.cwd(), filename)
const relateFilename = path.relative(process.cwd(), filename);

underlinedFilename = chalk.underline(relateFilename)
underlinedFilename = chalk.underline(relateFilename);
}
}

messages.forEach((origMessage) => {
const { message, rollup, line, col, type } = origMessage
const formatted = ['']
const isWarning = type === 'warning'
messages.forEach(origMessage => {
const { message, rollup, line, col, type } = origMessage;
const formatted = [''];
const isWarning = type === 'warning';

if (isWarning) {
this.totalWarnings++
this.totalWarnings++;
} else {
this.totalErrors++
this.totalErrors++;
}

if (rollup) {
formatted.push('')
formatted.push('')
formatted.push('');
formatted.push('');
} else {
formatted.push(chalk.gray(`line ${line}`))
formatted.push(chalk.gray(`char ${col}`))
formatted.push(chalk.gray(`line ${line}`));
formatted.push(chalk.gray(`char ${col}`));
}

formatted.push(isWarning ? chalk.blue(message) : chalk.red(message))
formatted.push(isWarning ? chalk.blue(message) : chalk.red(message));

output.push(formatted)
})
output.push(formatted);
});
}

if (underlinedFilename) {
return `\n${underlinedFilename}\n${table(output)}`
return `\n${underlinedFilename}\n${table(output)}`;
}
return `\n${table(output)}`
}
}
return `\n${table(output)}`;
},
};
89 changes: 45 additions & 44 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,78 +1,79 @@
/* eslint-env mocha */
/* eslint import/no-extraneous-dependencies: ["error", { "devDependencies": true }] */

import reporter from './stylish'
import { CSSLint } from 'csslint';
import chalk from 'chalk';

import { CSSLint } from 'csslint'
import chalk from 'chalk'
import path from 'path';
import assert from 'assert';

import path from 'path'
import assert from 'assert'
import reporter from './stylish';

describe('csslint-stylish', () => {
it('should report stuff', () => {
const res = CSSLint.verify('.class {\n color: red !important\n}\n')
const res = CSSLint.verify('.class {\n color: red !important\n}\n');

let report = reporter.startFormat() + reporter.formatResults(res, path.resolve('style.css')) + reporter.endFormat()
let report = reporter.startFormat() + reporter.formatResults(res, path.resolve('style.css')) + reporter.endFormat();

report = chalk.stripColor(report)
report = chalk.stripColor(report);

const filename = report.split('\n')[1]
const filename = report.split('\n')[1];

assert(filename === 'style.css', 'filename is correct')
assert(report.match(/line 2/), 'report contains text')
assert(report.match(/char 3/), 'report contains text')
assert(report.match(/Use of !important/), 'report contains text')
assert(report.match(/1 warning/), 'report contains text')
})
assert(filename === 'style.css', 'filename is correct');
assert(report.match(/line 2/), 'report contains text');
assert(report.match(/char 3/), 'report contains text');
assert(report.match(/Use of !important/), 'report contains text');
assert(report.match(/1 warning/), 'report contains text');
});

it('should report with full path', () => {
const res = CSSLint.verify('.class {\n color: red !important\n}\n')
const res = CSSLint.verify('.class {\n color: red !important\n}\n');

let report = reporter.startFormat() + reporter.formatResults(res, path.resolve('style.css'),
{absoluteFilePathsForFormatters: true}) + reporter.endFormat()
{ absoluteFilePathsForFormatters: true }) + reporter.endFormat();

report = chalk.stripColor(report)
report = chalk.stripColor(report);

const filename = report.split('\n')[1]
const filename = report.split('\n')[1];

assert(filename === path.join(__dirname, 'style.css'), 'filename is correct')
assert(report.match(/char 3/), 'report contains text')
assert(report.match(/Use of !important/), 'report contains text')
assert(report.match(/1 warning/), 'report contains text')
})
assert(filename === path.join(__dirname, 'style.css'), 'filename is correct');
assert(report.match(/char 3/), 'report contains text');
assert(report.match(/Use of !important/), 'report contains text');
assert(report.match(/1 warning/), 'report contains text');
});

it('should be able to be registered as formatter', () => {
assert(!CSSLint.hasFormat('stylish'), 'csslint should not be stylish')
assert(!CSSLint.hasFormat('stylish'), 'csslint should not be stylish');

CSSLint.addFormatter(reporter)
CSSLint.addFormatter(reporter);

assert(CSSLint.hasFormat('stylish'), 'csslint should be stylish')
})
assert(CSSLint.hasFormat('stylish'), 'csslint should be stylish');
});

it('should not report undefined output lines when no filename provided', () => {
const res = CSSLint.verify('.class {\n color: red !important\n}\n')
const res = CSSLint.verify('.class {\n color: red !important\n}\n');

let report = reporter.startFormat() + reporter.formatResults(res) + reporter.endFormat()
let report = reporter.startFormat() + reporter.formatResults(res) + reporter.endFormat();

report = chalk.stripColor(report)
report = chalk.stripColor(report);

let matches = report.match(/^undefined$/gm)
const matches = report.match(/^undefined$/gm);

assert(matches === null, 'report should not contains undefined text output')
})
assert(matches === null, 'report should not contains undefined text output');
});

it('should report filename provided', () => {
const res = CSSLint.verify('.class {\n color: red !important\n}\n')
const filename = path.resolve('filenamestyle.css')
const res = CSSLint.verify('.class {\n color: red !important\n}\n');
const filename = path.resolve('filenamestyle.css');
let report = reporter.startFormat() + reporter.formatResults(res, filename,
{ absoluteFilePathsForFormatters: true }) + reporter.endFormat()
{ absoluteFilePathsForFormatters: true }) + reporter.endFormat();

report = chalk.stripColor(report)
report = chalk.stripColor(report);

const matches = report.match(/^undefined$/gm)
const outfilename = report.split('\n')[1]
const matches = report.match(/^undefined$/gm);
const outfilename = report.split('\n')[1];

assert(matches === null, 'report should not contains undefined text output')
assert(outfilename === filename, 'filename should be in output lines')
})
})
assert(matches === null, 'report should not contains undefined text output');
assert(outfilename === filename, 'filename should be in output lines');
});
});

0 comments on commit 10f4dcf

Please sign in to comment.