Skip to content

Instantly share code, notes, and snippets.

@GaryJones
Last active November 2, 2018 19:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GaryJones/040a3091f09ed6142056 to your computer and use it in GitHub Desktop.
Save GaryJones/040a3091f09ed6142056 to your computer and use it in GitHub Desktop.
JSCS config file
{
"requireSpacesInConditionalExpression": true,
"requireSpacesInFunction": {
"beforeOpeningCurlyBrace": true
},
"disallowSpacesInFunction": {
"beforeOpeningRoundBrace": true
},
"requireMultipleVarDecl": "onevar",
"requireSpacesInsideObjectBrackets": "all",
"disallowSpaceAfterObjectKeys": true,
"requireSpaceAfterBinaryOperators": [
"=", "+=", "-=", "*=", "/=", "%=", "<<=", ">>=", ">>>=",
"&=", "|=", "^=", "+=",
"+", "-", "*", "/", "%", "<<", ">>", ">>>", "&",
"|", "^", "&&", "||", "===", "==", ">=",
"<=", "<", ">", "!=", "!=="
],
"requireSpaceBeforeBinaryOperators": [
"=", "+=", "-=", "*=", "/=", "%=", "<<=", ">>=", ">>>=",
"&=", "|=", "^=", "+=",
"+", "-", "*", "/", "%", "<<", ">>", ">>>", "&",
"|", "^", "&&", "||", "===", "==", ">=",
"<=", "<", ">", "!=", "!=="
],
"disallowKeywords": ["with"],
"disallowMultipleLineBreaks": true,
"validateLineBreaks": "LF",
"disallowMixedSpacesAndTabs": "smart",
"disallowTrailingWhitespace": true,
"requireCurlyBraces": [ "if", "else", "for", "while", "do", "try", "catch" ],
"requireSpaceBeforeBlockStatements": true,
"requireParenthesesAroundIIFE": true,
"requireBlocksOnNewline": true,
"requireOperatorBeforeLineBreak": [
"?",
"=",
"+",
"-",
"/",
"*",
"==",
"===",
"!=",
"!==",
">",
">=",
"<",
"<="
],
"requireSpaceBeforeBinaryOperators": [
"?",
"=",
"+",
"-",
"/",
"*",
"==",
"===",
"!=",
"!==",
">",
">=",
"<",
"<="
],
"requireSpaceAfterBinaryOperators": [
"?",
"=",
"+",
"/",
"*",
":",
"==",
"===",
"!=",
"!==",
">",
">=",
"<",
"<="
],
"disallowSpaceBeforeBinaryOperators": [","],
"disallowSpaceAfterBinaryOperators": [],
"disallowSpaceAfterPrefixUnaryOperators": ["++", "--", "+", "-", "~"],
"requireSpaceAfterPrefixUnaryOperators": ["!"],
"disallowSpaceBeforePostfixUnaryOperators": ["++", "--"],
"requireCamelCaseOrUpperCaseIdentifiers": true,
"disallowMultipleLineStrings": true,
"validateQuoteMarks": "'",
"validateIndentation": "\t",
"requireLineFeedAtFileEnd": true,
"requireDotNotation": true,
"disallowNewlineBeforeBlockStatements": true,
"disallowTrailingComma": true,
"disallowPaddingNewlinesInBlocks": true,
"disallowEmptyBlocks": true,
"disallowQuotedKeysInObjects": "allButReserved",
"disallowDanglingUnderscores": true,
"requireCommaBeforeLineBreak": true,
"disallowKeywordsOnNewLine": ["else"],
"requireCapitalizedConstructors": true,
"safeContextKeyword": [ "that" ],
"validateJSDoc": {
"checkParamNames": true,
"checkRedundantParams": true,
"requireParamTypes": true
}
}
@GaryJones
Copy link
Author

JSHint is moving away from code standards and more towards highlighting
bad coding practices. It recommends JSCS as the replacement for
detecting whitespace, alignment and other code standards issues.

WP core hasn't yet got on to this yet, so there's no existing config to
go from. However, the JavaScript code standards in the WP handbook do
give quite clear outlines on what is expected. Using that, and going
through each possible rule in JSCS, I've come up with a config that I
think works, and would be good for core (albeit core doesn't follow it's
own standards).

The top section are rules that agree with jQuery standard, which is what
WP generally follows. This was found in the jQuery preset for JSCS. We
can't use the jQuery preset and then extend it, as it includes
requiresSpacesInsideArrayBrackets: true, which we don't follow without
exceptions, and there's no way I've found to nullify that rule, hence
the need to duplicate some of the rules.

The second section (starting line 34) are rules which match up to the existing WP JS code
standards in the handbook.

The last section (starting line 98) are rules that aren't in WP JS code standards, but I
think should be. These could always be counted as additionalRules in the
context of the grunt task, should WP core decide not to include them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment