GitHub

Original file line numberDiff line numberDiff line change

@@ -226,6 +226,7 @@ coffeelint.registerRule require './rules/no_this.coffee'

226226

coffeelint.registerRule require './rules/eol_last.coffee'

227227

coffeelint.registerRule require './rules/no_private_function_fat_arrows.coffee'

228228

coffeelint.registerRule require './rules/missing_parseint_radix.coffee'

229+

coffeelint.registerRule require './rules/object_shorthand.coffee'

229230
230231

getTokens = (source) ->

231232

try

Original file line numberDiff line numberDiff line change

@@ -0,0 +1,39 @@

1+

module.exports = class ObjectShorthand

2+

rule:

3+

name: 'object_shorthand'

4+

level: 'ignore'

5+

message: 'Use property-value shorthand when using explicit braces'

6+

description: '''

7+

<p>Use property value shorthand in objects, when explicit braces are used.</p>

8+

<pre><code>test = "value"

9+
10+

# Good

11+

{test}

12+

test: test

13+
14+

# Bad

15+

{test: test}

16+

</code></pre>

17+

'''

18+
19+

tokens: [':']

20+
21+

lintToken: (token, tokenApi) ->

22+

checkExplicit = ->

23+

current = -2

24+

while tokenApi.peek(current)[0] isnt '{'

25+

current--

26+
27+

return not tokenApi.peek(current).generated

28+
29+

# Get the property name and the value

30+

property = tokenApi.peek -1

31+

value = tokenApi.peek 1

32+
33+

# Check if we have explicit {}

34+

explicit = checkExplicit()

35+
36+

if explicit and property[1] is value[1]

37+

context: "Use '{#{property[1]}}'"

38+

else

39+

null

Original file line numberDiff line numberDiff line change

@@ -0,0 +1,45 @@

1+

path = require 'path'

2+

vows = require 'vows'

3+

assert = require 'assert'

4+

coffeelint = require path.join('..', 'lib', 'coffeelint')

5+
6+

RULE = 'object_shorthand'

7+
8+

vows.describe(RULE).addBatch({

9+
10+

'Object property-value shorthand':

11+

topic:

12+

'''

13+

test = 'value'

14+

a = {test: test}

15+

b = {

16+

t: 1

17+

tt: 2

18+

test: test

19+

}

20+
21+

c = test: test

22+

'''

23+
24+

'not required by default': (source) ->

25+

errors = coffeelint.lint(source)

26+

assert.isArray(errors)

27+

assert.isEmpty(errors)

28+
29+

'can be required': (source) ->

30+

config = object_shorthand: { level: 'error' }

31+

errors = coffeelint.lint(source, config)

32+

assert.isArray(errors)

33+

assert.lengthOf(errors, 2)

34+
35+

error = errors[0]

36+

assert.equal(error.lineNumber, 2)

37+

assert.equal(error.message, 'Use property-value shorthand when using explicit braces')

38+

assert.equal(error.rule, RULE)

39+
40+

error = errors[1]

41+

assert.equal(error.lineNumber, 6)

42+

assert.equal(error.message, 'Use property-value shorthand when using explicit braces')

43+

assert.equal(error.rule, RULE)

44+
45+

}).export(module)

Read the original on github.com ↗