Skip to content

Commit 768fd6e

Browse files
fix: Fix source locations for Flatfile DSL.
1 parent 1e99c8f commit 768fd6e

File tree

3 files changed

+119
-9
lines changed

3 files changed

+119
-9
lines changed

rewriter/Flatfiles.cc

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
using namespace std;
1111

1212
namespace sorbet::rewriter {
13-
optional<core::NameRef> getFieldName(core::MutableContext ctx, ast::Send &send) {
13+
optional<pair<core::NameRef, core::LocOffsets>> getFieldName(core::MutableContext ctx, ast::Send &send) {
1414
if (auto propLit = ast::cast_tree<ast::Literal>(send.getPosArg(0))) {
1515
if (propLit->isSymbol()) {
16-
return propLit->asSymbol();
16+
return make_pair(propLit->asSymbol(), propLit->loc);
1717
}
1818
}
1919
if (send.numPosArgs() >= 2) {
2020
if (auto propLit = ast::cast_tree<ast::Literal>(send.getPosArg(1))) {
2121
if (propLit->isSymbol()) {
22-
return propLit->asSymbol();
22+
return make_pair(propLit->asSymbol(), propLit->loc);
2323
}
2424
}
2525
}
@@ -42,20 +42,21 @@ void handleFieldDefinition(core::MutableContext ctx, ast::ExpressionPtr &stat, v
4242
!send->recv.isSelfReference() || send->numPosArgs() < 1) {
4343
return;
4444
}
45-
auto name = getFieldName(ctx, *send);
46-
if (!name) {
45+
auto nameAndLoc = getFieldName(ctx, *send);
46+
if (!nameAndLoc) {
4747
return;
4848
}
49+
auto [name, nameLoc] = nameAndLoc.value();
4950

5051
methods.emplace_back(ast::MK::Sig0(send->loc, ast::MK::Untyped(send->loc)));
51-
methods.emplace_back(
52-
ast::MK::SyntheticMethod0(send->loc, send->loc, send->loc, *name, ast::MK::Nil(send->loc)));
52+
53+
methods.emplace_back(ast::MK::SyntheticMethod0(send->loc, send->loc, nameLoc, name, ast::MK::Nil(send->loc)));
5354
auto var = ast::MK::Local(send->loc, core::Names::arg0());
54-
auto setName = name->addEq(ctx);
55+
auto setName = name.addEq(ctx);
5556
methods.emplace_back(ast::MK::Sig1(send->loc, ast::MK::Symbol(send->loc, core::Names::arg0()),
5657
ast::MK::Untyped(send->loc), ast::MK::Untyped(send->loc)));
5758
methods.emplace_back(
58-
ast::MK::SyntheticMethod1(send->loc, send->loc, send->loc, setName, move(var), ast::MK::Nil(send->loc)));
59+
ast::MK::SyntheticMethod1(send->loc, send->loc, nameLoc, setName, move(var), ast::MK::Nil(send->loc)));
5960
}
6061
}
6162

test/scip/testdata/flatfile_dsl.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# typed: true
2+
3+
class Record
4+
def self.flatfile; end
5+
def self.from(*_); end
6+
def self.pattern(*_); end
7+
def self.field(*_); end
8+
end
9+
10+
class Flatfile < Record
11+
flatfile do
12+
from 1..2, :foo
13+
pattern(/A-Za-z/, :bar)
14+
field :baz
15+
end
16+
end
17+
18+
t = Flatfile.new
19+
t.foo = t.foo + 1
20+
t.bar = t.bar + 1
21+
t.baz = t.baz + 1
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# typed: true
2+
3+
class Record
4+
# ^^^^^^ definition [..] Record#
5+
def self.flatfile; end
6+
# ^^^^^^^^ definition [..] `<Class:Record>`#flatfile().
7+
def self.from(*_); end
8+
# ^^^^ definition [..] `<Class:Record>`#from().
9+
def self.pattern(*_); end
10+
# ^^^^^^^ definition [..] `<Class:Record>`#pattern().
11+
def self.field(*_); end
12+
# ^^^^^ definition [..] `<Class:Record>`#field().
13+
end
14+
15+
class Flatfile < Record
16+
# ^^^^^^^^ definition [..] Flatfile#
17+
# ^^^^^^ definition [..] Record#
18+
flatfile do
19+
from 1..2, :foo
20+
# ^^^^^^^^^^^^^^^^^ reference [..] Sorbet#Private#`<Class:Static>`#sig().
21+
# ^^^^^^^^^^^^^^^^^ reference [..] Sorbet#Private#Static#
22+
# ^^^^^^^^^^^^^^^^^ reference [..] T#Private#Methods#DeclBuilder#returns().
23+
# ^^^^^^^^^^^^^^^^^ reference [..] `<Class:T>`#untyped().
24+
# ^^^^^^^^^^^^^^^^^ reference [..] T#
25+
# ^^^^^^^^^^^^^^^^^ reference [..] T#Private#Methods#DeclBuilder#params().
26+
# ^^^^^^^^^^^^^^^^^ reference [..] `<Class:T>`#untyped().
27+
# ^^^^^^^^^^^^^^^^^ reference [..] T#
28+
# ^^^^^^^^^^^^^^^^^ reference [..] Sorbet#Private#Static#
29+
# ^^^^^^^^^^^^^^^^^ reference [..] T#Private#Methods#DeclBuilder#returns().
30+
# ^^^^^^^^^^^^^^^^^ reference [..] `<Class:T>`#untyped().
31+
# ^^^^^^^^^^^^^^^^^ reference [..] T#
32+
# ^^^^^^^^^^^^^^^^^ reference [..] Sorbet#Private#`<Class:Static>`#sig().
33+
# ^^^^ definition [..] Flatfile#`foo=`().
34+
# ^^^^ definition [..] Flatfile#foo().
35+
pattern(/A-Za-z/, :bar)
36+
# ^^^^^^^^^^^^^^^^^^^^^^^ reference [..] T#Private#Methods#DeclBuilder#returns().
37+
# ^^^^^^^^^^^^^^^^^^^^^^^ reference [..] Sorbet#Private#Static#
38+
# ^^^^^^^^^^^^^^^^^^^^^^^ reference [..] T#Private#Methods#DeclBuilder#returns().
39+
# ^^^^^^^^^^^^^^^^^^^^^^^ reference [..] `<Class:T>`#untyped().
40+
# ^^^^^^^^^^^^^^^^^^^^^^^ reference [..] T#
41+
# ^^^^^^^^^^^^^^^^^^^^^^^ reference [..] T#Private#Methods#DeclBuilder#params().
42+
# ^^^^^^^^^^^^^^^^^^^^^^^ reference [..] `<Class:T>`#untyped().
43+
# ^^^^^^^^^^^^^^^^^^^^^^^ reference [..] T#
44+
# ^^^^^^^^^^^^^^^^^^^^^^^ reference [..] Sorbet#Private#`<Class:Static>`#sig().
45+
# ^^^^^^^^^^^^^^^^^^^^^^^ reference [..] Sorbet#Private#Static#
46+
# ^^^^^^^^^^^^^^^^^^^^^^^ reference [..] `<Class:T>`#untyped().
47+
# ^^^^^^^^^^^^^^^^^^^^^^^ reference [..] T#
48+
# ^^^^^^^^^^^^^^^^^^^^^^^ reference [..] Sorbet#Private#`<Class:Static>`#sig().
49+
# ^^^^^^^^ reference [..] Regexp#
50+
# ^^^^ definition [..] Flatfile#`bar=`().
51+
# ^^^^ definition [..] Flatfile#bar().
52+
field :baz
53+
# ^^^^^^^^^^ reference [..] `<Class:T>`#untyped().
54+
# ^^^^^^^^^^ reference [..] `<Class:T>`#untyped().
55+
# ^^^^^^^^^^ reference [..] T#Private#Methods#DeclBuilder#returns().
56+
# ^^^^^^^^^^ reference [..] `<Class:T>`#untyped().
57+
# ^^^^^^^^^^ reference [..] T#
58+
# ^^^^^^^^^^ reference [..] T#Private#Methods#DeclBuilder#params().
59+
# ^^^^^^^^^^ reference [..] Sorbet#Private#`<Class:Static>`#sig().
60+
# ^^^^^^^^^^ reference [..] T#
61+
# ^^^^^^^^^^ reference [..] Sorbet#Private#Static#
62+
# ^^^^^^^^^^ reference [..] T#Private#Methods#DeclBuilder#returns().
63+
# ^^^^^^^^^^ reference [..] Sorbet#Private#Static#
64+
# ^^^^^^^^^^ reference [..] Sorbet#Private#`<Class:Static>`#sig().
65+
# ^^^^^^^^^^ reference [..] T#
66+
# ^^^^ definition [..] Flatfile#`baz=`().
67+
# ^^^^ definition [..] Flatfile#baz().
68+
end
69+
end
70+
71+
t = Flatfile.new
72+
#^ definition local 1~#119448696
73+
# ^^^^^^^^ reference [..] Flatfile#
74+
t.foo = t.foo + 1
75+
#^ reference local 1~#119448696
76+
# ^^^^^ reference [..] Flatfile#`foo=`().
77+
# ^ reference local 1~#119448696
78+
# ^^^ reference [..] Flatfile#foo().
79+
t.bar = t.bar + 1
80+
#^ reference local 1~#119448696
81+
# ^^^^^ reference [..] Flatfile#`bar=`().
82+
# ^ reference local 1~#119448696
83+
# ^^^ reference [..] Flatfile#bar().
84+
t.baz = t.baz + 1
85+
#^ reference local 1~#119448696
86+
# ^^^^^ reference [..] Flatfile#`baz=`().
87+
# ^ reference local 1~#119448696
88+
# ^^^ reference [..] Flatfile#baz().

0 commit comments

Comments
 (0)