Skip to content

Commit

Permalink
Don’t allow mixing different types of whitespace for indentation, per…
Browse files Browse the repository at this point in the history
… line.
  • Loading branch information
eelco committed Sep 20, 2016
1 parent 9806861 commit bb40b11
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 37 deletions.
4 changes: 4 additions & 0 deletions src/lexer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,10 @@ exports.Lexer = class Lexer
noNewlines = @unfinished()

newIndentLiteral = if size > 0 then indent[-size..] else ''
unless /^(.?)\1*$/.exec newIndentLiteral
@error 'mixed indentation', offset: indent.length
return indent.length

minLiteralLength = Math.min newIndentLiteral.length, @indentLiteral.length
if newIndentLiteral[...minLiteralLength] isnt @indentLiteral[...minLiteralLength]
@error 'indentation mismatch', offset: indent.length
Expand Down
12 changes: 0 additions & 12 deletions test/error_messages.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,6 @@ test "compiler error formatting", ->
^^^^
'''

test "compiler error formatting with mixed tab and space", ->
assertErrorFormat """
\t if a
\t test
""",
'''
[stdin]:1:4: error: unexpected if
\t if a
\t ^^
'''


if require?
fs = require 'fs'
path = require 'path'
Expand Down
27 changes: 15 additions & 12 deletions test/formatting.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -266,21 +266,24 @@ test "don’t allow mixing of spaces and tabs for indentation", ->
catch e
eq 'indentation mismatch', e.message

test "indentation can be a mix of spaces and tabs, if each line is the same", ->
test "each code block that starts at indentation 0 can use a different style", ->
doesNotThrow ->
CoffeeScript.compile '''
new Layer
x: 0
y: 1
new Layer
x: 0
y: 1
'''

test "tabs and spaces cannot be mixed for indentation", ->
try
CoffeeScript.compile '''
new Layer
x: 0
y: 1
'''

test "each code block that starts at indentation 0 can use a different style", ->
doesNotThrow ->
CoffeeScript.compile '''
new Layer
x: 0
y: 1
new Layer
x: 0
y: 1
'''
ok no
catch e
eq 'mixed indentation', e.message
26 changes: 13 additions & 13 deletions test/scope.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,21 @@ test "#1183: super + fat arrows", ->
dolater = (cb) -> cb()

class A
constructor: ->
@_i = 0
foo : (cb) ->
dolater =>
@_i += 1
cb()
constructor: ->
@_i = 0
foo : (cb) ->
dolater =>
@_i += 1
cb()

class B extends A
constructor : ->
super
foo : (cb) ->
dolater =>
dolater =>
@_i += 2
super cb
constructor : ->
super
foo : (cb) ->
dolater =>
dolater =>
@_i += 2
super cb

b = new B
b.foo => eq b._i, 3
Expand Down

0 comments on commit bb40b11

Please sign in to comment.