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

WIP: Output JS-compatible line numbers #5277

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
6 changes: 3 additions & 3 deletions lib/coffeescript/coffeescript.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions lib/coffeescript/lexer.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/coffeescript.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# contains the main entry functions for tokenizing, parsing, and compiling
# source CoffeeScript into JavaScript.

{Lexer} = require './lexer'
{Lexer, LINEBREAK} = require './lexer'
{parser} = require './parser'
helpers = require './helpers'
SourceMap = require './sourcemap'
Expand Down Expand Up @@ -114,7 +114,7 @@ exports.compile = compile = withPrettyErrors (code, options = {}) ->
# `clean` function in the lexer).
if options.ast
nodes.allCommentTokens = helpers.extractAllCommentTokens tokens
sourceCodeNumberOfLines = (code.match(/\r?\n/g) or '').length + 1
sourceCodeNumberOfLines = (code.match(LINEBREAK) or []).length + 1
sourceCodeLastLine = /.*$/.exec(code)[0] # `.*` matches all but line break characters.
ast = nodes.ast options
range = [0, code.length]
Expand Down
6 changes: 4 additions & 2 deletions src/lexer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1056,11 +1056,11 @@ exports.Lexer = class Lexer
else
string = @chunk[..offset-1]

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

column = @chunkColumn
if lineCount > 0
[..., lastLine] = string.split '\n'
[..., lastLine] = string.split LINEBREAK
column = lastLine.length
previousLinesCompensation = @getLocationDataCompensation @chunkOffset, @chunkOffset + offset - column
# Don't recompensate for initially inserted newline.
Expand Down Expand Up @@ -1318,6 +1318,8 @@ OPERATOR = /// ^ (

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

exports.LINEBREAK = LINEBREAK = /\r\n|[\r\n\u2028\u2029]/ug

COMMENT = /^(\s*)###([^#][\s\S]*?)(?:###([^\n\S]*)|###$)|^((?:\s*#(?!##[^#]).*)+)/

CODE = /^[-=]>/
Expand Down