Skip to content
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

Ensure indentation is consistent #4312

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/lexer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ exports.Lexer = class Lexer
@indebt = 0 # The over-indentation at the current level.
@outdebt = 0 # The under-outdentation at the current level.
@indents = [] # The stack of all current indentation levels.
@indentLiteral = '' # The indentation
@ends = [] # The stack for pairing up tokens.
@tokens = [] # Stream of parsed tokens in the form `['TYPE', value, location data]`.
@seenFor = no # Used to recognize FORIN and FOROF tokens.
Expand Down Expand Up @@ -347,6 +348,12 @@ exports.Lexer = class Lexer
size = indent.length - 1 - indent.lastIndexOf '\n'
noNewlines = @unfinished()

newIndentLiteral = if size > 0 then indent[-size..] else ''
minLiteralLength = Math.min newIndentLiteral.length, @indentLiteral.length
if newIndentLiteral[...minLiteralLength] isnt @indentLiteral[...minLiteralLength]
@error 'indentation mismatch', offset: indent.length
return indent.length

if size - @indebt is @indent
if noNewlines then @suppressNewlines() else @newlineToken 0
return indent.length
Expand All @@ -358,13 +365,15 @@ exports.Lexer = class Lexer
return indent.length
unless @tokens.length
@baseIndent = @indent = size
@indentLiteral = newIndentLiteral
return indent.length
diff = size - @indent + @outdebt
@token 'INDENT', diff, indent.length - size, size
@indents.push diff
@ends.push {tag: 'OUTDENT'}
@outdebt = @indebt = 0
@indent = size
@indentLiteral = newIndentLiteral
else if size < @baseIndent
@error 'missing indentation', offset: indent.length
else
Expand Down Expand Up @@ -401,6 +410,7 @@ exports.Lexer = class Lexer

@token 'TERMINATOR', '\n', outdentLength, 0 unless @tag() is 'TERMINATOR' or noNewlines
@indent = decreasedIndent
@indentLiteral = @indentLiteral[...decreasedIndent]
this

# Matches and consumes non-meaningful whitespace. Tag the previous token
Expand Down
2 changes: 1 addition & 1 deletion test/compilation.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ test "#2516: Unicode spaces should not be part of identifiers", ->
eq 5, {c: 5}[ 'c' ]

# A line where every space in non-breaking
  eq 1 + 12  
eq 1 + 12  

test "don't accidentally stringify keywords", ->
ok (-> this == 'this')() is false
Expand Down
30 changes: 30 additions & 0 deletions test/formatting.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,33 @@ test "#1275: allow indentation before closing brackets", ->
a = 1
)
eq 1, a

test "don’t allow mixing of spaces and tabs for indentation", ->
try
CoffeeScript.compile '''
new Layer
x: 0
y: 1
'''
ok no
catch e
eq 'indentation mismatch', e.message

test "indentation can be a mix of spaces and tabs, if each line is the same", ->
doesNotThrow ->
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
'''