GithubHelp home page GithubHelp logo

Compiler/Line Number Metadata about opal-rspec HOT 4 CLOSED

opal avatar opal commented on July 18, 2024
Compiler/Line Number Metadata

from opal-rspec.

Comments (4)

wied03 avatar wied03 commented on July 18, 2024

@elia - Sound interesting? I think it could start as a monkey patch but maybe the compiler could add a framework some day.

from opal-rspec.

iliabylich avatar iliabylich commented on July 18, 2024

Does opal-rspec support latest opal master? If it does you can do it with a custom AST rewriter:

class RSpecMetadataRewriter < Opal::Rewriters::Base
  RSPEC_METHODS = %i(describe context it)

  def on_send(node)
    node = super

    recv, meth, *args = *node

    if RSPEC_METHODS.include?(meth)
      file = node.loc.expression.source_buffer.name
      line = node.loc.line
      column = node.loc.column

      loc_metadata = "#{file}:#{line}:#{column}"
      loc_metadata_pair = s(:pair, s(:sym, :loc), s(:str, loc_metadata))

      if args.last.type == :iter
        *args, iter = *args
      end

      if args.last.type == :hash
        *args, last_hash_arg = *args
      else
        last_hash_arg = s(:hash)
      end

      pairs = *last_hash_arg

      pairs += [loc_metadata_pair]
      last_hash_arg = last_hash_arg.updated(nil, pairs)


      args = [*args, last_hash_arg, iter].compact

      node.updated(nil, [recv, meth, *args])
    else
      node
    end
  end
end

Opal::Rewriter::LIST << RSpecMetadataRewriter

test.rb:

def describe(*args)
  puts "describe"
  puts args
  yield
end

def context(*args)
  puts "context"
  puts args
  yield
end

def it(*args)
  puts "it"
  puts args
end

describe 'MyClass1', type: :some_type do
  context 'when something happens' do
    it 'does the following'
  end
end
➜  opal git:(master) ✗ opal test.rb
describe
["MyClass1", {"type"=>"some_type", "loc"=>"test:18:0"}]
context
["when something happens", {"loc"=>"test:19:2"}]
it
["does the following", {"loc"=>"test:20:4"}]

➜  opal git:(master) ✗ opal ../opal/test.rb
describe
["MyClass1", {"type"=>"some_type", "loc"=>"../opal/test:18:0"}]
context
["when something happens", {"loc"=>"../opal/test:19:2"}]
it
["does the following", {"loc"=>"../opal/test:20:4"}]

from opal-rspec.

wied03 avatar wied03 commented on July 18, 2024

It did but it's currently broken (Sprockets is the first reason).

from opal-rspec.

elia avatar elia commented on July 18, 2024

I'm inclined to rely on source-maps for browser and possibly for node too.

from opal-rspec.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.