Skip to content

Commit

Permalink
Refactor JSImport and add tests for homonymous aliases (#2488)
Browse files Browse the repository at this point in the history
  • Loading branch information
sodic authored Feb 7, 2025
1 parent f9c2bc7 commit d49cef0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 41 deletions.
58 changes: 20 additions & 38 deletions waspc/src/Wasp/JsImport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ module Wasp.JsImport
applyJsImportAlias,
getImportIdentifier,
getJsImportStmtAndIdentifier,
getJsImportStmtAndIdentifierRaw,
)
where

Expand Down Expand Up @@ -71,42 +70,25 @@ applyJsImportAlias importAlias jsImport = jsImport {_importAlias = importAlias}

getJsImportStmtAndIdentifier :: JsImport -> (JsImportStatement, JsImportIdentifier)
getJsImportStmtAndIdentifier (JsImport importPath importName maybeImportAlias) =
getJsImportStmtAndIdentifierRaw filePath importName maybeImportAlias
where
filePath = case importPath of
RelativeImportPath relPath -> normalizePath $ SP.fromRelFileP relPath
ModuleImportPath pathString -> SP.fromRelFileP pathString
normalizePath path = if ".." `isPrefixOf` path then path else "./" ++ path

-- todo(filip): attempt to simplify how we generate imports. I wanted to generate a
-- module import (e.g., '@ext-src/something') and couldn't do it. This is one of
-- the funtions I implemented while I was trying to pull it off.
getJsImportStmtAndIdentifierRaw ::
FilePath ->
JsImportName ->
Maybe JsImportAlias ->
(JsImportStatement, JsImportIdentifier)
getJsImportStmtAndIdentifierRaw importPath importName maybeImportAlias =
(importStatement, importIdentifier)
where
(importIdentifier, importClause) = jsImportIdentifierAndClause
importStatement = "import " ++ importClause ++ " from '" ++ importPath ++ "'"

-- First part of import statement based on type of import and alias
-- e.g. for import { Name as Alias } from "file.js" it returns ("Alias", "{ Name as Alias }")
-- e.g. for import Name from "file.js" it returns ("Name", "Name")
jsImportIdentifierAndClause :: (JsImportIdentifier, JsImportClause)
jsImportIdentifierAndClause = case importName of
JsImportModule defaultImport -> getForDefault defaultImport maybeImportAlias
JsImportField namedImport -> getForNamed namedImport maybeImportAlias
where
getForDefault :: JsImportIdentifier -> Maybe JsImportAlias -> (JsImportIdentifier, JsImportClause)
getForDefault identifier Nothing = (identifier, identifier)
getForDefault _ (Just importAlias) = (importAlias, importAlias)

getForNamed :: JsImportIdentifier -> Maybe JsImportAlias -> (JsImportIdentifier, JsImportClause)
getForNamed identifier Nothing = (identifier, "{ " ++ identifier ++ " }")
getForNamed identifier (Just importAlias) = (importAlias, "{ " ++ resolvedIdentifier ++ " }")
where
resolvedIdentifier =
if identifier == importAlias then identifier else identifier ++ " as " ++ importAlias
importStatement = "import " ++ importClause ++ " from '" ++ pathString ++ "'"
(importIdentifier, importClause) = getJsImportIdentifierAndClause importName maybeImportAlias
pathString = case importPath of
RelativeImportPath relPath -> normalizePath $ SP.fromRelFileP relPath
ModuleImportPath modulePath -> SP.fromRelFileP modulePath
normalizePath path
| ".." `isPrefixOf` path = path
| otherwise = "./" ++ path

-- First part of import statement based on type of import and alias
-- e.g. for import { Name as Alias } from "file.js" it returns ("Alias", "{ Name as Alias }")
-- e.g. for import Name from "file.js" it returns ("Name", "Name")
getJsImportIdentifierAndClause :: JsImportName -> Maybe JsImportAlias -> (JsImportIdentifier, JsImportClause)
getJsImportIdentifierAndClause importName maybeImportAlias = case (importName, maybeImportAlias) of
(JsImportModule moduleName, Nothing) -> (moduleName, moduleName)
(JsImportModule _, Just alias) -> (alias, alias)
(JsImportField fieldName, Nothing) -> (fieldName, "{ " ++ fieldName ++ " }")
(JsImportField fieldName, Just alias)
| fieldName == alias -> (fieldName, "{ " ++ fieldName ++ " }")
| otherwise -> (alias, "{ " ++ fieldName ++ " as " ++ alias ++ " }")
21 changes: 18 additions & 3 deletions waspc/test/JsImportTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Wasp.JsImport
spec_JsImportTest :: Spec
spec_JsImportTest = do
describe "makeJsImport" $ do
it "makes JsImport with default import from a path" $ do
it "makes JsImport with a module import from a path" $ do
makeJsImport testRelativeImportPath (JsImportModule "test")
`shouldBe` JsImport
{ _path = testRelativeImportPath,
Expand All @@ -22,17 +22,24 @@ spec_JsImportTest = do
`shouldBe` jsImport {_importAlias = Just "alias"}
describe "getJsImportStmtAndIdentifier" $ do
describe "generates import statement and identifier from" $ do
it "default import" $ do
it "module import" $ do
getJsImportStmtAndIdentifier
(makeJsImport testRelativeImportPath (JsImportModule "test"))
`shouldBe` ("import test from '" ++ generatedImportPathForRelativeImportPath ++ "'", "test")
it "default import with alias" $ do
it "module import with alias" $ do
getJsImportStmtAndIdentifier
( applyJsImportAlias
(Just "alias")
(makeJsImport testRelativeImportPath (JsImportModule "test"))
)
`shouldBe` ("import alias from '" ++ generatedImportPathForRelativeImportPath ++ "'", "alias")
it "module import with homonymous alias" $ do
getJsImportStmtAndIdentifier
( applyJsImportAlias
(Just "test")
(makeJsImport testRelativeImportPath (JsImportModule "test"))
)
`shouldBe` ("import test from '" ++ generatedImportPathForRelativeImportPath ++ "'", "test")
it "named import" $ do
getJsImportStmtAndIdentifier
(makeJsImport testRelativeImportPath (JsImportField "test"))
Expand All @@ -44,6 +51,14 @@ spec_JsImportTest = do
(makeJsImport testRelativeImportPath (JsImportField "test"))
)
`shouldBe` ("import { test as alias } from '" ++ generatedImportPathForRelativeImportPath ++ "'", "alias")

it "named import with homonymous alias" $ do
getJsImportStmtAndIdentifier
( applyJsImportAlias
(Just "test")
(makeJsImport testRelativeImportPath (JsImportField "test"))
)
`shouldBe` ("import { test } from '" ++ generatedImportPathForRelativeImportPath ++ "'", "test")
it "import from module" $ do
getJsImportStmtAndIdentifier
(makeJsImport testModuleImportPath (JsImportModule "test"))
Expand Down

0 comments on commit d49cef0

Please sign in to comment.