Skip to content

Commit cd23caf

Browse files
authored
Small improvements (#842)
* Small improvements * Replace `expectToBeDefined` with assertion signature * Refactor some tests * Fix type error * Don't elide empty `do` blocks * Fix semicolons test * Remove npm version bump scripts
1 parent 3ad73bb commit cd23caf

37 files changed

+295
-357
lines changed

.codecov.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
comment: off
1+
comment: false
22

33
coverage:
44
status:
@@ -7,5 +7,5 @@ coverage:
77
target: 90
88
threshold: null
99
base: auto
10-
changes: off
11-
patch: off
10+
changes: false
11+
patch: false

.gitignore

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
/node_modules
1+
node_modules
22
/dist
33
/coverage
44

55
yarn.lock
66
.vscode
77
.idea
8-
9-
# Release
10-
*.tgz
11-
12-
# OSX
138
.DS_Store
14-
*.lcov

.prettierrc.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

.prettierrc.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"printWidth": 120,
3+
"tabWidth": 4,
4+
"trailingComma": "es5",
5+
"endOfLine": "lf",
6+
"overrides": [{ "files": ["**/*.md", "**/*.yml"], "options": { "tabWidth": 2 } }]
7+
}

package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@
2626
"lint:prettier": "prettier --check \"**/*.{js,ts,yml,json,md}\" || (echo 'Run `npm run fix:prettier` to fix it.' && exit 1)",
2727
"lint:tslint": "tslint -p . && tslint -p test && tslint -p src/lualib",
2828
"fix:prettier": "prettier --check --write \"**/*.{js,ts,yml,json,md}\"",
29-
"release-major": "npm version major",
30-
"release-minor": "npm version minor",
31-
"release-patch": "npm version patch",
3229
"preversion": "npm run build && npm test",
3330
"postversion": "git push && git push --tags"
3431
},

src/LuaAST.ts

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// because we don't create the AST from text
66

77
import * as ts from "typescript";
8+
import { castArray } from "./utils";
89

