Skip to content

Commit

Permalink
Update tsconfig validation
Browse files Browse the repository at this point in the history
  • Loading branch information
infomiho committed Sep 19, 2024
1 parent c515bd4 commit 2e16552
Showing 1 changed file with 18 additions and 46 deletions.
64 changes: 18 additions & 46 deletions waspc/src/Wasp/ExternalConfig/TsConfig.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE TypeSynonymInstances #-}

module Wasp.ExternalConfig.TsConfig
( TsConfig (..),
Expand All @@ -13,7 +11,6 @@ import Control.Monad.Except
import Data.Aeson (FromJSON, parseJSON, withObject, (.:?))
import qualified Data.ByteString.Lazy.UTF8 as BS
import Data.Either.Extra (maybeToEither)
import qualified Data.Map as M
import GHC.Generics (Generic)
import StrongPath (Abs, Dir, File', Path', toFilePath)
import Wasp.Project.Common
Expand Down Expand Up @@ -93,16 +90,16 @@ validateTsConfig tsConfig =
where
tsConfigErrors =
concat
[ validateTsConfigField "module" (_module compilerOptionsValues) "esnext",
validateTsConfigField "target" (target compilerOptionsValues) "esnext",
validateTsConfigField "moduleResolution" (moduleResolution compilerOptionsValues) "bundler",
validateTsConfigField "jsx" (jsx compilerOptionsValues) "preserve",
validateTsConfigField "strict" (strict compilerOptionsValues) True,
validateTsConfigField "esModuleInterop" (esModuleInterop compilerOptionsValues) True,
validateTsConfigField "lib" (lib compilerOptionsValues) ["dom", "dom.iterable", "esnext"],
validateTsConfigField "allowJs" (allowJs compilerOptionsValues) True,
validateTsConfigField "typeRoots" (typeRoots compilerOptionsValues) ["node_modules/@testing-library", "node_modules/@types"],
validateTsConfigField "outDir" (outDir compilerOptionsValues) ".wasp/phantom"
[ validateRequiredField "module" (_module compilerOptionsValues) "esnext",
validateRequiredField "target" (target compilerOptionsValues) "esnext",
validateRequiredField "moduleResolution" (moduleResolution compilerOptionsValues) "bundler",
validateRequiredField "jsx" (jsx compilerOptionsValues) "preserve",
validateRequiredField "strict" (strict compilerOptionsValues) True,
validateRequiredField "esModuleInterop" (esModuleInterop compilerOptionsValues) True,
validateRequiredField "lib" (lib compilerOptionsValues) ["dom", "dom.iterable", "esnext"],
validateRequiredField "allowJs" (allowJs compilerOptionsValues) True,
validateRequiredField "typeRoots" (typeRoots compilerOptionsValues) ["node_modules/@testing-library", "node_modules/@types"],
validateRequiredField "outDir" (outDir compilerOptionsValues) ".wasp/phantom"
]
compilerOptionsValues = compilerOptions tsConfig

Expand All @@ -119,40 +116,15 @@ instance ShowJs Bool where
showJs True = "true"
showJs False = "false"

-- TODO: maybe use a more structured way of defining expected values, so that we can easily add new fields.
-- expectedValues :: M.Map FieldName TsConfigFieldType
-- expectedValues =
-- M.fromList
-- [ ("module", Str "esnext"),
-- ("target", Str "esnext"),
-- ("moduleResolution", Str "bundler"),
-- ("jsx", Str "preserve"),
-- ("strict", Bool True),
-- ("esModuleInterop", Bool True),
-- ("lib", StrList ["dom", "dom.iterable", "esnext"]),
-- ("allowJs", Bool True),
-- ("typeRoots", StrList ["node_modules/@testing-library", "node_modules/@types"]),
-- ("outDir", Str ".wasp/phantom")
-- ]

-- data TsConfigFieldType = Str String | Bool Bool | StrList [String]

-- instance Show TsConfigFieldType where
-- show :: TsConfigFieldType -> String
-- show (Str s) = s
-- show (Bool True) = show ("true" :: String)
-- show (Bool False) = show ("false" :: String)
-- show (StrList l) = show l

type FieldName = String

validateTsConfigField :: (Eq value, ShowJs value) => FieldName -> Maybe value -> value -> [CompileError]
validateTsConfigField fieldName Nothing expectedValue = [missingFieldErrorMessage]
where
missingFieldErrorMessage = unwords ["The", show fieldName, "field is missing in tsconfig.json. Expected value:", showJs expectedValue ++ "."]
validateTsConfigField fieldName (Just userProvidedValue) expectedValue =
if userProvidedValue /= expectedValue
then [invalidValueErrorMessage]
else []
validateRequiredField :: (Eq value, ShowJs value) => FieldName -> Maybe value -> value -> [CompileError]
validateRequiredField fieldName maybeUserProvidedValue expectedValue = case maybeUserProvidedValue of
Nothing -> [missingFieldErrorMessage]
Just userProvidedValue ->
if userProvidedValue /= expectedValue
then [invalidValueErrorMessage]
else []
where
invalidValueErrorMessage = unwords ["Invalid value for the", show fieldName, "field in tsconfig.json file, expected:", showJs expectedValue ++ "."]
missingFieldErrorMessage = unwords ["The", show fieldName, "field is missing in tsconfig.json. Expected value:", showJs expectedValue ++ "."]

0 comments on commit 2e16552

Please sign in to comment.