ruby / ruby/syntax_suggest

Improve error message on trailing comma in method definition

Open
#205 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
350
Forks
17
Avg merge
48m
Merged PRs (30d)
5

Description

The error produced by this (invalid) code is confusing and not helpful:

def run_build_script(
   workspace_dir: ARGV[0],
   output_dir: ARGV[1],
   cache_dir: ARGV[2],
   stack: ENV.fetch("STACK"),
   ruby_version: ENV.fetch("STACK"),
   io: STDOUT,
)
 end
$ exe/syntax_suggest /tmp/bad.rb
--> /tmp/bad.rb

syntax error, unexpected ':', expecting end-of-input

  1  def run_build_script(
> 2     workspace_dir: ARGV[0],
> 3     output_dir: ARGV[1],
> 4     cache_dir: ARGV[2],
> 5     stack: ENV.fetch("STACK"),
> 6     ruby_version: ENV.fetch("STACK"),
> 7     io: STDOUT,
  8  )

The problem is that there's a trailing comma. This code works (when you remove the comma after STDOUT):

$ cat /tmp/bad.rb
def run_build_script(
   workspace_dir: ARGV[0],
   output_dir: ARGV[1],
   cache_dir: ARGV[2],
   stack: ENV.fetch("STACK"),
   ruby_version: ENV.fetch("STACK"),
   io: STDOUT
)
 end
$ exe/syntax_suggest /tmp/bad.rb
Syntax OK

Fix Idea(s)

I'm not sure how to approach this. Possibly we could check for some variation of this regex https://rubular.com/r/aocG0GtGFjEiG2 as a brute force check. There may be a more elegant option.

Sidebar: I would love to see trailing commas allowed in method definitions. I think adding this behavior is an issue for positional args as you want that feedback that your airity is off. Idea: Allow it if and only if the method ends in a kwarg (instead of a positional arg). I don't know if it's been discussed or not.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducer through exe/syntax_suggest using the /tmp/bad.rb example, then compare its output with the version where the trailing comma is removed. Trace the error-reporting path to determine where this case can receive a clearer diagnostic; done means the invalid example reports the trailing comma rather than the confusing unexpected-colon error.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.