diff --git a/spec/compilers/dbg b/spec/compilers/dbg index 4895655e..f478cdb0 100644 --- a/spec/compilers/dbg +++ b/spec/compilers/dbg @@ -9,7 +9,7 @@ import { inspect as A } from "./runtime.js"; export const B = () => { return (() => { const a = `Hello World!`; - console.log(`./spec/compilers/dbg:3:4`); + console.log(`compilers/dbg:3:4`); console.log(A(a)); return a })() diff --git a/spec/compilers/dbg_as_function b/spec/compilers/dbg_as_function index 4afe6d70..80d4b288 100644 --- a/spec/compilers/dbg_as_function +++ b/spec/compilers/dbg_as_function @@ -8,7 +8,7 @@ import { inspect as A } from "./runtime.js"; export const B = () => { return (a) => { - console.log(`./spec/compilers/dbg_as_function:3:22`); + console.log(`compilers/dbg_as_function:3:22`); console.log(A(a)); return a }(`Hello World!`) diff --git a/spec/compilers/dbg_bang b/spec/compilers/dbg_bang index d18694f4..d5cd3aeb 100644 --- a/spec/compilers/dbg_bang +++ b/spec/compilers/dbg_bang @@ -7,7 +7,7 @@ component Main { export const A = () => { return (() => { const a = `Hello World!`; - console.log(`./spec/compilers/dbg_bang:3:4`); + console.log(`compilers/dbg_bang:3:4`); console.log(a); return a })() diff --git a/spec/compilers/directives/asset-absolute b/spec/compilers/directives/asset-absolute new file mode 100644 index 00000000..d6d5ff8c --- /dev/null +++ b/spec/compilers/directives/asset-absolute @@ -0,0 +1,15 @@ +component Main { + fun render : String { + @asset(/fixtures/icon.svg) + } +} +-------------------------------------------------------------------------------- +---=== /__mint__/index.js ===--- +export const A = () => { + return `/__mint__/icon_c97b81630bc53286dadc8996727d348e.svg` +}; + +---=== /__mint__/icon_c97b81630bc53286dadc8996727d348e.svg ===--- + + + diff --git a/spec/compilers/directives/highlight-file-absolute b/spec/compilers/directives/highlight-file-absolute new file mode 100644 index 00000000..66823171 --- /dev/null +++ b/spec/compilers/directives/highlight-file-absolute @@ -0,0 +1,67 @@ +component Main { + fun render : Html { + @highlight-file(/fixtures/Test.mint) + } +} +-------------------------------------------------------------------------------- +import { + createElement as A, + fragment as B +} from "./runtime.js"; + +export const C = () => { + return A(B, {}, [ + A("span", { + className: "line" + }, [ + A("span", { + className: "keyword" + }, [`component`]), + ` `, + A("span", { + className: "type" + }, [`Main`]), + ` { +` + ]), + A("span", { + className: "line" + }, [ + ` `, + A("span", { + className: "keyword" + }, [`fun`]), + ` render : `, + A("span", { + className: "type" + }, [`Html`]), + ` { +` + ]), + A("span", { + className: "line" + }, [ + ` <`, + A("span", { + className: "namespace" + }, [`div`]), + `> +` + ]), + A("span", { + className: "line" + }, [` } +`]), + A("span", { + className: "line" + }, [`} +`]), + A("span", { + className: "line" + }, [``]) + ]) +}; diff --git a/spec/compilers/directives/inline-absolute b/spec/compilers/directives/inline-absolute new file mode 100644 index 00000000..840fcce9 --- /dev/null +++ b/spec/compilers/directives/inline-absolute @@ -0,0 +1,10 @@ +component Main { + fun render : String { + @inline(/fixtures/data.txt) + } +} +-------------------------------------------------------------------------------- +export const A = () => { + return `Hello World! +` +}; diff --git a/spec/compilers/directives/svg-absolute b/spec/compilers/directives/svg-absolute new file mode 100644 index 00000000..169851ff --- /dev/null +++ b/spec/compilers/directives/svg-absolute @@ -0,0 +1,20 @@ +component Main { + fun render : Html { + @svg(/fixtures/icon.svg) + } +} +-------------------------------------------------------------------------------- +import { createElement as A } from "./runtime.js"; + +export const + a = A(`svg`, { + dangerouslySetInnerHTML: { + __html: `` + }, + viewBox: `0 0 24 24`, + height: `24`, + width: `24` + }), + B = () => { + return a + }; diff --git a/spec/compilers_spec.cr b/spec/compilers_spec.cr index bf7dbfd1..2ce71e0b 100644 --- a/spec/compilers_spec.cr +++ b/spec/compilers_spec.cr @@ -11,7 +11,7 @@ Dir sample, expected = File.read(file).split("-" * 80) # Parse the sample - ast = Mint::Parser.parse(sample, file) + ast = Mint::Parser.parse(sample, File.dirname(__FILE__) + file.lchop("./spec")) ast.class.should eq(Mint::Ast) artifacts = diff --git a/spec/mint.json b/spec/mint.json new file mode 100644 index 00000000..ffc3e4cd --- /dev/null +++ b/spec/mint.json @@ -0,0 +1,3 @@ +{ + "name": "ROOT FOR TESTS" +} diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index 1cc4144f..9360d3bd 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -1,6 +1,5 @@ require "spec" -ENV["SPEC"] = "TRUE" MINT_ENV["TEST"] = "TRUE" require "./spec_helpers" diff --git a/src/ast/directives/file_based.cr b/src/ast/directives/file_based.cr index 814d7c29..0a92f6f4 100644 --- a/src/ast/directives/file_based.cr +++ b/src/ast/directives/file_based.cr @@ -4,19 +4,29 @@ module Mint class FileBased < Node include Errorable - # The real path of the asset on the disk. - getter real_path : Path - # The given path of the asset, relative to the source file. getter path : String + # The relative path of the asset to the project root (closest `mint.json`). + getter relative_path : String do + File.relative_path_from_ancestor(path, "mint.json") + end + + # The real path of the asset on the disk. + getter real_path : Path do + if path.starts_with?("/") + if json = File.find_in_ancestors(file.path, "mint.json") + Path[json].sibling(path.lchop('/')).expand + end + end || Path[file.path].sibling(path).expand + end + def initialize( @from : Parser::Location, @to : Parser::Location, @file : Parser::File, @path : String ) - @real_path = Path[file.path].sibling(path).expand end # Returns the hashed filename of the target. For the build version it diff --git a/src/compilers/dbg.cr b/src/compilers/dbg.cr index 8f2ccd6e..a5da36e3 100644 --- a/src/compilers/dbg.cr +++ b/src/compilers/dbg.cr @@ -3,7 +3,7 @@ module Mint def compile(node : Ast::Dbg) : Compiled compile node do location = - js.string("#{node.file.path}:#{node.from.line}:#{node.from.column}") + js.string("#{node.file.relative_path}:#{node.from.line}:#{node.from.column}") var = [Variable.new] of Item diff --git a/src/ext/file.cr b/src/ext/file.cr index 517bc76a..ed85607a 100644 --- a/src/ext/file.cr +++ b/src/ext/file.cr @@ -4,6 +4,11 @@ class File File.write path, contents end + def self.relative_path_from_ancestor(path : String, name : String) : String + return path unless directory = File.find_in_ancestors(path, name) + Path[path].relative_to(File.dirname(directory)).to_s + end + def self.find_in_ancestors(base : String, name : String) : String? root = File.dirname(base) diff --git a/src/parser/file.cr b/src/parser/file.cr index 5e3cc50b..f7c0652f 100644 --- a/src/parser/file.cr +++ b/src/parser/file.cr @@ -3,6 +3,11 @@ module Mint class File getter contents, path + # The relative path of the file to the project root (closest `mint.json`). + getter relative_path : String do + ::File.relative_path_from_ancestor(path, "mint.json") + end + def initialize(@contents : String, @path : String) end diff --git a/src/type_checkers/directives/asset.cr b/src/type_checkers/directives/asset.cr index 4b607a56..68c3737a 100644 --- a/src/type_checkers/directives/asset.cr +++ b/src/type_checkers/directives/asset.cr @@ -2,14 +2,7 @@ module Mint class TypeChecker def check(node : Ast::Directives::Asset) : Checkable error! :asset_directive_expected_file do - block "The path specified for an asset directive does not exist: " - - if ENV["SPEC"]? - snippet node.path.to_s - else - snippet node.real_path.to_s - end - + snippet "The path specified for an asset directive does not exist:", node.relative_path snippet "The asset directive in question is here:", node end unless node.exists? diff --git a/src/type_checkers/directives/highlight_file.cr b/src/type_checkers/directives/highlight_file.cr index c89bd62f..2a70e1bd 100644 --- a/src/type_checkers/directives/highlight_file.cr +++ b/src/type_checkers/directives/highlight_file.cr @@ -2,14 +2,7 @@ module Mint class TypeChecker def check(node : Ast::Directives::HighlightFile) : Checkable error! :highlight_file_directive_expected_file do - block "The path specified for an highlight file directive does not exist: " - - if ENV["SPEC"]? - snippet node.path.to_s - else - snippet node.real_path.to_s - end - + snippet "The path specified for an highlight file directive does not exist:", node.relative_path snippet "The highlight file directive in question is here:", node end unless node.exists? diff --git a/src/type_checkers/directives/inline.cr b/src/type_checkers/directives/inline.cr index 6ba728d1..583b35d8 100644 --- a/src/type_checkers/directives/inline.cr +++ b/src/type_checkers/directives/inline.cr @@ -2,14 +2,7 @@ module Mint class TypeChecker def check(node : Ast::Directives::Inline) : Checkable error! :inline_directive_expected_file do - block "The path specified for an inline directive does not exist:" - - if ENV["SPEC"]? - snippet node.path.to_s - else - snippet node.real_path.to_s - end - + snippet "The path specified for an inline directive does not exist:", node.relative_path snippet "The inline directive in question is here:", node end unless node.exists? diff --git a/src/type_checkers/directives/svg.cr b/src/type_checkers/directives/svg.cr index f1c1c060..d36e6cfa 100644 --- a/src/type_checkers/directives/svg.cr +++ b/src/type_checkers/directives/svg.cr @@ -2,14 +2,7 @@ module Mint class TypeChecker def check(node : Ast::Directives::Svg) : Checkable error! :svg_directive_expected_file do - path = - if ENV["SPEC"]? - node.path.to_s - else - node.real_path.to_s - end - - snippet "The specified file for an svg directive does not exist:", path + snippet "The specified file for an svg directive does not exist:", node.relative_path snippet "The svg directive in question is here:", node end unless node.exists?