Skip to content

Commit ababc2b

Browse files
committed
Don't compile debug information when optimize flag is set.
1 parent fee860c commit ababc2b

File tree

3 files changed

+57
-37
lines changed

3 files changed

+57
-37
lines changed

spec/compilers/dbg_optimized

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
optimize: true
2+
================================================================================component Main {
3+
fun render : String {
4+
dbg "Hello World!"
5+
}
6+
}
7+
--------------------------------------------------------------------------------
8+
export const A=()=>{return `Hello World!`};

spec/compilers_spec.cr

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ require "./spec_helper"
33
struct Config
44
include YAML::Serializable
55

6-
getter exports : Array(String)
6+
getter exports = [] of String
7+
getter? optimize = false
8+
9+
def initialize
10+
end
711
end
812

913
Dir
@@ -24,15 +28,15 @@ Dir
2428

2529
{Config.from_yaml(parts[0]), parts[1]}
2630
else
27-
{nil, raw}
31+
{Config.new, raw}
2832
end
2933

3034
# Parse the sample
3135
ast = Mint::Parser.parse(sample, File.dirname(__FILE__) + file.lchop("./spec"))
3236
ast.class.should eq(Mint::Ast)
3337

3438
artifacts =
35-
Mint::TypeChecker.check(ast, config.try(&.exports) || [] of String)
39+
Mint::TypeChecker.check(ast, config.exports)
3640

3741
test_information =
3842
if File.basename(file).starts_with?("test")
@@ -43,15 +47,15 @@ Dir
4347
Mint::Bundler::Config.new(
4448
json: Mint::MintJson.parse("{}", "mint.json"),
4549
generate_source_maps: false,
50+
optimize: config.optimize?,
4651
generate_manifest: false,
4752
include_program: false,
4853
test: test_information,
4954
hash_routing: false,
5055
live_reload: false,
5156
runtime_path: nil,
5257
skip_icons: false,
53-
hash_assets: true,
54-
optimize: false)
58+
hash_assets: true)
5559

5660
files =
5761
Mint::Bundler.new(

src/compilers/dbg.cr

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,50 @@ module Mint
22
class Compiler
33
def compile(node : Ast::Dbg) : Compiled
44
compile node do
5-
location =
6-
js.string("#{node.file.relative_path}:#{node.from.line}:#{node.from.column}")
7-
8-
var =
9-
[Variable.new] of Item
10-
11-
arg =
12-
if node.bang?
13-
var
5+
if config.optimize
6+
if expression = node.expression
7+
compile(expression)
148
else
15-
js.call(Builtin::Inspect, [var])
16-
end
17-
18-
location =
19-
if config.generate_source_maps
209
[] of Item
21-
else
22-
js.call(["console.log"] of Item, [location])
23-
end
24-
25-
if expression = node.expression
26-
js.iif do
27-
js.statements([
28-
js.const(var, compile(expression)),
29-
location,
30-
js.call(["console.log"] of Item, [arg]),
31-
js.return(var),
32-
])
3310
end
3411
else
35-
js.arrow_function([var]) do
36-
js.statements([
37-
location,
38-
js.call(["console.log"] of Item, [arg]),
39-
js.return(var),
40-
])
12+
location =
13+
js.string("#{node.file.relative_path}:#{node.from.line}:#{node.from.column}")
14+
15+
var =
16+
[Variable.new] of Item
17+
18+
arg =
19+
if node.bang?
20+
var
21+
else
22+
js.call(Builtin::Inspect, [var])
23+
end
24+
25+
location =
26+
if config.generate_source_maps
27+
[] of Item
28+
else
29+
js.call(["console.log"] of Item, [location])
30+
end
31+
32+
if expression = node.expression
33+
js.iif do
34+
js.statements([
35+
js.const(var, compile(expression)),
36+
location,
37+
js.call(["console.log"] of Item, [arg]),
38+
js.return(var),
39+
])
40+
end
41+
else
42+
js.arrow_function([var]) do
43+
js.statements([
44+
location,
45+
js.call(["console.log"] of Item, [arg]),
46+
js.return(var),
47+
])
48+
end
4149
end
4250
end
4351
end

0 commit comments

Comments
 (0)