[luaparse] Added more complete typings for the luaparse library (#35664)

* Added more complete typings for the luaparse library, including definitions for the lua ast.

* Added typing for token and corrected onCreateScope and onDestroyScope options.

* Correct node typing in onCreateNode option.

* Corrected body property of ForGenericStatement.

* Added LogicalExpression to ast.

* Removed const enum and replaced types with raw strings. Also tidied up Node type by utilising Statement and Expression.

* Split some long comments into multiple lines. Removed overridden line length linting rule.
This commit is contained in:
thomasfn
2019-05-31 08:42:58 -07:00
committed by Sheetal Nandi
parent 0bb8e0ea97
commit ef685c5a82
4 changed files with 305 additions and 87 deletions
+58 -3
View File
@@ -1,7 +1,62 @@
// Type definitions for luaparse
// Project: https://github.com/oxyc/luaparse
// Type definitions for luaparse 0.2
// Project: https://oxyc.github.io/luaparse
// Definitions by: Sam Saint-Pettersen <https://github.com/stpettersens>
// thomasfn <https://github.com/thomasfn>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.4
import * as ast from "./lib/ast";
export * from "./lib/ast";
export declare function parse(code: string, options?: Object): Object;
export interface Options {
/** Explicitly tell the parser when the input ends. */
wait: boolean;
/** Store comments as an array in the chunk object. */
comments: boolean;
/** Track identifier scopes. */
scope: boolean;
/** Store location information on each syntax node. */
locations: boolean;
/** Store the start and end character locations on each syntax node. */
ranges: boolean;
/**
* A callback which will be invoked when a syntax node has been completed.
* The node which has been created will be passed as the only parameter.
*/
onCreateNode: (node: ast.Node) => void;
/** A callback which will be invoked when a new scope is created. */
onCreateScope: () => void;
/** A callback which will be invoked when the current scope is destroyed. */
onDestroyScope: () => void;
/**
* A callback which will be invoked when a local variable is declared.
* The identifier will be passed as the only parameter.
*/
onLocalDeclaration: (identifier: ast.Identifier) => void;
/** The version of Lua the parser will target; supported values are '5.1', '5.2', '5.3' and 'LuaJIT'. */
luaVersion: "5.1" | "5.2" | "5.3" | "LuaJIT";
/**
* Whether to allow code points ≥ U+0080 in identifiers, like LuaJIT does.
* See 'Note on character encodings' below if you wish to use this option.
* Note: setting luaVersion: 'LuaJIT' currently does not enable this option; this may change in the future.
*/
extendedIdentifiers: false;
}
export interface Token {
type: number;
value: string;
line: number;
lineStart: number;
range: [number, number];
}
export interface Parser {
write(segment: string): void;
end(segment: string): ast.Chunk;
lex(): Token;
}
export function parse(code: string, options: Partial<Options> & { wait: true }): Parser;
export function parse(code: string, options?: Partial<Options>): ast.Chunk;
export function parse(options?: Partial<Options>): Parser;
+230
View File
@@ -0,0 +1,230 @@
export interface Base<TType extends string> {
type: TType;
}
export interface LabelStatement extends Base<"LabelStatement"> {
label: string;
}
export type BreakStatement = Base<"BreakStatement">;
export interface GotoStatement extends Base<"GotoStatement"> {
label: string;
}
export interface ReturnStatement extends Base<"ReturnStatement"> {
arguments: Expression[];
}
export interface IfStatement extends Base<"IfStatement"> {
clauses: IfClause[];
}
export interface IfClause extends Base<"IfClause"> {
condition: Expression;
body: Statement[];
}
export interface ElseifClause extends Base<"ElseifClause"> {
condition: Expression;
body: Statement[];
}
export interface ElseClause extends Base<"ElseClause"> {
body: Statement[];
}
export interface WhileStatement extends Base<"WhileStatement"> {
condition: Expression;
body: Statement[];
}
export interface DoStatement extends Base<"DoStatement"> {
body: Statement[];
}
export interface RepeatStatement extends Base<"RepeatStatement"> {
condition: Expression;
body: Statement[];
}
export interface LocalStatement extends Base<"LocalStatement"> {
variables: Identifier[];
init: Expression[];
}
export interface AssignmentStatement extends Base<"AssignmentStatement"> {
variables: Array<IndexExpression | MemberExpression | Identifier>;
init: Expression[];
}
export interface CallStatement extends Base<"CallStatement"> {
expression: CallExpression | StringCallExpression | TableCallExpression;
}
export interface FunctionDeclaration extends Base<"FunctionDeclaration"> {
identifier: Identifier | null;
isLocal: boolean;
parameters: Identifier[];
body: Statement[];
}
export interface ForNumericStatement extends Base<"ForNumericStatement"> {
variable: Identifier;
start: Expression;
end: Expression;
step: Expression | null;
body: Statement[];
}
export interface ForGenericStatement extends Base<"ForGenericStatement"> {
variables: Identifier[];
iterators: Expression[];
body: Statement[];
}
export interface Chunk extends Base<"Chunk"> {
body: Statement[];
comments: string[];
}
export interface Identifier extends Base<"Identifier"> {
name: string;
}
export interface StringLiteral extends Base<"StringLiteral"> {
value: string;
raw: string;
}
export interface NumericLiteral extends Base<"NumericLiteral"> {
value: number;
raw: string;
}
export interface BooleanLiteral extends Base<"BooleanLiteral"> {
value: boolean;
raw: string;
}
export interface NilLiteral extends Base<"NilLiteral"> {
value: null;
raw: string;
}
export interface VarargLiteral extends Base<"VarargLiteral"> {
value: string;
raw: string;
}
export interface TableKey extends Base<"TableKey"> {
key: Expression;
value: Expression;
}
export interface TableKeyString extends Base<"TableKeyString"> {
key: Identifier;
value: Expression;
}
export interface TableValue extends Base<"TableValue"> {
value: Expression;
}
export interface TableConstructorExpression extends Base<"TableConstructorExpression"> {
fields: Array<TableKey | TableKeyString>;
}
export interface UnaryExpression extends Base<"UnaryExpression"> {
operator: "#" | "not";
argument: Expression;
}
export interface BinaryExpression extends Base<"BinaryExpression"> {
operator: "+" | "-" | "*" | "/" | "%" | "^" | "==" | "~=" | "<=" | ">=" | "<" | ">" | "..";
left: Expression;
right: Expression;
}
export interface LogicalExpression extends Base<"LogicalExpression"> {
operator: "or" | "and";
left: Expression;
right: Expression;
}
export interface MemberExpression extends Base<"MemberExpression"> {
indexer: string;
identifier: Identifier;
base: Expression;
}
export interface IndexExpression extends Base<"IndexExpression"> {
base: Expression;
index: Expression;
}
export interface CallExpression extends Base<"CallExpression"> {
base: Expression;
arguments: Expression[];
}
export interface TableCallExpression extends Base<"TableCallExpression"> {
base: Expression;
arguments: Expression[];
}
export interface StringCallExpression extends Base<"StringCallExpression"> {
base: Expression;
argument: Expression;
}
export interface Comment extends Base<"Comment"> {
value: string;
raw: string;
}
export type Expression =
FunctionDeclaration |
Identifier |
StringLiteral |
NumericLiteral |
BooleanLiteral |
NilLiteral |
VarargLiteral |
TableConstructorExpression |
BinaryExpression |
LogicalExpression |
UnaryExpression |
MemberExpression |
IndexExpression |
CallExpression |
TableCallExpression |
StringCallExpression;
export type Statement =
LabelStatement |
BreakStatement |
GotoStatement |
ReturnStatement |
IfStatement |
WhileStatement |
DoStatement |
RepeatStatement |
LocalStatement |
AssignmentStatement |
CallStatement |
FunctionDeclaration |
ForNumericStatement |
ForGenericStatement;
export type Node =
Statement |
Expression |
IfClause |
ElseifClause |
ElseClause |
Chunk |
TableKey |
TableKeyString |
TableValue |
Comment;
+16 -6
View File
@@ -1,9 +1,19 @@
import luaparse = require("luaparse");
import luaparse = require('luaparse');
const luacode = "-- this is hello world in lua.\nprint('hello world')";
var luacode: string = '-- this is hello world in lua.\nprint(\'hello world\')';
var ast: Object = luaparse.parse(luacode);
console.log(JSON.stringify(ast));
// $ExpectType Chunk
const ast1 = luaparse.parse(luacode);
console.log(JSON.stringify(ast1));
ast = luaparse.parse(luacode, {comments: false});
console.log(JSON.stringify(ast));
// $ExpectType Chunk
const ast2 = luaparse.parse(luacode, {comments: false});
console.log(JSON.stringify(ast2));
// $ExpectType Parser
const ast3 = luaparse.parse({wait: true});
console.log(JSON.stringify(ast3));
// $ExpectType Parser
const ast4 = luaparse.parse(luacode, {wait: true});
console.log(JSON.stringify(ast4));
+1 -78
View File
@@ -1,80 +1,3 @@
{
"extends": "dtslint/dt.json",
"rules": {
"adjacent-overload-signatures": false,
"array-type": false,
"arrow-return-shorthand": false,
"ban-types": false,
"callable-types": false,
"comment-format": false,
"dt-header": false,
"npm-naming": false,
"eofline": false,
"export-just-namespace": false,
"import-spacing": false,
"interface-name": false,
"interface-over-type-literal": false,
"jsdoc-format": false,
"max-line-length": false,
"member-access": false,
"new-parens": false,
"no-any-union": false,
"no-boolean-literal-compare": false,
"no-conditional-assignment": false,
"no-consecutive-blank-lines": false,
"no-construct": false,
"no-declare-current-package": false,
"no-duplicate-imports": false,
"no-duplicate-variable": false,
"no-empty-interface": false,
"no-for-in-array": false,
"no-inferrable-types": false,
"no-internal-module": false,
"no-irregular-whitespace": false,
"no-mergeable-namespace": false,
"no-misused-new": false,
"no-namespace": false,
"no-object-literal-type-assertion": false,
"no-padding": false,
"no-redundant-jsdoc": false,
"no-redundant-jsdoc-2": false,
"no-redundant-undefined": false,
"no-reference-import": false,
"no-relative-import-in-test": false,
"no-self-import": false,
"no-single-declare-module": false,
"no-string-throw": false,
"no-unnecessary-callback-wrapper": false,
"no-unnecessary-class": false,
"no-unnecessary-generics": false,
"no-unnecessary-qualifier": false,
"no-unnecessary-type-assertion": false,
"no-useless-files": false,
"no-var-keyword": false,
"no-var-requires": false,
"no-void-expression": false,
"no-trailing-whitespace": false,
"object-literal-key-quotes": false,
"object-literal-shorthand": false,
"one-line": false,
"one-variable-per-declaration": false,
"only-arrow-functions": false,
"prefer-conditional-expression": false,
"prefer-const": false,
"prefer-declare-function": false,
"prefer-for-of": false,
"prefer-method-signature": false,
"prefer-template": false,
"radix": false,
"semicolon": false,
"space-before-function-paren": false,
"space-within-parens": false,
"strict-export-declare-modifiers": false,
"trim-file": false,
"triple-equals": false,
"typedef-whitespace": false,
"unified-signatures": false,
"void-return": false,
"whitespace": false
}
"extends": "dtslint/dt.json"
}