JScriptor is a lightweight, typed superset π of JavaScript that catches type errors early and keeps your codebase clean and maintainableβwithout any heavy setup.
- π 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
- β
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
npm install --save-dev jscriptor
- 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
- Initialize configuration (optional):
jscriptor init
- Add to
package.json
scripts:
{
"scripts": {
"typecheck": "jscriptor check-all",
"typecheck:file": "jscriptor check"
}
}
- Run type checking:
# Check all files based on config
npm run typecheck
# Check a specific file
npm run typecheck:file src/app.js
We've created a complete example project that demonstrates using JScriptor from the npm registry:
# 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
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
npm run typecheck
- Check all files based on confignpm run typecheck:file
- Check a specific filenpm run init
- Initialize JScriptor configuration
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
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
// 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 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
- compile β Parses JavaScript into an AST
- typeCheck β Infers and validates types
- nameCheck β Checks naming and scope rules
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
jscriptor init
: Creates ajscriptor.config.js
file with sensible defaultsjscriptor check <file>
: Type checks a single JavaScript filejscriptor check-all
: Type checks all files matching patterns in your configjscriptor help
: Displays usage information and available commands
JScriptor uses a jscriptor.config.js
file to configure type checking behavior. Run jscriptor init
to create a default configuration file.
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",
}
};
include
: Glob patterns for files to type checkexclude
: Glob patterns for files to skipstrict
: Strict mode settings for additional checkstypeCheck
: Core type checking behaviorcompiler
: Compilation and output options
- β Basic type inference and checking
- β CLI interface with configuration support
- β Error reporting with file locations
- β Support for common JavaScript constructs
- π 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
If you want to contribute or test JS Scriptor locally instead of installing from npm:
-
Clone the repo
git clone https://github.com/Hareesh108/jscriptor.git cd jscriptor
-
Install dependencies
npm install
-
Link the CLI locally This makes the
jscriptor
command available on your system:npm link
-
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
andString
. -
Unlink when done If you no longer want the global link:
npm unlink -g jscriptor
- 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
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
MIT Β© 2025 Hareesh Bhittam