Skip to content

Commit 771ea6e

Browse files
committed
use js-compatible linebreak regex
1 parent 9667be5 commit 771ea6e

File tree

4 files changed

+14
-10
lines changed

4 files changed

+14
-10
lines changed

lib/coffeescript/coffeescript.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/coffeescript/lexer.js

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/coffeescript.coffee

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# contains the main entry functions for tokenizing, parsing, and compiling
44
# source CoffeeScript into JavaScript.
55

6-
{Lexer} = require './lexer'
6+
{Lexer, LINEBREAK} = require './lexer'
77
{parser} = require './parser'
88
helpers = require './helpers'
99
SourceMap = require './sourcemap'
@@ -114,7 +114,7 @@ exports.compile = compile = withPrettyErrors (code, options = {}) ->
114114
# `clean` function in the lexer).
115115
if options.ast
116116
nodes.allCommentTokens = helpers.extractAllCommentTokens tokens
117-
sourceCodeNumberOfLines = (code.match(/\r?\n/g) or '').length + 1
117+
sourceCodeNumberOfLines = (code.match(LINEBREAK) or []).length + 1
118118
sourceCodeLastLine = /.*$/.exec(code)[0] # `.*` matches all but line break characters.
119119
ast = nodes.ast options
120120
range = [0, code.length]

src/lexer.coffee

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,11 +1056,11 @@ exports.Lexer = class Lexer
10561056
else
10571057
string = @chunk[..offset-1]
10581058

1059-
lineCount = count string, '\n'
1059+
lineCount = (string.match(LINEBREAK) or []).length
10601060

10611061
column = @chunkColumn
10621062
if lineCount > 0
1063-
[..., lastLine] = string.split '\n'
1063+
[..., lastLine] = string.split LINEBREAK
10641064
column = lastLine.length
10651065
previousLinesCompensation = @getLocationDataCompensation @chunkOffset, @chunkOffset + offset - column
10661066
# Don't recompensate for initially inserted newline.
@@ -1314,6 +1314,8 @@ OPERATOR = /// ^ (
13141314

13151315
WHITESPACE = /^[^\n\S]+/
13161316

1317+
exports.LINEBREAK = LINEBREAK = /\r\n|[\r\n\u2028\u2029]/ug
1318+
13171319
COMMENT = /^(\s*)###([^#][\s\S]*?)(?:###([^\n\S]*)|###$)|^((?:\s*#(?!##[^#]).*)+)/
13181320

13191321
CODE = /^[-=]>/

0 commit comments

Comments
 (0)