CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/hackage-purescript

A strongly typed functional programming language compiler that compiles to JavaScript with advanced type system features and IDE integration

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

codegen.mddocs/

JavaScript Code Generation

Comprehensive JavaScript code generation from CoreFn with optimization passes, source maps, and runtime system integration for efficient output.

Capabilities

Core Code Generation

Main module-to-JavaScript transformation with optimization and source map support.

-- | Convert CoreFn module to JavaScript
moduleToJs :: CF.Module CF.Ann -> Doc Text

-- | Generate JavaScript for module with options
moduleToJsWithOptions :: CodegenOptions -> CF.Module CF.Ann -> Doc Text

-- | Code generation options
data CodegenOptions = CodegenOptions
  { optSourceMaps :: Bool
  , optComments :: Bool
  , optTCO :: Bool           -- Tail call optimization
  , optMagicDo :: Bool       -- Magic do optimization
  , optInline :: Bool        -- Function inlining
  , optUnused :: Bool        -- Dead code elimination
  }

-- | Default code generation options
defaultCodegenOptions :: CodegenOptions

JavaScript AST

Intermediate JavaScript representation for code generation.

-- | JavaScript expressions
data JS
  = JSNumericLiteral (Either Integer Double)
  | JSStringLiteral PSString
  | JSBooleanLiteral Bool
  | JSArrayLiteral [JS]
  | JSObjectLiteral [(PSString, JS)]
  | JSBlock [JS]
  | JSVar Ident
  | JSVariableIntroduction Ident (Maybe JS)
  | JSAssignment (Either Ident SourcePos) JS
  | JSWhile JS [JS]
  | JSFor Ident JS JS [JS]
  | JSForIn Ident JS [JS]
  | JSIfElse JS [JS] (Maybe [JS])
  | JSReturn JS
  | JSThrow JS
  | JSBreak (Maybe Ident)
  | JSContinue (Maybe Ident)
  | JSLabel Ident [JS]
  | JSFunction (Maybe Ident) [Ident] [JS]
  | JSApp JS [JS]
  | JSConditional JS JS JS
  | JSBinary BinaryOperator JS JS
  | JSUnary UnaryOperator JS
  | JSAccessor PSString JS
  | JSIndexer JS JS
  | JSInstanceOf JS JS
  | JSComment [Comment] JS
  | JSRaw PSString

-- | Binary operators
data BinaryOperator
  = Add | Subtract | Multiply | Divide | Modulus
  | EqualTo | NotEqualTo | LessThan | LessThanOrEqualTo | GreaterThan | GreaterThanOrEqualTo
  | And | Or | BitwiseAnd | BitwiseOr | BitwiseXor | ShiftLeft | ShiftRight | ZeroFillShiftRight

-- | Unary operators  
data UnaryOperator = Negate | Not | BitwiseNot | Positive | New

Code Generation Pipeline

Multi-stage code generation with CoreFn to JavaScript transformation.

-- | Code generation pipeline stages
codegenPipeline :: CodegenOptions -> CF.Module CF.Ann -> JS

-- | Convert CoreFn expression to JavaScript
exprToJs :: CF.Expr CF.Ann -> JS

-- | Convert CoreFn binder to JavaScript pattern
binderToJs :: CF.Binder CF.Ann -> JS

-- | Generate top-level module wrapper
moduleWrapper :: ModuleName -> [JS] -> JS

-- | Generate CommonJS exports
generateExports :: ModuleName -> [DeclarationRef] -> JS

-- | Generate foreign function interface
generateFFI :: ModuleName -> JS

Runtime System Integration

Integration with PureScript's JavaScript runtime system.

-- | Runtime function calls
runtimeFunction :: RuntimeFunction -> [JS] -> JS

-- | Runtime functions
data RuntimeFunction
  = Fn [Ident] [JS]                    -- Curried function
  | Data ModuleName ProperName [JS]    -- Data constructor
  | Dict [(PSString, JS)]              -- Dictionary/record
  | Lazy JS                            -- Lazy evaluation
  | RunST JS                           -- ST monad runner
  | UnsafePartial JS                   -- Partial function marker

