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

Change CSSLint to Stylelint #56

Closed
wants to merge 8 commits into from
3 changes: 3 additions & 0 deletions server/.stylelintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
Copy link
Owner

Choose a reason for hiding this comment

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

unclear exactly how/when this file is read by the library. It'd be better if this could be embedded into the code to avoid spooky action at a distance

Copy link
Author

Choose a reason for hiding this comment

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

Looking in the Stylelint documentation, there is:

stylelint uses cosmiconfig to find and load your configuration object. Starting from the current working directory, it looks for the following possible sources:

  • a stylelint property in package.json
  • a .stylelintrc file
  • a stylelint.config.js file exporting a JS object
  • a stylelint.config.cjs file exporting a JS object. When running stylelint in JavaScript packages that specify "type":"module" in their package.json

No rules are turned on by default and there are no default values. You must explicitly configure each rule to turn it on.

You can extend an existing configuration (whether your own or a third-party one).

Popular configurations include:

stylelint-config-recommended - turns on just possible error rules
stylelint-config-standard - extends recommended one by turning on 60 stylistic rules

I used the standard configuration because I don't have much time to create a file with all the rules, and it'd be better to use the library standard config file

"extends": "stylelint-config-standard"
}
84 changes: 53 additions & 31 deletions server/cssfile.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var csslint = require('csslint').CSSLint;
var cssparser = require('postcss');
var stylelint = require('stylelint');

function CssFile(source, path, callback){
callback = callback || function(){}
Expand Down Expand Up @@ -35,42 +35,64 @@ CssFile.prototype.selectorFromPosition = function(line, column){
};

CssFile.prototype.setContent = function(source, callback){
var messages = csslint.verify(source).messages;
var errors = [];
messages.forEach(function(msg){
if(msg.type == 'error'){
errors.push(msg);
const errors = [];
rei-nert marked this conversation as resolved.
Show resolved Hide resolved
this.source = source;
const lint = stylelint.lint({code: source}).then((result) => result.results).then(results => {
results.forEach((msg) =>{
if(msg.errored){
msg.warnings.forEach(warning => {
errors.push(warning)
});
}
})
return results
}).then(results => {
if(errors.length > 0 && callback){
throw new Error("Errors found")
rei-nert marked this conversation as resolved.
Show resolved Hide resolved
}
});
return results
}).catch(err => {
return err
})
rei-nert marked this conversation as resolved.
Show resolved Hide resolved

if(errors.length > 0 && callback){
callback(errors);
return;
const parser = new Promise((res, rej) => {
try {
const parsed = cssparser.parse(source)
rei-nert marked this conversation as resolved.
Show resolved Hide resolved
this.parsed = parsed
res(parsed)
}
catch(err){
rej(err);}
}
).then((parsed) => {
for (const rule of parsed.nodes) {
let source = rule.source;
source.start.line--;
source.start.column--;
source.end.column++;
}
}).catch(err => {
return err
})

var changed = (this.source != undefined && this.source != source);

this.source = source;

try{
this.parsed = cssparser.parse(source);
}catch(err){
callback(err);
return;
Promise.all([lint, parser]).then(result => {
var changed = (this.source != undefined && this.source != source);
if(changed){
callback(null);
}else{
callback(null, null);
}
}
).catch(err => {
rei-nert marked this conversation as resolved.
Show resolved Hide resolved
if (err === "Errors found") {
callback(errors)
}else {
callback(err)
}
})

for (const rule of this.parsed.nodes) {
let source = rule.source;
source.start.line--;
source.start.column--;
source.end.column++;
}
return
rei-nert marked this conversation as resolved.
Show resolved Hide resolved

if(changed){
callback(null);
}else{
callback(null, null);
}
};
}

module.exports = CssFile;
Loading