Skip to content

Commit bb40b11

Browse files
committed
Don’t allow mixing different types of whitespace for indentation, per line.
1 parent 9806861 commit bb40b11

File tree

4 files changed

+32
-37
lines changed

4 files changed

+32
-37
lines changed

src/lexer.coffee

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,10 @@ exports.Lexer = class Lexer
349349
noNewlines = @unfinished()
350350

351351
newIndentLiteral = if size > 0 then indent[-size..] else ''
352+
unless /^(.?)\1*$/.exec newIndentLiteral
353+
@error 'mixed indentation', offset: indent.length
354+
return indent.length
355+
352356
minLiteralLength = Math.min newIndentLiteral.length, @indentLiteral.length
353357
if newIndentLiteral[...minLiteralLength] isnt @indentLiteral[...minLiteralLength]
354358
@error 'indentation mismatch', offset: indent.length

test/error_messages.coffee

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,6 @@ test "compiler error formatting", ->
4141
^^^^
4242
'''
4343

44-
test "compiler error formatting with mixed tab and space", ->
45-
assertErrorFormat """
46-
\t if a
47-
\t test
48-
""",
49-
'''
50-
[stdin]:1:4: error: unexpected if
51-
\t if a
52-
\t ^^
53-
'''
54-
55-
5644
if require?
5745
fs = require 'fs'
5846
path = require 'path'

test/formatting.coffee

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -266,21 +266,24 @@ test "don’t allow mixing of spaces and tabs for indentation", ->
266266
catch e
267267
eq 'indentation mismatch', e.message
268268

269-
test "indentation can be a mix of spaces and tabs, if each line is the same", ->
269+
test "each code block that starts at indentation 0 can use a different style", ->
270270
doesNotThrow ->
271+
CoffeeScript.compile '''
272+
new Layer
273+
x: 0
274+
y: 1
275+
new Layer
276+
x: 0
277+
y: 1
278+
'''
279+
280+
test "tabs and spaces cannot be mixed for indentation", ->
281+
try
271282
CoffeeScript.compile '''
272283
new Layer
273284
x: 0
274285
y: 1
275286
'''
276-
277-
test "each code block that starts at indentation 0 can use a different style", ->
278-
doesNotThrow ->
279-
CoffeeScript.compile '''
280-
new Layer
281-
x: 0
282-
y: 1
283-
new Layer
284-
x: 0
285-
y: 1
286-
'''
287+
ok no
288+
catch e
289+
eq 'mixed indentation', e.message

test/scope.coffee

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,21 @@ test "#1183: super + fat arrows", ->
5959
dolater = (cb) -> cb()
6060

6161
class A
62-
constructor: ->
63-
@_i = 0
64-
foo : (cb) ->
65-
dolater =>
66-
@_i += 1
67-
cb()
62+
constructor: ->
63+
@_i = 0
64+
foo : (cb) ->
65+
dolater =>
66+
@_i += 1
67+
cb()
6868

6969
class B extends A
70-
constructor : ->
71-
super
72-
foo : (cb) ->
73-
dolater =>
74-
dolater =>
75-
@_i += 2
76-
super cb
70+
constructor : ->
71+
super
72+
foo : (cb) ->
73+
dolater =>
74+
dolater =>
75+
@_i += 2
76+
super cb
7777

7878
b = new B
7979
b.foo => eq b._i, 3

0 commit comments

Comments
 (0)