From 0dbe448959d6aa03f0ea99a7e180e2cafaedf651 Mon Sep 17 00:00:00 2001 From: stuebinm Date: Sat, 4 Dec 2021 15:12:30 +0100 Subject: better lints for invalid links --- lib/Properties.hs | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) (limited to 'lib/Properties.hs') diff --git a/lib/Properties.hs b/lib/Properties.hs index 07b4397..1d01216 100644 --- a/lib/Properties.hs +++ b/lib/Properties.hs @@ -11,7 +11,7 @@ module Properties (checkMap, checkTileset, checkLayer) where import Control.Monad (forM_, unless, when) -import Data.Text (Text, isPrefixOf) +import Data.Text (Text, isPrefixOf, intercalate) import qualified Data.Vector as V import Tiled (Layer (..), Object (..), Property (..), PropertyValue (..), Tile (..), Tiledmap (..), @@ -147,7 +147,7 @@ checkTileset = do where checkTileProperty :: Property -> LintWriter Tileset checkTileProperty p@(Property name _) = case name of "collides" -> isBool p - _ -> warn $ "uknown tile property " <> prettyprint name + _ -> warn $ "unknown tile property " <> prettyprint name <> " in tile with global id " <> showText (tileId tile) @@ -283,9 +283,9 @@ checkTileLayerProperty p@(Property name _value) = case name of (setProperty "openWebsiteTriggerMessage") "playAudio" -> do uselessEmptyLayer - unwrapLink p $ \link -> if "https://" `isPrefixOf` link - then dependsOn $ Link link - else unwrapPath link (dependsOn . Local) + unwrapURI (Proxy @"audio") p + (dependsOn . Link) + (dependsOn . Local) "audioLoop" -> do isBool p requireProperty "playAudio" @@ -339,6 +339,8 @@ checkTileLayerProperty p@(Property name _value) = case name of unwrapBool p $ \case True -> pure () False -> warn "property \"collides\" set to 'false' is useless." + "getBadge" -> complain "\"getBadge\" must be set on an \"objectgroup\" \ + \ layer; it does not work on tile layers." "name" -> isUnsupported _ -> warn $ "unknown property type " <> prettyprint name @@ -439,7 +441,7 @@ propertyRequiredBy req by = suggestProperty :: Property -> LintWriter Layer suggestProperty (Property name value) = unlessHasProperty name - $ suggest $ "set property " <> prettyprint name <> " to " <> prettyprint value<>"." + $ suggest $ "set property " <> prettyprint name <> " to \"" <> prettyprint value<>"\"." @@ -472,15 +474,6 @@ unwrapString (Property name value) f = case value of _ -> complain $ "type error: property " <> prettyprint name <> " should be of type string." --- | same as unwrapString, but also forbids http:// as prefix -unwrapLink :: Property -> (Text -> LintWriter a) -> LintWriter a -unwrapLink (Property name value) f = case value of - StrProp str -> if "http://" `isPrefixOf` str - then complain "cannot access content via http; either use https or include\ - \it locally in your repository instead." - else f str - _ -> complain $ "type error: property " <> prettyprint name <> " should be\ - \of type string and contain a valid uri." -- | asserts that this property is a boolean, and unwraps it unwrapBool :: Property -> (Bool -> LintWriter a) -> LintWriter a @@ -512,9 +505,10 @@ unwrapBadgeToken str f = case parseToken str of Just a -> f a Nothing -> complain "invalid badge token." + unwrapURI :: (KnownSymbol s, HasProperties a) => Proxy s -> Property -> (Text -> LintWriter a) -> (RelPath -> LintWriter a) -> LintWriter a -unwrapURI sym p@(Property name _) f g = unwrapLink p $ \link -> do +unwrapURI sym p@(Property name _) f g = unwrapString p $ \link -> do subst <- lintConfig configUriSchemas case applySubst sym subst link of Right uri -> do @@ -526,8 +520,11 @@ unwrapURI sym p@(Property name _) f g = unwrapLink p $ \link -> do InvalidLink -> link <> " is invalid." SchemaDoesNotExist schema -> "the URI schema " <> schema <> ":// does not exist." - WrongScope schema -> - "the URI schema " <> schema <> ":// cannot be used on \""<>name<>"\"." + WrongScope schema allowed -> + "the URI schema " <> schema <> ":// cannot be used in property \ + \\"" <> name <> "\"; allowed " + <> (if length allowed == 1 then "is " else "are ") + <> intercalate ", " (fmap (<> "://") allowed) <> "." -- | just asserts that this is a string isString :: Property -> LintWriter a -- cgit v1.2.3