|
| 1 | +# ----------------------------------------------------------------------- |
| 2 | +# This file is part of MoonScript |
| 3 | +# |
| 4 | +# MoonSript is free software: you can redistribute it and/or modify |
| 5 | +# it under the terms of the GNU General Public License as published by |
| 6 | +# the Free Software Foundation, either version 3 of the License, or |
| 7 | +# (at your option) any later version. |
| 8 | +# |
| 9 | +# MoonSript is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU General Public License |
| 15 | +# along with MoonSript. If not, see <https://www.gnu.org/licenses/>. |
| 16 | +# |
| 17 | +# Copyright (C) 2025 Krisna Pranav, MoonScript Developers |
| 18 | +# ----------------------------------------------------------------------- |
| 19 | + |
| 20 | +module MoonScript |
| 21 | + class TypeChecker |
| 22 | + def check(node : Ast::Directives::Svg) : Checkable |
| 23 | + error! :svg_directive_expected_file do |
| 24 | + snippet "The specified file for an svg directive does not exist:", node.relative_path |
| 25 | + snippet "The svg dir: ", node |
| 26 | + end unless node.exists? |
| 27 | + |
| 28 | + contents = |
| 29 | + node.file_contents |
| 30 | + |
| 31 | + document = |
| 32 | + XML.parse(contents) |
| 33 | + |
| 34 | + errors = |
| 35 | + document.errors.try(&.map(&.to_s)) || %w[] |
| 36 | + |
| 37 | + error! :svg_directive_expected_svg do |
| 38 | + snippet( |
| 39 | + "The specified file for an svg directive is not an SVG file " \ |
| 40 | + "These are the errors founded: ", |
| 41 | + errors.join("\n")) |
| 42 | + |
| 43 | + snippet( |
| 44 | + "First len of the line: ", |
| 45 | + contents.lines[0..4].join("\n")) |
| 46 | + |
| 47 | + snippet "The svg directive: ", node |
| 48 | + end unless errors.empty? |
| 49 | + |
| 50 | + svg = |
| 51 | + document.first_element_child |
| 52 | + |
| 53 | + error! :svg_directive_expected_svg_tag do |
| 54 | + snippet( |
| 55 | + "The specified file for an svg directive does not contain an " \ |
| 56 | + "<svg> tag. few lines of the file:", |
| 57 | + contents.lines[0..4].join("\n")) |
| 58 | + |
| 59 | + snippet "The svg directive in question is here:", node |
| 60 | + end if !svg || svg.name != "svg" |
| 61 | + |
| 62 | + error! :svg_directive_expected_dimensions do |
| 63 | + snippet "needed certain attributes for an svg for it to render " \ |
| 64 | + "correctly. The specified file for an svg directive does " \ |
| 65 | + "not have these required attributes:", "width, height, viewBox" |
| 66 | + |
| 67 | + snippet( |
| 68 | + "first few lines of the file:", |
| 69 | + contents.lines[0..4].join("\n")) |
| 70 | + |
| 71 | + snippet "The svg directive in question is here:", node |
| 72 | + end unless svg["width"]? && svg["height"]? && svg["viewBox"]? |
| 73 | + |
| 74 | + HTML |
| 75 | + end |
| 76 | + end |
| 77 | +end |
0 commit comments