Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix constants/gets not working within a Provider #621

Merged
merged 1 commit into from
Jul 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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