-- | Generate curry wrapper
curry :: [JS] -> JS

-- | Generate uncurried function call
uncurry :: JS -> [JS] -> JS

-- | Generate pattern matching
patternMatch :: [CF.Binder CF.Ann] -> JS -> [JS]

Optimization Passes

Multiple optimization passes for efficient JavaScript output.

-- | Apply all optimizations
optimize :: CodegenOptions -> JS -> JS

-- | Tail call optimization
optimizeTCO :: JS -> JS

-- | Magic do optimization (for Effect monad)
optimizeMagicDo :: JS -> JS

-- | Function inlining
optimizeInline :: JS -> JS

-- | Dead code elimination
optimizeUnused :: JS -> JS

-- | Block flattening
optimizeBlocks :: JS -> JS

-- | Common subexpression elimination
optimizeCSE :: JS -> JS

Source Map Generation

Source map generation for debugging support.

-- | Source map data
data SourceMap = SourceMap
  { smVersion :: Int
  , smSources :: [FilePath]
  , smNames :: [Text]
  , smMappings :: Text
  , smSourcesContent :: Maybe [Text]
  }

-- | Generate source map for module
generateSourceMap :: FilePath -> CF.Module CF.Ann -> JS -> SourceMap

-- | Source mapping information
data SourceMapping = SourceMapping
  { smGeneratedLine :: Int
  , smGeneratedColumn :: Int
  , smSourceFile :: Maybe FilePath
  , smSourceLine :: Maybe Int
  , smSourceColumn :: Maybe Int
  , smName :: Maybe Text
  }

-- | Add source mapping to JavaScript
addSourceMapping :: SourceMapping -> JS -> JS

-- | Encode source map to JSON
encodeSourceMap :: SourceMap -> Text

Pretty Printing

JavaScript pretty printing with configurable formatting options.

-- | Pretty print JavaScript
prettyPrintJS :: JS -> Text

-- | Pretty print with options
prettyPrintJSWithOptions :: PrintOptions -> JS -> Text

-- | Print options
data PrintOptions = PrintOptions
  { printIndent :: Int        -- Indentation size
  , printWidth :: Int         -- Maximum line width
  , printMinify :: Bool       -- Minify output
  , printComments :: Bool     -- Include comments
  , printSourceMap :: Maybe SourceMap -- Source map reference
  }

-- | Default print options
defaultPrintOptions :: PrintOptions

-- | Minified printing (single line)
minifyJS :: JS -> Text

Module System Integration

Integration with JavaScript module systems (CommonJS, ES6, AMD, UMD).

-- | Module system types
data ModuleSystem
  = CommonJS    -- Node.js require/exports
  | ES6         -- ES6 import/export
  | AMD         -- RequireJS
  | UMD         -- Universal Module Definition
  | IIFE        -- Immediately Invoked Function Expression

-- | Generate module wrapper for system
wrapModule :: ModuleSystem -> ModuleName -> JS -> JS

-- | Generate import statement
generateImport :: ModuleSystem -> ModuleName -> [Text] -> JS

-- | Generate export statement  
generateExport :: ModuleSystem -> [(Text, JS)] -> JS

-- | Generate AMD wrapper
amdWrapper :: ModuleName -> [ModuleName] -> JS -> JS

-- | Generate UMD wrapper
umdWrapper :: ModuleName -> [ModuleName] -> JS -> JS

Foreign Function Interface

FFI code generation and integration with JavaScript libraries.

-- | Generate FFI require statements
generateFFIRequire :: ModuleName -> FilePath -> JS

-- | Check FFI exports
checkFFIExports :: ModuleName -> [Ident] -> FilePath -> IO (Either [Ident] ())

-- | FFI binding generation
generateFFIBinding :: Ident -> SourceType -> JS

-- | Validate FFI file
validateFFI :: ModuleName -> FilePath -> IO [FFIError]

-- | FFI error types
data FFIError
  = FFIFileMissing
  | FFIExportMissing Ident
  | FFIParseError Text
  | FFITypeError Ident Text

Dead Code Elimination

Advanced dead code elimination and tree shaking.

-- | Dead code elimination analysis
data DCEAnalysis = DCEAnalysis
  { dceUsedIdentifiers :: S.Set Ident
  , dceReachableModules :: S.Set ModuleName
  , dceExportedNames :: M.Map ModuleName [Ident]
  }

-- | Analyze dead code across modules
analyzeDCE :: [CF.Module CF.Ann] -> DCEAnalysis

-- | Remove dead code from module
eliminateDeadCode :: DCEAnalysis -> CF.Module CF.Ann -> CF.Module CF.Ann

-- | Mark identifiers as used
markUsed :: Ident -> DCEAnalysis -> DCEAnalysis

-- | Check if identifier is used
isUsed :: Ident -> DCEAnalysis -> Bool

Bundle Generation

Bundle multiple modules into a single JavaScript file.

-- | Bundle configuration
data BundleConfig = BundleConfig
  { bundleEntryPoint :: ModuleName
  , bundleModules :: [CF.Module CF.Ann]
  , bundleMinify :: Bool
  , bundleSourceMap :: Bool
  , bundleWrapper :: ModuleSystem
  }

-- | Generate bundle from modules
generateBundle :: BundleConfig -> Text

-- | Bundle with dependency resolution
bundleWithDependencies :: ModuleName -> [CF.Module CF.Ann] -> Text

-- | Extract runtime dependencies
extractRuntimeDeps :: [CF.Module CF.Ann] -> [Text]

-- | Generate loader code
generateLoader :: [ModuleName] -> JS

Types

Code Generation Context

-- | Code generation context
data CodegenContext = CodegenContext
  { cgcOptions :: CodegenOptions
  , cgcModuleName :: ModuleName
  , cgcExports :: [DeclarationRef]
  , cgcForeignExports :: [Ident]
  , cgcSourceMap :: Maybe SourceMapBuilder
  }

-- | Source map builder state
data SourceMapBuilder = SourceMapBuilder
  { smbSources :: [FilePath]
  , smbNames :: [Text]
  , smbMappings :: [SourceMapping]
  , smbCurrentLine :: Int
  , smbCurrentColumn :: Int
  }

-- | Initialize code generation context
initCodegenContext :: CodegenOptions -> ModuleName -> [DeclarationRef] -> CodegenContext

JavaScript Names

-- | JavaScript identifier mangling
mangleIdent :: Ident -> Text

-- | Mangle module name for JavaScript
mangleModuleName :: ModuleName -> Text

-- | Mangle property name
manglePropertyName :: PSString -> Text

-- | Reserved JavaScript identifiers
jsReserved :: S.Set Text

-- | Check if identifier needs mangling
needsMangling :: Text -> Bool

-- | Generate safe JavaScript identifier
safeName :: Text -> Text

Output Organization

-- | Output file structure
data OutputFile = OutputFile
  { ofPath :: FilePath
  , ofContent :: Text
  , ofSourceMap :: Maybe SourceMap
  }

-- | Generate output files for module
generateOutputFiles :: CodegenOptions -> FilePath -> CF.Module CF.Ann -> [OutputFile]

-- | Output directory structure
data OutputStructure = OutputStructure
  { osModuleDir :: FilePath      -- Module-specific directory
  , osIndexJs :: FilePath        -- Main JavaScript file
  , osIndexJsMap :: FilePath     -- Source map file
  , osExterns :: FilePath        -- Externs file
  , osPackageJson :: FilePath    -- Package.json for module
  }

-- | Create output structure for module
createOutputStructure :: FilePath -> ModuleName -> OutputStructure

Install with Tessl CLI

npx tessl i tessl/hackage-purescript

docs

ast.md

build.md

cli.md

codegen.md

corefn.md

docs.md

errors.md

ide.md

index.md

interactive.md

parser.md

sugar.md

types.md

tile.json