Skip to content

Commit

Permalink
Add support for TypeScript (#260)
Browse files Browse the repository at this point in the history
Add build system infrastructure to slowly migrate all code to
TypeScript.

We need to immediately migrate all imports/exports to ES6 syntax
because otherwise the module does not compile at all and the
library exports are completely broken.
  • Loading branch information
gcampax authored Sep 22, 2020
1 parent 563c910 commit d32290e
Show file tree
Hide file tree
Showing 103 changed files with 1,251 additions and 1,027 deletions.
4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
lib/grammar.js
lib/nn-syntax/parser.js
node_modules
dist
coverage
93 changes: 75 additions & 18 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,31 +1,44 @@
env:
es6: true
node: true
parser: '@typescript-eslint/parser'
plugins:
- '@typescript-eslint'
parserOptions:
ecmaVersion: 2018
extends: 'eslint:recommended'
extends:
- 'eslint:recommended'
- 'plugin:@typescript-eslint/recommended'
rules:
indent: off
no-console: off
no-fallthrough: off
linebreak-style:
- error
- unix
semi:
'@typescript-eslint/no-empty-function': off
require-atomic-updates: off
no-lonely-if: off
arrow-body-style: off

# temporary until migration is complete
'@typescript-eslint/no-this-alias': off

# correctness checks
strict:
- error
- always
- global
consistent-return: error
curly:
- error
- multi-or-nest
- consistent
array-callback-return: error
eqeqeq:
- error
- always
no-unused-vars:
no-var: error
no-unused-vars: off
'@typescript-eslint/no-unused-vars':
- error
-
varsIgnorePattern: _
args: none
no-shadow-restricted-names: error
no-promise-executor-return: error
no-case-declarations: warn
no-eval: error
no-proto: error
Expand All @@ -35,16 +48,60 @@ rules:
no-useless-call: warn
no-useless-return: warn
no-void: error
no-self-compare: error
prefer-promise-reject-errors: error
strict:
- error
- global
no-label-var: error
no-lonely-if: off
no-new-object: error
arrow-body-style: off
no-invalid-this: error
prefer-arrow-callback: warn
prefer-numeric-literals: error
no-constructor-return: error
no-new-wrappers: error
no-return-await: warn
no-unused-expressions: warn

# code style
linebreak-style:
- error
- unix
semi:
- error
- always
curly:
- error
- multi-or-nest
- consistent
dot-location:
- error
- property
arrow-parens:
- error
- always
prefer-arrow-callback: warn
prefer-numeric-literals: error
block-spacing: error
computed-property-spacing: error
func-call-spacing: error
keyword-spacing:
- error
-
overrides:
catch:
before: true
after: false
space-before-function-paren:
- error
-
anonymous: never
named: never
asyncArrow: always
generator-star-spacing:
- error
-
anonymous:
before: false
after: false
named:
before: false
after: true
method:
before: false
after: false
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.DS_Store
node_modules/
coverage/
dist/
.nyc_output/
po/thingtalk.pot
po/*.mo
Expand Down
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
%.js : %.lr tools/generate-parser/*.js tools/generate-parser/grammar.js
node tools/generate-parser $@ $<
node -c $@
ts-node tools/generate-parser $@ $<

%.js : %.pegjs
pegjs -o $@ $<
Expand All @@ -15,7 +14,11 @@ all = \
lib/grammar.js \
test/test_sr_parser_generator.js

all: $(all)
dist: lib lib/* lib/ast/* lib/builtin/* lib/compiler/* lib/nn-syntax/* lib/runtime/* $(all)
tsc --build tsconfig.json
touch $@

all: dist

lib/grammar.js : lib/grammar.pegjs
pegjs --allowed-start-rules input,type_ref,permission_rule -o $@ $<
95 changes: 0 additions & 95 deletions index.js

This file was deleted.

44 changes: 23 additions & 21 deletions lib/ast/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
// limitations under the License.
//
// Author: Giovanni Campagna <gcampagn@cs.stanford.edu>
"use strict";

const assert = require('assert');
import assert from 'assert';

const Ast = require('.');
import { Selector } from './expression';
import { Action } from './primitive';
import { ClassDef } from './class_def';
import { Statement, Program, Library, Example } from './program';

const DeclarationProto = Ast.Statement.Declaration.prototype;
const ExampleProto = Ast.Example.prototype;
const DeclarationProto = Statement.Declaration.prototype;
const ExampleProto = Example.prototype;


// utilities
Expand All @@ -37,9 +39,9 @@ const ExampleProto = Ast.Example.prototype;
* @alias Ast.notifyAction
* @deprecated Use {@link Ast.Action.notifyAction} instead.
*/
module.exports.notifyAction = function notifyAction(what) {
return Ast.Action.notifyAction(what);
};
export function notifyAction(what) {
return Action.notifyAction(what);
}

/**
* Convert a manifest to a ThingTalk library.
Expand All @@ -50,10 +52,9 @@ module.exports.notifyAction = function notifyAction(what) {
* @deprecated Manifests are deprecated and should not be used. Use .tt files instead.
* @alias Ast.fromManifest
*/
function fromManifest(kind, manifest) {
return new Ast.Library(null, [Ast.ClassDef.fromManifest(kind, manifest)], []);
export function fromManifest(kind, manifest) {
return new Library(null, [ClassDef.fromManifest(kind, manifest)], []);
}
module.exports.fromManifest = fromManifest;

/**
* Convert a ThingTalk library to a manifest.
Expand All @@ -63,13 +64,14 @@ module.exports.fromManifest = fromManifest;
* @deprecated Manifests are deprecated and should not be used. Use .tt files instead.
* @alias Ast.toManifest
*/
function toManifest(meta) {
assert(meta instanceof Ast.Library);
export function toManifest(meta) {
assert(meta instanceof Library);
return meta.classes[0].toManifest();
}
module.exports.toManifest = toManifest;

function declarationLikeToProgram() {
/* eslint no-invalid-this: off */

const nametoslot = {};

let i = 0;
Expand All @@ -78,14 +80,14 @@ function declarationLikeToProgram() {

let program;
if (this.type === 'action') {
program = new Ast.Program(null, [], [],
[new Ast.Statement.Command(null, null, [this.value.clone()])], null);
program = new Program(null, [], [],
[new Statement.Command(null, null, [this.value.clone()])], null);
} else if (this.type === 'query') {
program = new Ast.Program(null, [], [],
[new Ast.Statement.Command(null, this.value.clone(), [Ast.Action.notifyAction()])], null);
program = new Program(null, [], [],
[new Statement.Command(null, this.value.clone(), [Action.notifyAction()])], null);
} else if (this.type === 'stream') {
program = new Ast.Program(null, [], [],
[new Ast.Statement.Rule(null, this.value.clone(), [Ast.Action.notifyAction()])], null);
program = new Program(null, [], [],
[new Statement.Rule(null, this.value.clone(), [Action.notifyAction()])], null);
} else {
program = this.value.clone();
}
Expand All @@ -100,7 +102,7 @@ function declarationLikeToProgram() {
}

for (let slot of program.iterateSlots2()) {
if (slot instanceof Ast.Selector)
if (slot instanceof Selector)
continue;
recursiveHandleSlot(slot.get());
}
Expand Down
9 changes: 4 additions & 5 deletions lib/ast/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
// limitations under the License.
//
// Author: Silei Xu <silei@cs.stanford.edu>
"use strict";

const assert = require('assert');
const NodeVisitor = require('./visitor');
import assert from 'assert';
import NodeVisitor from './visitor';

/**
* A single point in the source code input stream.
Expand Down Expand Up @@ -50,7 +49,7 @@ const NodeVisitor = require('./visitor');
* @alias Ast~Node
* @abstract
*/
module.exports = class Node {
export default class Node {
/**
* Construct a new AST node.
*
Expand Down Expand Up @@ -155,4 +154,4 @@ module.exports = class Node {
this.visit(visitor);
return buffer;
}
};
}
Loading

0 comments on commit d32290e

Please sign in to comment.