910
export enum SyntaxKind {
1011
Block,
@@ -236,14 +237,8 @@ export function createVariableDeclarationStatement(
236237
tsOriginal?: ts.Node
237238
): VariableDeclarationStatement {
238239
const statement = createNode(SyntaxKind.VariableDeclarationStatement, tsOriginal) as VariableDeclarationStatement;
239-
statement.left = Array.isArray(left) ? left : [left];
240-
241-
if (Array.isArray(right)) {
242-
statement.right = right;
243-
} else if (right) {
244-
statement.right = [right];
245-
}
246-
240+
statement.left = castArray(left);
241+
if (right) statement.right = castArray(right);
247242
return statement;
248243
}
249244

@@ -264,14 +259,8 @@ export function createAssignmentStatement(
264259
tsOriginal?: ts.Node
265260
): AssignmentStatement {
266261
const statement = createNode(SyntaxKind.AssignmentStatement, tsOriginal) as AssignmentStatement;
267-
statement.left = Array.isArray(left) ? left : [left];
268-
269-
if (Array.isArray(right)) {
270-
statement.right = right;
271-
} else {
272-
statement.right = right ? [right] : [];
273-
}
274-
262+
statement.left = castArray(left);
263+
statement.right = right ? castArray(right) : [];
275264
return statement;
276265
}
277266

@@ -429,14 +418,14 @@ export function createLabelStatement(name: string, tsOriginal?: ts.Node): LabelS
429418

430419
export interface ReturnStatement extends Statement {
431420
kind: SyntaxKind.ReturnStatement;
432-
expressions?: Expression[];
421+
expressions: Expression[];
433422
}
434423

435424
export function isReturnStatement(node: Node): node is ReturnStatement {
436425
return node.kind === SyntaxKind.ReturnStatement;
437426
}
438427

439-
export function createReturnStatement(expressions?: Expression[], tsOriginal?: ts.Node): ReturnStatement {
428+
export function createReturnStatement(expressions: Expression[], tsOriginal?: ts.Node): ReturnStatement {
440429
const statement = createNode(SyntaxKind.ReturnStatement, tsOriginal) as ReturnStatement;
441430
statement.expressions = expressions;
442431
return statement;
@@ -496,11 +485,7 @@ export function isBooleanLiteral(node: Node): node is BooleanLiteral {
496485
}
497486

498487
export function createBooleanLiteral(value: boolean, tsOriginal?: ts.Node): BooleanLiteral {
499-
if (value) {
500-
return createNode(SyntaxKind.TrueKeyword, tsOriginal) as BooleanLiteral;
501-
} else {
502-
return createNode(SyntaxKind.FalseKeyword, tsOriginal) as BooleanLiteral;
503-
}
488+
return createNode(value ? SyntaxKind.TrueKeyword : SyntaxKind.FalseKeyword, tsOriginal) as BooleanLiteral;
504489
}
505490

506491
// TODO Call this DotsLiteral or DotsKeyword?
@@ -518,7 +503,7 @@ export function createDotsLiteral(tsOriginal?: ts.Node): DotsLiteral {
518503

519504
// StringLiteral / NumberLiteral
520505
// TODO TS uses the export interface "LiteralLikeNode" with a "text: string" member
521-
// but since we dont parse from text i think we can simplify by just having a value member
506+
// but since we don't parse from text I think we can simplify by just having a value member
522507

523508
// TODO NumericLiteral or NumberLiteral?
524509
export interface NumericLiteral extends Expression {
@@ -552,16 +537,15 @@ export function createStringLiteral(value: string, tsOriginal?: ts.Node): String
552537
}
553538

554539
export enum FunctionExpressionFlags {
555-
None = 0x0,
556-
Inline = 0x1, // Keep function on same line
557-
Declaration = 0x2, // Prefer declaration syntax `function foo()` over assignment syntax `foo = function()`
540+
None = 1 << 0,
541+
Inline = 1 << 1, // Keep function body on same line
542+
Declaration = 1 << 2, // Prefer declaration syntax `function foo()` over assignment syntax `foo = function()`
558543
}
559544

560545
export interface FunctionExpression extends Expression {
561546
kind: SyntaxKind.FunctionExpression;
562547
params?: Identifier[];
563548
dots?: DotsLiteral;
564-
restParamName?: Identifier;
565549
body: Block;
566550
flags: FunctionExpressionFlags;
567551
}
@@ -574,15 +558,13 @@ export function createFunctionExpression(
574558
body: Block,
575559
params?: Identifier[],
576560
dots?: DotsLiteral,
577-
restParamName?: Identifier,
578561
flags = FunctionExpressionFlags.None,
579562
tsOriginal?: ts.Node
580563
): FunctionExpression {
581564
const expression = createNode(SyntaxKind.FunctionExpression, tsOriginal) as FunctionExpression;
582565
expression.body = body;
583566
expression.params = params;
584567
expression.dots = dots;
585-
expression.restParamName = restParamName;
586568
expression.flags = flags;
587569
return expression;
588570
}
@@ -671,7 +653,7 @@ export function createBinaryExpression(
671653
export interface CallExpression extends Expression {
672654
kind: SyntaxKind.CallExpression;
673655
expression: Expression;
674-
params?: Expression[];
656+
params: Expression[];
675657
}
676658

677659
export function isCallExpression(node: Node): node is CallExpression {
@@ -680,7 +662,7 @@ export function isCallExpression(node: Node): node is CallExpression {
680662

681663
export function createCallExpression(
682664
expression: Expression,
683-
params?: Expression[],
665+
params: Expression[],
684666
tsOriginal?: ts.Node
685667
): CallExpression {
686668
const callExpression = createNode(SyntaxKind.CallExpression, tsOriginal) as CallExpression;
@@ -693,7 +675,7 @@ export interface MethodCallExpression extends Expression {
693675
kind: SyntaxKind.MethodCallExpression;
694676
prefixExpression: Expression;
695677
name: Identifier;
696-
params?: Expression[];
678+
params: Expression[];
697679
}
698680

699681
export function isMethodCallExpression(node: Node): node is MethodCallExpression {
@@ -703,7 +685,7 @@ export function isMethodCallExpression(node: Node): node is MethodCallExpression
703685
export function createMethodCallExpression(
704686
prefixExpression: Expression,
705687
name: Identifier,
706-
params?: Expression[],
688+
params: Expression[],
707689
tsOriginal?: ts.Node
708690
): MethodCallExpression {
709691
const callExpression = createNode(SyntaxKind.MethodCallExpression, tsOriginal) as MethodCallExpression;

0 commit comments

Comments
 (0)