Skip to content

Commit

Permalink
Add missing 0.16.0 tsconfig properties to validation (#2471)
Browse files Browse the repository at this point in the history
* Refactor TS config validation

* Validate skipLibCheck and include in tsconfig.json
  • Loading branch information
sodic authored Jan 27, 2025
1 parent 1e7eaea commit 1b24645
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 45 deletions.
4 changes: 3 additions & 1 deletion waspc/src/Wasp/ExternalConfig/TsConfig.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ import qualified Data.Aeson as Aeson
import GHC.Generics (Generic)

data TsConfig = TsConfig
{ compilerOptions :: !CompilerOptions
{ compilerOptions :: !CompilerOptions,
include :: !(Maybe [String])
}
deriving (Show, Generic, FromJSON)

data CompilerOptions = CompilerOptions
{ _module :: !(Maybe String),
target :: !(Maybe String),
composite :: !(Maybe Bool),
skipLibCheck :: !(Maybe Bool),
moduleResolution :: !(Maybe String),
jsx :: !(Maybe String),
strict :: !(Maybe Bool),
Expand Down
99 changes: 55 additions & 44 deletions waspc/src/Wasp/Generator/ExternalConfig/TsConfig.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ import Data.List (intercalate)
import qualified Wasp.ExternalConfig.TsConfig as T
import Wasp.Generator.ExternalConfig.Common (ErrorMsg)

class IsJavascriptValue a where
class JsonValue a where
showAsJsValue :: a -> String

instance IsJavascriptValue String where
instance JsonValue String where
showAsJsValue = show

instance IsJavascriptValue [String] where
instance JsonValue [String] where
showAsJsValue = show

instance IsJavascriptValue Bool where
instance JsonValue Bool where
showAsJsValue True = "true"
showAsJsValue False = "false"

Expand All @@ -34,49 +34,60 @@ instance Show FullyQualifiedFieldName where

validateSrcTsConfig :: T.TsConfig -> [ErrorMsg]
validateSrcTsConfig tsConfig =
validateRequiredField (FieldName ["include"]) (T.include tsConfig) ["src"]
++ validateCompilerOptions (T.compilerOptions tsConfig)

validateCompilerOptions :: T.CompilerOptions -> [ErrorMsg]
validateCompilerOptions compilerOptions =
concat
[ validateRequiredFieldInCompilerOptions "module" "esnext" T._module,
validateRequiredFieldInCompilerOptions "target" "esnext" T.target,
validateRequiredFieldInCompilerOptions "moduleResolution" "bundler" T.moduleResolution,
validateRequiredFieldInCompilerOptions "jsx" "preserve" T.jsx,
validateRequiredFieldInCompilerOptions "strict" True T.strict,
validateRequiredFieldInCompilerOptions "esModuleInterop" True T.esModuleInterop,
validateRequiredFieldInCompilerOptions "lib" ["dom", "dom.iterable", "esnext"] T.lib,
validateRequiredFieldInCompilerOptions "allowJs" True T.allowJs,
validateRequiredFieldInCompilerOptions "typeRoots" ["node_modules/@testing-library", "node_modules/@types"] T.typeRoots,
validateRequiredFieldInCompilerOptions "outDir" ".wasp/out/user" T.outDir,
validateRequiredFieldInCompilerOptions "composite" True T.composite,
anyValueIsFine (FieldName ["compilerOptions", "skipLibCheck"])
[ validateRequiredFieldInCompilerOptions "module" T._module "esnext",
validateRequiredFieldInCompilerOptions "target" T.target "esnext",
validateRequiredFieldInCompilerOptions "moduleResolution" T.moduleResolution "bundler",
validateRequiredFieldInCompilerOptions "jsx" T.jsx "preserve",
validateRequiredFieldInCompilerOptions "strict" T.strict True,
validateRequiredFieldInCompilerOptions "esModuleInterop" T.esModuleInterop True,
validateRequiredFieldInCompilerOptions "lib" T.lib ["dom", "dom.iterable", "esnext"],
validateRequiredFieldInCompilerOptions "allowJs" T.allowJs True,
validateRequiredFieldInCompilerOptions "typeRoots" T.typeRoots ["node_modules/@testing-library", "node_modules/@types"],
validateRequiredFieldInCompilerOptions "outDir" T.outDir ".wasp/out/user",
validateRequiredFieldInCompilerOptions "composite" T.composite True,
validateRequiredFieldInCompilerOptions "skipLibCheck" T.skipLibCheck True
]
where
validateRequiredFieldInCompilerOptions fieldName expectedValue getFieldValue = case fieldValue of
Just actualValue -> validateFieldValue fullyQualifiedFieldName expectedValue actualValue
Nothing -> [missingFieldErrorMessage]
where
fieldValue = getFieldValue $ T.compilerOptions tsConfig
fullyQualifiedFieldName = FieldName ["compilerOptions", fieldName]
validateRequiredFieldInCompilerOptions relativeFieldName getFieldValue =
validateRequiredField (FieldName ["compilerOptions", relativeFieldName]) (getFieldValue compilerOptions)

validateRequiredField :: (Eq a, JsonValue a) => FullyQualifiedFieldName -> Maybe a -> a -> [String]
validateRequiredField fullyQualifiedFieldName fieldValue expectedValue =
validateFieldValue fullyQualifiedFieldName (Just expectedValue) fieldValue

missingFieldErrorMessage =
unwords
[ "The",
"\"" ++ show fullyQualifiedFieldName ++ "\"",
"field is missing in tsconfig.json, expected value:",
showAsJsValue expectedValue ++ "."
]
validateFieldValue :: (Eq a, JsonValue a) => FullyQualifiedFieldName -> Maybe a -> Maybe a -> [String]
validateFieldValue fullyQualifiedFieldName expectedValue actualValue =
case (expectedValue, actualValue) of
(Nothing, Nothing) -> []
(Just expected, Just actual) -> [makeInvalidValueErrorMessage expected | actual /= expected]
(Just expected, Nothing) -> [makeMissingFieldErrorMessage expected]
(Nothing, Just _) -> [fieldMustBeUnsetErrorMessage]
where
makeInvalidValueErrorMessage expected =
unwords
[ "Invalid value for the",
"\"" ++ show fullyQualifiedFieldName ++ "\"",
"field in tsconfig.json, you must set it to:",
showAsJsValue expected ++ "."
]

anyValueIsFine :: FullyQualifiedFieldName -> [String]
anyValueIsFine = const []
fieldMustBeUnsetErrorMessage =
unwords
[ "The",
"\"" ++ show fullyQualifiedFieldName ++ "\"",
"field in tsconfig.json must be unset."
]

validateFieldValue :: (Eq value, IsJavascriptValue value) => FullyQualifiedFieldName -> value -> value -> [String]
validateFieldValue fullyQualifiedFieldName expectedValue actualValue =
if actualValue == expectedValue
then []
else [invalidValueErrorMessage]
where
invalidValueErrorMessage =
unwords
[ "Invalid value for the",
"\"" ++ show fullyQualifiedFieldName ++ "\"",
"field in tsconfig.json, expected value:",
showAsJsValue expectedValue ++ "."
]
makeMissingFieldErrorMessage expected =
unwords
[ "The",
"\"" ++ show fullyQualifiedFieldName ++ "\"",
"field is missing in tsconfig.json, you must set it to:",
showAsJsValue expected ++ "."
]

0 comments on commit 1b24645

Please sign in to comment.