Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
@typescript-eslint/typescript-estree
Advanced tools
A parser that converts TypeScript source code into an ESTree compatible form
The @typescript-eslint/typescript-estree package is a parser that converts TypeScript source code into an ESTree-compatible form. It is primarily used in the context of ESLint to enable linting of TypeScript code by converting TypeScript syntax into a format that ESLint can understand and work with.
Parsing TypeScript code to ESTree
This feature allows you to parse TypeScript code and get an Abstract Syntax Tree (AST) that is compatible with ESTree. This is useful for tools that need to analyze or manipulate the syntax of TypeScript code.
const tsEStree = require('@typescript-eslint/typescript-estree');
const code = 'let x: number = 1;';
const ast = tsEStree.parse(code, { jsx: false });
console.log(ast);
Parsing TypeScript code with JSX
This feature is similar to the previous one but includes support for JSX syntax, which is commonly used in React applications. It allows the parser to correctly interpret JSX elements within TypeScript code.
const tsEStree = require('@typescript-eslint/typescript-estree');
const code = '<div>Hello, TypeScript!</div>';
const ast = tsEStree.parse(code, { jsx: true });
console.log(ast);
Parsing a file
This feature allows you to parse the contents of a TypeScript file by reading the file and then parsing the code. It is useful when you want to analyze or lint a file directly.
const tsEStree = require('@typescript-eslint/typescript-estree');
const fs = require('fs');
const filePath = './example.ts';
const code = fs.readFileSync(filePath, 'utf8');
const ast = tsEStree.parse(code, { filePath });
console.log(ast);
This package was the predecessor to @typescript-eslint/typescript-estree and has been deprecated in favor of the newer package. It provided similar functionality in terms of parsing TypeScript code for ESLint.
babel-eslint is a parser that allows ESLint to run on source code that is transpiled with Babel. While it is not TypeScript-specific, it can be used with Babel's TypeScript preset to parse TypeScript code.
Espree is the default parser for ESLint and is built on top of Acorn. It is designed to parse ECMAScript (JavaScript) code. While it does not natively support TypeScript, it serves a similar purpose for JavaScript code as @typescript-eslint/typescript-estree does for TypeScript.
A parser that converts TypeScript source code into an ESTree-compatible form
This parser is somewhat generic and robust, and could be used to power any use-case which requires taking TypeScript source code and producing an ESTree-compatible AST.
In fact, it is already used within these hyper-popular open-source projects to power their TypeScript support:
npm install @typescript-eslint/typescript-estree --save-dev
Parses the given string of code with the options provided and returns an ESTree-compatible AST. The options object has the following properties:
{
// attach range information to each node
range: false,
// attach line/column location information to each node
loc: false,
// create a top-level tokens array containing all tokens
tokens: false,
// create a top-level comments array containing all comments
comment: false,
// enable parsing JSX. For more details, see https://www.typescriptlang.org/docs/handbook/jsx.html
jsx: false,
/*
* The JSX AST changed the node type for string literals
* inside a JSX Element from `Literal` to `JSXText`.
* When value is `true`, these nodes will be parsed as type `JSXText`.
* When value is `false`, these nodes will be parsed as type `Literal`.
*/
useJSXTextNode: false,
// Cause the parser to error if it encounters an unknown AST node type (useful for testing)
errorOnUnknownASTType: false,
/*
* Allows overriding of function used for logging.
* When value is `false`, no logging will occur.
* When value is not provided, `console.log()` will be used.
*/
loggerFn: undefined
}
Example usage:
const parser = require('@typescript-eslint/typescript-estree');
const code = `const hello: string = 'world';`;
const ast = parser.parse(code, {
range: true,
loc: true,
});
Exposes the current version of typescript-estree as specified in package.json.
Example usage:
const parser = require('@typescript-eslint/typescript-estree');
const version = parser.version;
Exposes an object that contains the AST node types produced by the parser.
Example usage:
const parser = require('@typescript-eslint/typescript-estree');
const astNodeTypes = parser.AST_NODE_TYPES;
We will always endeavor to support the latest stable version of TypeScript.
The version of TypeScript currently supported by this parser is ~3.2.1
. This is reflected in the devDependency
requirement within the package.json file, and it is what the tests will be run against. We have an open peerDependency
requirement in order to allow for experimentation on newer/beta versions of TypeScript.
If you use a non-supported version of TypeScript, the parser will log a warning to the console.
Please ensure that you are using a supported version before submitting any issues/bug reports.
Please check the current list of open and known issues and ensure the issue has not been reported before. When creating a new issue provide as much information about your environment as possible. This includes:
typescript-estree
versionA couple of years after work on this parser began, the TypeScript Team at Microsoft began officially supporting TypeScript parsing via Babel.
I work closely with TypeScript Team and we are gradually aliging the AST of this project with the one produced by Babel's parser. To that end, I have created a full test harness to compare the ASTs of the two projects which runs on every PR, please see the code for more details.
npm test
- run all testsnpm run unit-tests
- run only unit testsnpm run ast-alignment-tests
- run only Babylon AST alignment testsTypeScript ESTree inherits from the the original TypeScript ESLint Parser license, as the majority of the work began there. It is licensed under a permissive BSD 2-clause license.
FAQs
A parser that converts TypeScript source code into an ESTree compatible form
The npm package @typescript-eslint/typescript-estree receives a total of 32,040,583 weekly downloads. As such, @typescript-eslint/typescript-estree popularity was classified as popular.
We found that @typescript-eslint/typescript-estree demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers 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.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.