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
Created no-dangle rule. #87
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# no dangle | ||
|
||
Trailing commas in object literals are valid according to the ECMAScript 5 (and ECMAScript 3!) spec, however IE8 (when not in IE8 document mode) and below will throw an error when it encounters trailing commas in JavaScript. | ||
|
||
```js | ||
var foo = { | ||
bar: "baz", | ||
qux: "quux", | ||
}; | ||
``` | ||
|
||
## Rule Details | ||
|
||
This rule is aimed at detecting trailing commas in object literals. As such, it will warn whenever it encounters a trailing comma in an object literal. | ||
|
||
The following are considered warnings: | ||
|
||
```js | ||
var foo = { | ||
bar: "baz", | ||
qux: "quux", | ||
}; | ||
|
||
var arr = [1,2,]; | ||
|
||
foo({ | ||
bar: "baz", | ||
qux: "quux", | ||
}); | ||
``` | ||
|
||
The following are okay and will not raise warnings: | ||
|
||
```js | ||
var foo = { | ||
bar: "baz", | ||
qux: "quux" | ||
}; | ||
|
||
var arr = [1,2]; | ||
|
||
foo({ | ||
bar: "baz", | ||
qux: "quux" | ||
}); | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If your code will not be run in IE8 or below (a NodeJS application, for example) and you'd prefer to allow trailing commas, turn this rule off. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* @fileoverview Rule to flag trailing commas in object literals. | ||
* @author Ian Christian Myers | ||
*/ | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = function(context) { | ||
|
||
//------------------------------------------------------------------------- | ||
// Helpers | ||
//------------------------------------------------------------------------- | ||
|
||
function checkForTrailingComma(node) { | ||
var tokens = context.getTokens(node), | ||
secondToLastToken = tokens[tokens.length - 2]; | ||
|
||
// The last token in an object/array literal will always be a closing | ||
// curly, so we check the second to last token for a comma. | ||
if (secondToLastToken.value === ",") { | ||
context.report(node, "Found trailing comma in object literal."); | ||
} | ||
} | ||
|
||
//-------------------------------------------------------------------------- | ||
// Public API | ||
//-------------------------------------------------------------------------- | ||
|
||
return { | ||
"ObjectExpression": checkForTrailingComma, | ||
"ArrayExpression": checkForTrailingComma | ||
}; | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/** | ||
* @fileoverview Tests for no-dangle rule. | ||
* @author Ian Christian Myers | ||
*/ | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
var vows = require("vows"), | ||
assert = require("assert"), | ||
eslint = require("../../../lib/eslint"); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Constants | ||
//------------------------------------------------------------------------------ | ||
|
||
var RULE_ID = "no-dangle"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
vows.describe(RULE_ID).addBatch({ | ||
|
||
"when evaluating object literal with a dangling comma": { | ||
|
||
topic: "var foo = { bar: \"baz\", }", | ||
|
||
"should report a violation": function(topic) { | ||
var config = { rules: {} }; | ||
config.rules[RULE_ID] = 1; | ||
|
||
var messages = eslint.verify(topic, config); | ||
|
||
assert.equal(messages.length, 1); | ||
assert.equal(messages[0].ruleId, RULE_ID); | ||
assert.equal(messages[0].message, "Found trailing comma in object literal."); | ||
assert.include(messages[0].node.type, "ObjectExpression"); | ||
} | ||
}, | ||
|
||
"when evaluating object literal passed to a function with a dangling comma": { | ||
|
||
topic: "foo({ bar: \"baz\", qux: \"quux\", });", | ||
|
||
"should report a violation": function(topic) { | ||
var config = { rules: {} }; | ||
config.rules[RULE_ID] = 1; | ||
|
||
var messages = eslint.verify(topic, config); | ||
|
||
assert.equal(messages.length, 1); | ||
assert.equal(messages[0].ruleId, RULE_ID); | ||
assert.equal(messages[0].message, "Found trailing comma in object literal."); | ||
assert.include(messages[0].node.type, "ObjectExpression"); | ||
} | ||
}, | ||
|
||
|
||
"when evaluating object literal without a dangling comma": { | ||
|
||
topic: "var foo = { bar: \"baz\" }", | ||
|
||
"should not report a violation": function(topic) { | ||
var config = { rules: {} }; | ||
config.rules[RULE_ID] = 1; | ||
|
||
var messages = eslint.verify(topic, config); | ||
|
||
assert.equal(messages.length, 0); | ||
} | ||
}, | ||
|
||
"when evaluating array literal with a dangling comma": { | ||
|
||
topic: "var foo = [ \"baz\", ]", | ||
|
||
"should report a violation": function(topic) { | ||
var config = { rules: {} }; | ||
config.rules[RULE_ID] = 1; | ||
|
||
var messages = eslint.verify(topic, config); | ||
|
||
assert.equal(messages.length, 1); | ||
assert.equal(messages[0].ruleId, RULE_ID); | ||
assert.equal(messages[0].message, "Found trailing comma in object literal."); | ||
assert.include(messages[0].node.type, "ArrayExpression"); | ||
} | ||
}, | ||
|
||
"when evaluating array literal without a dangling comma": { | ||
|
||
topic: "var foo = [ \"baz\" ]", | ||
|
||
"should not report a violation": function(topic) { | ||
var config = { rules: {} }; | ||
config.rules[RULE_ID] = 1; | ||
|
||
var messages = eslint.verify(topic, config); | ||
|
||
assert.equal(messages.length, 0); | ||
} | ||
} | ||
|
||
}).export(module); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or square bracket