
Research
/Security News
Fake imToken Chrome Extension Steals Seed Phrases via Phishing Redirects
Mixed-script homoglyphs and a lookalike domain mimic imToken’s import flow to capture mnemonics and private keys.
TS PEG.js is a TS code generation plugin for peggy.
Installs ts-pegjs + peggy
$ npm install ts-pegjs
In Node.js, require both the peggy parser generator and the ts-pegjs plugin:
var peggy = require('peggy');
var tspegjs = require('ts-pegjs');
To generate a TS parser, pass to pegjs.generate ts-pegjs plugin and your grammar:
var parser = pegjs.generate("start = ('a' / 'b')+", {
output: 'source',
format: 'commonjs',
plugins: [tspegjs],
tspegjs: {
customHeader: "// import lib\nimport { Lib } from 'mylib';"
}
});
The method will return source code of generated parser as a string.
Supported options of pegjs.generate:
cache — if true, makes the parser cache results, avoiding exponential
parsing time in pathological cases but making the parser slower (default:
false). This is strongly recommended for big grammars
(like javascript.pegjs or css.pegjs in example folder)allowedStartRules — rules the parser will be allowed to start parsing from
(default: the first rule in the grammar)Note: Options in CLI mode are written in POSIX (long names as kebab-case) convention e.g. --custom-header but with camelcase on JavaScript e.g. customHeader.
customHeader — A string or an array of strings which are a valid TS code to be injected on the header of the output file. E.g. provides a convenient place for adding library imports.customHeaderFile — A header file to include.errorName — The name of the exported internal error class to override. For backward compatibility the default value is SyntaxError.returnTypes — An object containing rule names as keys and a valid TS return type as string.Sample usage:
peggy --plugin ./src/tspegjs -o examples/arithmetics.ts --cache examples/arithmetics.pegjs
(Note ./src/tspegjs is the path to tspegjs.js in the project. If you installed ts-pegjs using npm, it should probably be ./node_modules/ts-pegjs/src/tspegjs.)
It will generarate the parser in the TS flavour.
If you need to pass specific plugin options you can use the option --extra-options-file provided by pegjs and pass it a filename (e.g. pegconfig.json) containing specific options like the following JSON sample:
peggy --plugin ./src/tspegjs --extra-options-file pegconfig.json -o examples/arithmetics.ts --cache examples/arithmetics.pegjs
{
"tspegjs": {
"customHeader": "// import lib\nimport { Lib } from 'mylib';"
},
"returnTypes": {
"Integer": "number",
"Expression": "number",
}
}
For rules not listed in
returnTypesobjectanytype is declared by default.
Make sure to pass any additional CLI options, like
--extra-options-filebefore the parameter-oas these will otherwise be treated as arguments to that one.
Save parser generated by pegjs.generate to a file or use the one generated from the CLI tool.
In client TS code:
import { SyntaxError, parse } from './arithmetics';
try {
const sampleOutput = parse('my sample...');
} catch (ex: SyntaxError) {
// Handle parsing error
// [...]
}
Thanks to:
(c) 2017-2022, Pedro J. Molina at metadev.pro
FAQs
TS target for peggy parser generator
The npm package ts-pegjs receives a total of 34,889 weekly downloads. As such, ts-pegjs popularity was classified as popular.
We found that ts-pegjs demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Research
/Security News
Mixed-script homoglyphs and a lookalike domain mimic imToken’s import flow to capture mnemonics and private keys.

Security News
Latio’s 2026 report recognizes Socket as a Supply Chain Innovator and highlights our work in 0-day malware detection, SCA, and auto-patching.

Company News
Join Socket for live demos, rooftop happy hours, and one-on-one meetings during BSidesSF and RSA 2026 in San Francisco.