问题
I am using http://jscs.info. I need to get a warning if my app has left trailing comma, example, using:
var x = {
prop1: 10,
prop2: 20,
};
I should get a warning. With the following settings:
disallowTrailingComma true
requireTrailingComma true
I cannot get the warning.
What I am doing wrong here?
{
"disallowCommaBeforeLineBreak": null,
"disallowDanglingUnderscores": true,
"disallowEmptyBlocks": true,
"disallowImplicitTypeConversion": [ "string" ],
"disallowKeywordsOnNewLine": [ "else" ],
"disallowKeywords": [ "with" ],
"disallowMixedSpacesAndTabs": true,
"disallowMultipleLineBreaks": true,
"disallowMultipleLineStrings": true,
"disallowMultipleVarDecl": null,
"disallowPaddingNewlinesInBlocks": null,
"disallowQuotedKeysInObjects": true,
"disallowSpaceAfterBinaryOperators": true,
"disallowSpaceAfterKeywords": [ "for", "while", "do", "switch" ],
"disallowSpaceAfterLineComment": false,
"disallowSpaceAfterObjectKeys": null,
"disallowSpaceAfterPrefixUnaryOperators": true,
"disallowSpaceBeforeBinaryOperators": null,
"disallowSpaceBeforeBlockStatements": null,
"disallowSpaceBeforePostfixUnaryOperators": true,
"disallowSpacesInAnonymousFunctionExpression": {
"beforeOpeningCurlyBrace": true
},
"disallowSpacesInConditionalExpression": null,
"disallowSpacesInFunctionDeclaration": null,
"disallowSpacesInFunctionExpression": {
"beforeOpeningRoundBrace": true
},
"disallowSpacesInNamedFunctionExpression": null,
"disallowSpacesInsideArrayBrackets": null,
"disallowSpacesInsideObjectBrackets": null,
"disallowSpacesInsideParentheses": null,
"disallowTrailingComma": true, // -------------------null
"disallowTrailingWhitespace": true,
"disallowYodaConditions": true,
"maximumLineLength": 120,
"requireAlignedObjectValues": "skipWithFunction",
"requireBlocksOnNewline": true,
"requireCamelCaseOrUpperCaseIdentifiers": "ignoreProperties",
"requireCapitalizedConstructors": true,
"requireCommaBeforeLineBreak": true,
"requireCurlyBraces": [ "if", "else", "for", "while", "do", "try", "catch" ],
"requireDotNotation": true,
"requireKeywordsOnNewLine": null,
"requireLineFeedAtFileEnd": true,
"requireMultipleVarDecl": true,
"requireOperatorBeforeLineBreak": true,
"requirePaddingNewlinesInBlocks": true,
"requireParenthesesAroundIIFE": true,
"requireSpaceAfterBinaryOperators": null,
"requireSpaceAfterKeywords": [ "if", "else", "return", "try", "catch" ],
"requireSpaceAfterLineComment": null,
"requireSpaceAfterObjectKeys": true,
"requireSpaceAfterPrefixUnaryOperators": null,
"requireSpaceBeforeBinaryOperators": true,
"requireSpaceBeforeBlockStatements": true,
"requireSpaceBeforePostfixUnaryOperators": null,
"requireSpacesInAnonymousFunctionExpression": {
"beforeOpeningRoundBrace": true
},
"requireSpacesInConditionalExpression": true,
"requireSpacesInFunctionDeclaration": {
"beforeOpeningRoundBrace": true,
"beforeOpeningCurlyBrace": true
},
"requireSpacesInFunctionExpression": {
"beforeOpeningCurlyBrace": true
},
"requireSpacesInNamedFunctionExpression": {
"beforeOpeningRoundBrace": true,
"beforeOpeningCurlyBrace": true
},
"requireSpacesInsideArrayBrackets": "all",
"requireSpacesInsideObjectBrackets": "allButNested",
"requireSpacesInsideParentheses": "all",
"requireTrailingComma": true, // -------------------------true
"safeContextKeyword": true,
"validateIndentation": 4,
"validateJSDoc": {
"checkParamNames": true,
"requireParamTypes": true
},
"validateLineBreaks": "LF",
"validateQuoteMarks": true
}
回答1:
Your two options contradict each other. At the same time you require that there are trailing commas with "requireTrailingComma": true
and forbid the trailing comma with "disallowTrailingComma": true
.
To get the warning for trailing commas, remove the "requireTrailingComma": true
option and at the same time keep "disallowTrailingComma": true
.
UPDATE
I removed the following options that generated errors in console and it worked for your slightly modified example code, that only the trailing comma warning is displayed:
var x ={
prop1 : 10,
prop2 : 20,
};
I removed the following lines (shown together with their errors)
"disallowSpaceAfterLineComment": false
because of
AssertionError: disallowSpaceAfterLineComment option requires the value true
and
"safeContextKeyword": true
because
AssertionError: safeContextKeyword option requires string or array value
and
"requireTrailingComma": true
because of the contradiction described above.
来源:https://stackoverflow.com/questions/29120520/disallowtrailingcomma-does-not-work-in-jscs