Expresses a TypeScript AST as an object literal.
npm install @kingjs/parse-sourcejs
var assert = require('assert');
var parseSource = require('@kingjs/parse-source');var ast = parseSource(__filename, {
type: true,
lines: true,
character: true
});
var {
SourceFile,
VariableStatement,
VariableDeclaration,
CallExpression
} = parseSource;
assert(ast instanceof SourceFile);
var statement = ast.statements[0];
assert(statement instanceof VariableStatement);
var declaration = statement.declarationList[0];
assert(declaration instanceof VariableDeclaration);
assert(declaration.name == 'assert');
var callExpression = declaration.initializer;
assert(callExpression instanceof CallExpression);
assert(callExpression.expression == 'require');
assert(callExpression.arguments[0] == 'assert');
var json = JSON.stringify(ast, null, 2);
console.log(json);
`API
`ts
parse(path, options)
`$3
- path: Path to file to a file to parse.
- options: Options to add type, and positioning information to AST.
- options.type: If true, type information is added to the AST as . properties so it'll appear if the AST is serialized to JSON.
- options.line: If true, line numbers are added to the AST.
- options.character: If true, character position is added to the AST.
$3
Each node of the Typescript AST is stripped down to just those properties that return nodes or are terminal literals.
Install
With npm installed, run
`
$ npm install @kingjs/parse-source
`
Dependencies
|Package|Version|
|---|---|
|@kingjs/reflect.is|^1.0.1|
|typescript|^3.2.4`|[@kingjs]: https://www.npmjs.com/package/kingjs
[ns0]: https://www.npmjs.com/package/@kingjs/parse-source