Skip to content

Commit cebed8f

Browse files
committed
Add ffi check for ide rebuild without codegen
1 parent 6d93fff commit cebed8f

File tree

2 files changed

+55
-20
lines changed

2 files changed

+55
-20
lines changed

src/Language/PureScript/Ide/Rebuild.hs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ import qualified Data.Set as S
1616
import qualified Data.Time as Time
1717
import qualified Data.Text as Text
1818
import qualified Language.PureScript as P
19+
import Language.PureScript.Make (ffiCodegen')
1920
import Language.PureScript.Make.Cache (CacheInfo(..), normaliseForCache)
2021
import qualified Language.PureScript.CST as CST
22+
2123
import Language.PureScript.Ide.Error
2224
import Language.PureScript.Ide.Logging
2325
import Language.PureScript.Ide.State
@@ -69,10 +71,15 @@ rebuildFile file actualFile codegenTargets runOpenBuild = do
6971
-- For rebuilding, we want to 'RebuildAlways', but for inferring foreign
7072
-- modules using their file paths, we need to specify the path in the 'Map'.
7173
let filePathMap = M.singleton moduleName (Left P.RebuildAlways)
72-
let pureRebuild = S.null codegenTargets
73-
foreigns <- P.inferForeignModules (M.singleton moduleName (Right file))
74+
let pureRebuild = fp == ""
75+
let modulePath = if pureRebuild then fp' else file
76+
foreigns <- P.inferForeignModules (M.singleton moduleName (Right modulePath))
7477
let makeEnv = P.buildMakeActions outputDirectory filePathMap foreigns False
7578
& (if pureRebuild then shushCodegen else identity)
79+
& ( if pureRebuild
80+
then enableForeignCheck foreigns codegenTargets
81+
else identity
82+
)
7683
& shushProgress
7784
-- Rebuild the single module using the cached externs
7885
(result, warnings) <- logPerf (labelTimespec "Rebuilding Module") $
@@ -184,6 +191,16 @@ shushCodegen ma =
184191
, P.ffiCodegen = \_ -> pure ()
185192
}
186193

194+
-- | Enables foreign module check without actual codegen.
195+
enableForeignCheck
196+
:: M.Map P.ModuleName FilePath
197+
-> S.Set P.CodegenTarget
198+
-> P.MakeActions P.Make
199+
-> P.MakeActions P.Make
200+
enableForeignCheck foreigns codegenTargets ma =
201+
ma { P.ffiCodegen = ffiCodegen' foreigns codegenTargets Nothing
202+
}
203+
187204
-- | Returns a topologically sorted list of dependent ExternsFiles for the given
188205
-- module. Throws an error if there is a cyclic dependency within the
189206
-- ExternsFiles

src/Language/PureScript/Make/Actions.hs

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module Language.PureScript.Make.Actions
88
, cacheDbFile
99
, readCacheDb'
1010
, writeCacheDb'
11+
, ffiCodegen'
1112
) where
1213

1314
import Prelude
@@ -280,23 +281,7 @@ buildMakeActions outputDir filePathMap foreigns usePrefix =
280281
ffiCodegen :: CF.Module CF.Ann -> Make ()
281282
ffiCodegen m = do
282283
codegenTargets <- asks optionsCodegenTargets
283-
when (S.member JS codegenTargets) $ do
284-
let mn = CF.moduleName m
285-
case mn `M.lookup` foreigns of
286-
Just path
287-
| not $ requiresForeign m ->
288-
tell $ errorMessage' (CF.moduleSourceSpan m) $ UnnecessaryFFIModule mn path
289-
| otherwise -> do
290-
checkResult <- checkForeignDecls m path
291-
case checkResult of
292-
Left _ -> copyFile path (outputFilename mn "foreign.js")
293-
Right (ESModule, _) -> copyFile path (outputFilename mn "foreign.js")
294-
Right (CJSModule, _) -> do
295-
throwError $ errorMessage' (CF.moduleSourceSpan m) $ DeprecatedFFICommonJSModule mn path
296-
297-
Nothing | requiresForeign m -> throwError . errorMessage' (CF.moduleSourceSpan m) $ MissingFFIModule mn
298-
| otherwise -> return ()
299-
284+
ffiCodegen' foreigns codegenTargets (Just outputFilename) m
300285

301286
genSourceMap :: String -> String -> Int -> [SMap] -> Make ()
302287
genSourceMap dir mapFile extraLines mappings = do
@@ -358,7 +343,7 @@ checkForeignDecls m path = do
358343
modSS = CF.moduleSourceSpan m
359344

360345
checkFFI :: JS.JSAST -> Make (ForeignModuleType, S.Set Ident)
361-
checkFFI js = do
346+
checkFFI js = do
362347
(foreignModuleType, foreignIdentsStrs) <-
363348
case (,) <$> getForeignModuleExports js <*> getForeignModuleImports js of
364349
Left reason -> throwError $ errorParsingModule reason
@@ -438,3 +423,36 @@ checkForeignDecls m path = do
438423
. CST.runTokenParser CST.parseIdent
439424
. CST.lex
440425
$ T.pack str
426+
427+
-- | FFI check and codegen action.
428+
-- If path maker is supplied copies foreign module to the output.
429+
ffiCodegen'
430+
:: M.Map ModuleName FilePath
431+
-> S.Set CodegenTarget
432+
-> Maybe (ModuleName -> String -> FilePath)
433+
-> CF.Module CF.Ann
434+
-> Make ()
435+
ffiCodegen' foreigns codegenTargets makeOutputPath m = do
436+
when (S.member JS codegenTargets) $ do
437+
let mn = CF.moduleName m
438+
case mn `M.lookup` foreigns of
439+
Just path
440+
| not $ requiresForeign m ->
441+
tell $ errorMessage' (CF.moduleSourceSpan m) $ UnnecessaryFFIModule mn path
442+
| otherwise -> do
443+
checkResult <- checkForeignDecls m path
444+
case checkResult of
445+
Left _ -> copyForeign path mn
446+
Right (ESModule, _) -> copyForeign path mn
447+
Right (CJSModule, _) -> do
448+
throwError $ errorMessage' (CF.moduleSourceSpan m) $ DeprecatedFFICommonJSModule mn path
449+
Nothing | requiresForeign m -> throwError . errorMessage' (CF.moduleSourceSpan m) $ MissingFFIModule mn
450+
| otherwise -> return ()
451+
where
452+
requiresForeign = not . null . CF.moduleForeign
453+
454+
copyForeign path mn =
455+
case makeOutputPath of
456+
Nothing -> pure ()
457+
Just outputFilename ->
458+
copyFile path (outputFilename mn "foreign.js")

0 commit comments

Comments
 (0)