-
Notifications
You must be signed in to change notification settings - Fork 510
Ability to autoconfigure JSCS from a sample file #341
Comments
But that wouldn't make it harder for humans (aka the contributors) parse that file? :D |
Contributors don't read Example "disallowSpacesInFunctionExpression": {
"beforeOpeningRoundBrace": true,
"beforeOpeningCurlyBrace": true
} Valid function a(){} Invalid function a () {}
function a (){} In other words they translate rule for robots to a valid example for humans. But we can have such example from the very beginning. |
And if you are creating the brand new Code style. The only thing you need is to manually format code reference. It is much easier than look up for suitable rules. |
Looking at that code style:
It's a problem of how much can be interpreted from that file. In one extreme an empty ruleset is valid for that file whereas on the other extreme it will result in conflicting rules being enabled, just because the example file doesn't contain cases for them. Also with such example code style, new rules added to The only sane way to handle the rules is specifying them one by one... Now generating the initial |
This code is just a brief example of
As example. We can have function declaration for each rule: function undefinedExample (a) {
return typeof a === 'undefined';
} We can discuss this.
But this destroys |
So the real
With a configuration file of 100+ lines, finding the spot that specifies a rule (and thus has an example of it) becomes equally cumbersome.
Which is.. ?
Configuration needs to be:
What if the two options were combined? Support /* jscs: validateIndentation: 2 */
/* jscs: disallowSpacesInFunctionExpression: {
beforeOpeningRoundBrace: true,
beforeOpeningCurlyBrace: false
} */
function a() {
// No space before '(', space after '{'
/* jscs: requireKeywordsOnNewLine: else */
/* jscs: requireSpaceAfterKeywords: if, else */
if (true) {
//
}
else {
//
}
}
// Other options
/* jscs: disallowTrailingWhitespace */ This would be...
The main downside is using this format for quickly checking the wanted code style. The |
We can use ES6 imports to import rules: import {beforeOpeningRoundBrace} from 'http://jscs.org/rules/v1.3.4/common.js'; I like your example of // validate indentation: 2 spaes
// disallow spaces in function expression: before opening round brace
function a() {
// require keywords on new line: else
// require space after keywords: if, else
if (true) {
}
else {
}
}
// disallow trailing whitespace |
I guess this idea was suggested to a lot code analysis tools, but the problem with that idea and reason why all, as far i'm aware, those tools has lack of that feature - it's because logic of it is elusive and fuzzy and all&all - pretty hard to implement. But that said, we can start from the easy part (but still has to deal with a lot of icky stuff) and generate the rules (i.e. Although we have a lot on our plate right now, even above mentioned inline rules, should come up only in 2.1 (as it stands right now). |
👍 This would make JSCS a lot more approachable. And it would open up the possibility of running JSCS on a codebase without a |
Just stumbled upon this discussion, and not quite sure where I stand. the I had planned on taking a stab at resolving this dilemma, but didn't consider the |
what seems to be behind this idea is each rule having it's own method to detect it's setting value based on sample code. processNextJsFile(filename, code) {
rules.forEach(function(rule) {
if (rule.isSet) {
// current behavior
rule.validate(code);
} else if (rule.checkConsistency) {
if (!rule.tryToGuessFromSample) {
throw new Error('sorry this rule doesn\'t support auto detection: ' + rule.name);
}
rule.tryToGuessFromSample(code);
// if successful, now rule.isSet===true
}
});
} this would allow auto-detection to be gradually (and optionally) added to each rule. |
Thanks everyone for their input. I'm going to open a new issue here and close this one as it has gotten quite noisy here. |
I think
.json
config file is for robots. And we write this file because robots can't understand our code style from example. But they can do that and should.Imagine that you have a reference
.codestyle.js
file, written by a human for humans, and a robot can understand your code style and apply it for the reset of your files.This file could contain all common code-style cases.
The text was updated successfully, but these errors were encountered: