Skip to content

πŸ› οΈ JS Scriptor is a lightweight, experimental JavaScript type checker that catches type mismatches before runtime πŸš€.

License

Notifications You must be signed in to change notification settings

Hareesh108/jscriptor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

68 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ› οΈ JScriptor β€” Lightweight Typed Superset for JavaScript

JScriptor is a lightweight, typed superset πŸš€ of JavaScript that catches type errors early and keeps your codebase clean and maintainableβ€”without any heavy setup.


✨ Features

  • πŸ” Type Inference: Automatic type detection using Hindley-Milner type system
  • 🧠 Polymorphic Functions: Supports functions that work with multiple types
  • πŸ“¦ Zero Configuration: Works out of the box with sensible defaults
  • πŸ’‘ Clear Error Reporting: Detailed type error messages with file locations
  • βš™οΈ Configurable: Customize type checking behavior via jscriptor.config.js
  • πŸš€ Fast: Lightweight implementation with minimal overhead
  • πŸ”§ CLI Tools: Command-line interface for easy integration

Supported JavaScript Features

  • βœ… Variable declarations (const, let)
  • βœ… Arrow functions and function calls
  • βœ… Binary expressions (+, -, *, /, ==, !=, etc.)
  • βœ… Conditional expressions (ternary operator)
  • βœ… Array and object literals
  • βœ… String, number, and boolean literals
  • βœ… Return statements
  • βœ… Block statements with scope management

πŸ“¦ Installation

npm install --save-dev jscriptor

⚑ Quick Start

  1. Create your program:
// myProgram.js
const double = (x) => { return x + x; };
const num = 5;
const str = "hello";

const doubledNum = double(num);
const mixed = double(num) + double(str); // ❌ Type error
  1. Initialize configuration (optional):
jscriptor init
  1. Add to package.json scripts:
{
  "scripts": {
    "typecheck": "jscriptor check-all",
    "typecheck:file": "jscriptor check"
  }
}
  1. Run type checking:
# Check all files based on config
npm run typecheck

# Check a specific file
npm run typecheck:file src/app.js

πŸ“¦ NPM Registry Example

We've created a complete example project that demonstrates using JScriptor from the npm registry:

Quick Setup

# Clone the example
git clone https://github.com/Hareesh108/jscriptor.git
cd jscriptor/npm-example

# Install JScriptor from npm registry
npm install --save-dev jscriptor

# Initialize configuration
npm run init

# Run type checking
npm run typecheck

Example Project Structure

npm-example/
β”œβ”€β”€ package.json              # Dependencies and scripts
β”œβ”€β”€ jscriptor.config.js       # JScriptor configuration
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ simple-valid.js       # Valid code (no errors)
β”‚   β”œβ”€β”€ simple-errors.js      # Code with type errors
β”‚   └── utils.js              # Utility functions
└── README.md                 # Example documentation

Available Scripts

  • npm run typecheck - Check all files based on config
  • npm run typecheck:file - Check a specific file
  • npm run init - Initialize JScriptor configuration

Test Results

The example demonstrates:

  • βœ… Valid code: simple-valid.js passes type checking
  • ❌ Type errors: simple-errors.js shows clear error messages
  • πŸ”§ Configuration: Customizable via jscriptor.config.js

πŸ–₯️ Example Output

JScriptor provides clear, detailed error messages:

πŸ“„ Checking src/simple-errors.js...

❌ 1 error(s) found

1. [E_TERNARY_TEST_NOT_BOOL] Type mismatch in ternary: condition must be Boolean, got Number
   at src/simple-errors.js:15:20

 13 | 
 14 | // Type error: ternary with non-boolean condition
 15 | const badTernary = num ? "yes" : "no";
    |                    ^
 16 | 
 17 | console.log("This file has type errors!");
   πŸ’‘ Hint: The condition in a ternary operator must evaluate to a boolean

πŸ“Š Summary: Found 1 type error(s) in src/simple-errors.js

πŸ§ͺ More Examples

βœ… Valid Code (No Type Errors)

// Function with type inference
const identity = (x) => { return x; };
const result = identity(42); // βœ… Number type inferred

// Binary operations with matching types
const sum = 5 + 10; // βœ… Number + Number
const message = "Hello" + " World"; // βœ… String + String

// Conditional expressions
const max = a > b ? a : b; // βœ… Boolean condition

❌ Type Errors

// Type mismatch in binary operation
const badMath = 5 + "hello"; // ❌ Number + String

// Wrong argument type for function
const add = (x, y) => { return x + y; };
const res = add(5, "hello"); // ❌ String passed to function expecting Number

// Non-boolean condition in ternary
const num = 5;
const result = num ? "yes" : "no"; // ❌ Number used as condition

πŸ›  How It Works

Compiler Design

  • compile β†’ Parses JavaScript into an AST
  • typeCheck β†’ Infers and validates types
  • nameCheck β†’ Checks naming and scope rules

πŸ–₯️ CLI Commands

JScriptor provides a comprehensive command-line interface:

# Initialize a new project with default configuration
jscriptor init

# Type check a single file
jscriptor check src/app.js

# Type check all files based on jscriptor.config.js
jscriptor check-all

# Show help and usage information
jscriptor help

Command Details

  • jscriptor init: Creates a jscriptor.config.js file with sensible defaults
  • jscriptor check <file>: Type checks a single JavaScript file
  • jscriptor check-all: Type checks all files matching patterns in your config
  • jscriptor help: Displays usage information and available commands

βš™οΈ Configuration

JScriptor uses a jscriptor.config.js file to configure type checking behavior. Run jscriptor init to create a default configuration file.

Configuration Options

module.exports = {
  // Entry points - files or directories to type-check
  include: [
    "src/**/*.js",
    "lib/**/*.js"
  ],
  
  // Files to exclude from type checking
  exclude: [
    "node_modules/**/*",
    "dist/**/*",
    "build/**/*",
    "**/*.test.js",
    "**/*.spec.js"
  ],
  
  // Output directory for compiled files (optional)
  outDir: null,
  
  // Whether to watch for file changes
  watch: false,
  
  // Strict mode settings
  strict: {
    // Require explicit type annotations
    explicitTypes: false,
    // Check for unused variables
    unusedVars: false,
    // Check for unreachable code
    unreachableCode: false,
  },
  
  // Type checking options
  typeCheck: {
    // Whether to infer types from usage
    inferTypes: true,
    // Whether to check function return types
    checkReturnTypes: true,
    // Whether to check parameter types
    checkParameterTypes: true,
  },
  
  // Compiler options
  compiler: {
    // Whether to preserve comments
    preserveComments: true,
    // Whether to generate source maps
    sourceMaps: false,
    // Target JavaScript version
    target: "es2020",
  }
};

Configuration Sections

  • include: Glob patterns for files to type check
  • exclude: Glob patterns for files to skip
  • strict: Strict mode settings for additional checks
  • typeCheck: Core type checking behavior
  • compiler: Compilation and output options

πŸ“‹ Roadmap

Current Version (v0.0.6)

  • βœ… Basic type inference and checking
  • βœ… CLI interface with configuration support
  • βœ… Error reporting with file locations
  • βœ… Support for common JavaScript constructs

Upcoming Features

  • 🌐 Enhanced CLI output with syntax highlighting
  • 🧩 Plugin system for custom type rules
  • πŸ– VS Code extension for real-time type checking
  • πŸ“š Type annotations support (optional)
  • πŸ”„ Watch mode for continuous type checking
  • πŸ“Š Performance optimizations for large codebases
  • πŸ§ͺ More comprehensive test coverage
  • πŸ“– Detailed documentation and tutorials

πŸ”§ Local Development Setup

If you want to contribute or test JS Scriptor locally instead of installing from npm:

  1. Clone the repo

    git clone https://github.com/Hareesh108/jscriptor.git
    cd jscriptor
  2. Install dependencies

    npm install
  3. Link the CLI locally This makes the jscriptor command available on your system:

    npm link
  4. Test it on an example file

    echo 'const x = 5; const y = "hi"; const z = x + y;' > examples/test.js
    jscriptor examples/test.js

    βœ… You should see a type error reported for mixing Number and String.

  5. Unlink when done If you no longer want the global link:

    npm unlink -g jscriptor

πŸ›  Development Workflow

  • Source code lives in src/
  • CLI entrypoint β†’ src/cli.js
  • Tokenization β†’ src/01-tokenize/
  • Parsing β†’ src/02-parse/
  • Type checking β†’ src/03-typecheck/
  • Compiler β†’ src/compile.js
  • Configuration β†’ src/config.js

Project Structure

src/
β”œβ”€β”€ cli.js                 # Command-line interface
β”œβ”€β”€ compile.js             # Main compilation entry point
β”œβ”€β”€ config.js              # Configuration loading and processing
β”œβ”€β”€ 01-tokenize/           # Lexical analysis
β”‚   β”œβ”€β”€ tokenize.js        # Tokenizer implementation
β”‚   └── utils.js           # Token utilities
β”œβ”€β”€ 02-parse/              # Syntax analysis
β”‚   └── parse.js           # Parser implementation
β”œβ”€β”€ 03-typecheck/          # Type checking and inference
β”‚   β”œβ”€β”€ typecheck.js       # Main type checker
β”‚   β”œβ”€β”€ db.js              # Type database
β”‚   β”œβ”€β”€ errors.js          # Error reporting
β”‚   β”œβ”€β”€ scope.js           # Scope management
β”‚   β”œβ”€β”€ unification.js     # Type unification
β”‚   β”œβ”€β”€ type-utils.js      # Type utilities
β”‚   β”œβ”€β”€ utils.js           # General utilities
β”‚   └── visitors/          # AST visitors
β”‚       β”œβ”€β”€ index.js       # Visitor dispatcher
β”‚       β”œβ”€β”€ declarations.js # Declaration visitors
β”‚       β”œβ”€β”€ expressions.js  # Expression visitors
β”‚       β”œβ”€β”€ functions.js    # Function visitors
β”‚       β”œβ”€β”€ identifiers.js  # Identifier visitors
β”‚       └── literals.js     # Literal visitors
└── test/                  # Test files
    β”œβ”€β”€ check.js           # Test runner
    β”œβ”€β”€ test.js            # Test utilities
    └── typecheck.spec.js  # Type checker tests

You can run your local changes directly with:

# Test the CLI
jscriptor check ./src/test/check.js 

# Test with example project
jscriptor check-all

πŸ“œ License

MIT Β© 2025 Hareesh Bhittam

About

πŸ› οΈ JS Scriptor is a lightweight, experimental JavaScript type checker that catches type mismatches before runtime πŸš€.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •