1.0.0 • Published 5 years ago
@kingjs/parse-source v1.0.0
@kingjs/parse-source
Expresses a TypeScript AST as an object literal.
Usage
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
parse(path, options)
Parameters
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.
Returns
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 |
Source
https://repository.kingjs.net/parse-source
License
MIT
1.0.0
5 years ago