Skip to content

Location data for functions should include the ending semicolon #4584

@alangpierce

Description

@alangpierce

This is kind of a subtle issue involving location data, so it probably only affects tools for CoffeeScript rather than ever causing incorrect CoffeeScript output.

With code like this:

->
  ;

the location for the function ends at one character before the semicolon rather than it including the semicolon. This caused a bug in decaffeinate, and I ended up working around it with a decaffeinate-specific change, but figure it would be good to leave a note here.

More context here: decaffeinate/decaffeinate-parser#229

I think this is related to my change involving OUTDENT tokens here: #4296

I spent a little time digging and the issue is that semicolon tokens are removed in the normalizeLines step of the rewriter, which means that fixOutdentLocationData pushes them left to the INDENT token. I tried just reordering the phases, but that broke tests. I think the right behavior is to include the TERMINATOR token in the function range if it's a semicolon, but not if it's a newline, so the ideal solution might be to distinguish those in the rewriter.

Here's how someone can repro it more precisely. In a project with CoffeeScript installed, run this in the node repl:

> require('coffee-script').nodes('->\n  ;').expressions[0]
Code {
  params: [],
  body: 
   Block {
     expressions: [],
     locationData: { first_line: 1, first_column: 0, last_line: 1, last_column: 1 } },
  bound: false,
  isGenerator: false,
  locationData: { first_line: 0, first_column: 0, last_line: 1, last_column: 1 } }

locationData in CoffeeScript is 0-indexed with an inclusive end, so both the Code node and the Block node end at the space just before the semicolon.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions