Skip to content

Commit

Permalink
Fix constants/gets not working within a Provider
Browse files Browse the repository at this point in the history
  • Loading branch information
jansul committed Jul 22, 2023
1 parent 1a9f3f4 commit d3157b4
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 3 deletions.
102 changes: 102 additions & 0 deletions spec/compilers/provider_with_items
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
record Subscription {
a : Bool,
b : Bool
}

provider Provider : Subscription {
const NAME = "hello"

state a : String = ""

get b : String {
a
}

fun name : String {
NAME
}
}

component Main {
use Provider {
a: true,
b: false
}

fun render {
<div/>
}
}
--------------------------------------------------------------------------------
const A = _R({
a: [
"a",
Decoder.boolean
],
b: [
"b",
Decoder.boolean
]
});

const B = new(class extends _P {
constructor() {
super();

this.state = {
c: ``
};

this._d({
b: () => {
return `hello`
}
});
}

get c() {
return this.state.c;
}

get d() {
return this.c
}

a() {
return this.b;
}
});

class C extends _C {
componentWillUnmount() {
B._unsubscribe(this);
}

componentDidUpdate() {
if (true) {
B._subscribe(this, new A({
a: true,
b: false
}))
} else {
B._unsubscribe(this)
};
}

componentDidMount() {
if (true) {
B._subscribe(this, new A({
a: true,
b: false
}))
} else {
B._unsubscribe(this)
};
}

render() {
return _h("div", {});
}
};

C.displayName = "Main";
24 changes: 24 additions & 0 deletions spec/formatters/constant_provider
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
record Subscription {
a : Bool
}

provider Provider:Subscription {
/* Comment */constNAME="hello"

fun name:String {
NAME
}
}
--------------------------------------------------------------------------------
record Subscription {
a : Bool
}

provider Provider : Subscription {
/* Comment */
const NAME = "hello"

fun name : String {
NAME
}
}
34 changes: 34 additions & 0 deletions spec/formatters/provider_with_items
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
record Subscription {
a : Bool
}

provider Provider:Subscription {
/* Comment */constNAME="hello"

state a :String = ""
getb:String {
a
}
fun name:String {
NAME
}
}
--------------------------------------------------------------------------------
record Subscription {
a : Bool
}

provider Provider : Subscription {
/* Comment */
const NAME = "hello"

state a : String = ""

get b : String {
a
}

fun name : String {
NAME
}
}
24 changes: 24 additions & 0 deletions spec/type_checking/provider_const
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
record Subscription {
a : Bool
}

provider Provider : Subscription {
const ONE_PLUS_ONE = 1 + 1
const TWO = 2

fun test : Bool {
ONE_PLUS_ONE == TWO
}
}
-----------------------------------------------------------------VariableMissing
record Subscription {
a : Bool
}

provider Provider : Subscription {
const TWO = 2

fun test : Bool {
1 + TWO == THREE
}
}
2 changes: 1 addition & 1 deletion src/formatters/provider.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Mint
node.subscription

body =
list node.functions + node.comments + node.states + node.gets
list node.functions + node.comments + node.states + node.gets + node.constants

comment =
node.comment.try { |item| "#{format(item)}\n" }
Expand Down
2 changes: 1 addition & 1 deletion src/parsers/provider.cr
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module Mint
opening_bracket: ProviderExpectedOpeningBracket,
closing_bracket: ProviderExpectedClosingBracket
) do
items = many { function || state || constant || self.comment }
items = many { function || state || get || constant || self.comment }

raise ProviderExpectedBody if items.none?(Ast::Function)

Expand Down
2 changes: 1 addition & 1 deletion src/type_checker/scope.cr
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ module Mint
node.functions.find(&.name.value.==(variable)) ||
node.states.find(&.name.value.==(variable)) ||
node.gets.find(&.name.value.==(variable)) ||
node.constants.find(&.name.==(variable))
node.constants.find(&.name.value.==(variable))
end
end

Expand Down

0 comments on commit d3157b4

Please sign in to comment.