Skip to content

Commit

Permalink
Remove ts-simple-type (#1103)
Browse files Browse the repository at this point in the history
* Remove ts-simple-type

* Fix isPossiblyType case for any, unknown, and defined types.

* Fix isPossiblyType on type variables

* Fix whitespace

* Fix bad bitwise test in isPossiblyFalse and isPossiblyUndefined

* Move isPossiblyEmptyString, isPossiblyNaN, isPossiblyZero to types.ts

* Remove canTypeBeLuaFalsy
  • Loading branch information
osyrisrblx committed Sep 27, 2020
1 parent a882e99 commit b4618a1
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 109 deletions.
7 changes: 1 addition & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
"fs-extra": "^8.1.0",
"kleur": "^3.0.3",
"prompts": "^2.3.2",
"ts-simple-type": "^0.3.7",
"typescript": "^3.9.5",
"yargs": "^15.3.1"
},
Expand Down
4 changes: 2 additions & 2 deletions src/TSTransformer/classes/RoactSymbolManager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ts from "byots";
import { assert } from "Shared/util/assert";
import { isSomeType } from "TSTransformer/util/types";
import { isType } from "TSTransformer/util/types";

export const ROACT_SYMBOL_NAMES = {
Component: "Component",
Expand Down Expand Up @@ -93,6 +93,6 @@ export class RoactSymbolManager {

public isElementType(type: ts.Type) {
const symbol = this.getSymbolOrThrow(ROACT_SYMBOL_NAMES.Element);
return isSomeType(type, t => t.symbol === symbol);
return isType(type, t => t.symbol === symbol);
}
}
21 changes: 0 additions & 21 deletions src/TSTransformer/classes/TransformState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import { PARENT_FIELD, ProjectType } from "Shared/constants";
import { diagnostics } from "Shared/diagnostics";
import { assert } from "Shared/util/assert";
import { getOrSetDefault } from "Shared/util/getOrSetDefault";
import * as tsst from "ts-simple-type";
import { MultiTransformState } from "TSTransformer";
import { createGetService } from "TSTransformer/util/createGetService";
import { propertyAccessExpressionChain } from "TSTransformer/util/expressionChain";
import { getModuleAncestor, skipUpwards } from "TSTransformer/util/traversal";
import originalTS from "typescript";

/**
* The ID of the Runtime library.
Expand Down Expand Up @@ -150,25 +148,6 @@ export class TransformState {
);
}

/**
* Converts a TypeScript type into a "SimpleType"
* @param type The type to convert.
*/
public getSimpleType(type: ts.Type) {
return tsst.toSimpleType(
(type as unknown) as originalTS.Type,
(this.typeChecker as unknown) as originalTS.TypeChecker,
);
}

/**
* Converts the TypeScript type of `node` into a "SimpleType"
* @param node The node with the type to convert.
*/
public getSimpleTypeFromNode(node: ts.Node) {
return this.getSimpleType(this.getType(node));
}

/**
* Returns the prerequisite statements created by `callback`.
*/
Expand Down
11 changes: 3 additions & 8 deletions src/TSTransformer/nodes/expressions/transformBinaryExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { createBinaryFromOperator } from "TSTransformer/util/createBinaryFromOpe
import { ensureTransformOrder } from "TSTransformer/util/ensureTransformOrder";
import { isUsedAsStatement } from "TSTransformer/util/isUsedAsStatement";
import { skipDownwards } from "TSTransformer/util/traversal";
import { isLuaTupleType, isNumberType, isStringSimpleType, isStringType } from "TSTransformer/util/types";
import { isLuaTupleType, isNumberType, isStringType } from "TSTransformer/util/types";
import { validateNotAnyType } from "TSTransformer/util/validateNotAny";
import { wrapToString } from "TSTransformer/util/wrapToString";

Expand Down Expand Up @@ -236,12 +236,7 @@ export function transformBinaryExpression(state: TransformState, node: ts.Binary

const writableType = state.getType(node.left);
const valueType = state.getType(node.right);
const rightSimpleType = state.getSimpleType(valueType);
const operator = getSimpleAssignmentOperator(
state.getSimpleType(writableType),
operatorKind as ts.AssignmentOperator,
rightSimpleType,
);
const operator = getSimpleAssignmentOperator(writableType, operatorKind as ts.AssignmentOperator, valueType);
const { writable, readable, value } = transformWritableAssignment(
state,
node.left,
Expand All @@ -254,7 +249,7 @@ export function transformBinaryExpression(state: TransformState, node: ts.Binary
state,
writable,
operator,
operator === "..=" && !isStringSimpleType(rightSimpleType) ? wrapToString(value) : value,
operator === "..=" && !isStringType(valueType) ? wrapToString(value) : value,
);
} else {
return createCompoundAssignmentExpression(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import luau from "LuauAST";
import { TransformState } from "TSTransformer";
import { transformExpression } from "TSTransformer/nodes/expressions/transformExpression";
import { createTruthinessChecks } from "TSTransformer/util/createTruthinessChecks";
import { canTypeBeLuaFalsy } from "TSTransformer/util/types";
import { isPossiblyFalse, isPossiblyUndefined } from "TSTransformer/util/types";

export function transformConditionalExpression(state: TransformState, node: ts.ConditionalExpression) {
const condition = transformExpression(state, node.condition);
const [whenTrue, whenTruePrereqs] = state.capture(() => transformExpression(state, node.whenTrue));
const [whenFalse, whenFalsePrereqs] = state.capture(() => transformExpression(state, node.whenFalse));
const type = state.getType(node.whenTrue);
if (
!canTypeBeLuaFalsy(state, state.getType(node.whenTrue)) &&
!isPossiblyFalse(state, type) &&
!isPossiblyUndefined(type) &&
luau.list.isEmpty(whenTruePrereqs) &&
luau.list.isEmpty(whenFalsePrereqs)
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { transformExpression } from "TSTransformer/nodes/expressions/transformEx
import { transformMethodDeclaration } from "TSTransformer/nodes/transformMethodDeclaration";
import { transformObjectKey } from "TSTransformer/nodes/transformObjectKey";
import { assignToMapPointer, disableMapInline, MapPointer } from "TSTransformer/util/pointer";
import { canBeUndefined } from "TSTransformer/util/types";
import { isPossiblyUndefined } from "TSTransformer/util/types";

function transformPropertyAssignment(
state: TransformState,
Expand All @@ -33,7 +33,7 @@ function transformSpreadAssignment(state: TransformState, ptr: MapPointer, prope
disableMapInline(state, ptr);
let spreadExp = transformExpression(state, property.expression);

const possiblyUndefined = canBeUndefined(state, state.getType(property.expression));
const possiblyUndefined = isPossiblyUndefined(state.getType(property.expression));
if (possiblyUndefined) {
spreadExp = state.pushToVarIfComplex(spreadExp);
}
Expand Down
4 changes: 2 additions & 2 deletions src/TSTransformer/nodes/jsx/transformJsxAttributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from "TSTransformer/util/jsx/constants";
import { createRoactIndex } from "TSTransformer/util/jsx/createRoactIndex";
import { assignToMapPointer, disableMapInline, MapPointer } from "TSTransformer/util/pointer";
import { canBeUndefined } from "TSTransformer/util/types";
import { isPossiblyUndefined } from "TSTransformer/util/types";

function transformJsxInitializer(
state: TransformState,
Expand All @@ -33,7 +33,7 @@ function createJsxAttributeLoop(
expression: luau.Expression,
type: ts.Type,
) {
const possiblyUndefined = canBeUndefined(state, type);
const possiblyUndefined = isPossiblyUndefined(type);
if (possiblyUndefined) {
expression = state.pushToVarIfComplex(expression);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { transformWritableAssignment, transformWritableExpression } from "TSTran
import { isUnaryAssignmentOperator } from "TSTransformer/typeGuards";
import { createCompoundAssignmentStatement, getSimpleAssignmentOperator } from "TSTransformer/util/assignment";
import { skipDownwards } from "TSTransformer/util/traversal";
import { isStringSimpleType } from "TSTransformer/util/types";
import { isStringType } from "TSTransformer/util/types";
import { wrapToString } from "TSTransformer/util/wrapToString";

function transformUnaryExpressionStatement(
Expand All @@ -32,11 +32,10 @@ export function transformExpressionStatementInner(state: TransformState, express
) {
const writableType = state.getType(expression.left);
const valueType = state.getType(expression.right);
const rightSimpleType = state.getSimpleType(valueType);
const operator = getSimpleAssignmentOperator(
state.getSimpleType(writableType),
writableType,
operatorKind as ts.AssignmentOperator,
rightSimpleType,
valueType,
);
const { writable, readable, value } = transformWritableAssignment(
state,
Expand All @@ -50,7 +49,7 @@ export function transformExpressionStatementInner(state: TransformState, express
luau.create(luau.SyntaxKind.Assignment, {
left: writable,
operator,
right: operator === "..=" && !isStringSimpleType(rightSimpleType) ? wrapToString(value) : value,
right: operator === "..=" && !isStringType(valueType) ? wrapToString(value) : value,
}),
);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/TSTransformer/nodes/transformLogical.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function getLogicalChain(
const [expression, prereqs] = state.capture(() => transformExpression(state, original));
let inline = false;
if (enableInlining) {
const willWrap = index < array.length - 1 && willCreateTruthinessChecks(state, type);
const willWrap = index < array.length - 1 && willCreateTruthinessChecks(type);
inline = luau.list.isEmpty(prereqs) && !willWrap;
}
return { type, expression, statements: prereqs, inline };
Expand Down
9 changes: 4 additions & 5 deletions src/TSTransformer/util/assignment.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import ts from "byots";
import luau from "LuauAST";
import * as tsst from "ts-simple-type";
import { TransformState } from "TSTransformer";
import { createBinaryFromOperator } from "TSTransformer/util/createBinaryFromOperator";
import { isStringSimpleType } from "TSTransformer/util/types";
import { isStringType } from "TSTransformer/util/types";

const COMPOUND_OPERATOR_MAP = new Map<ts.SyntaxKind, luau.AssignmentOperator>([
// compound assignment
Expand All @@ -22,13 +21,13 @@ const COMPOUND_OPERATOR_MAP = new Map<ts.SyntaxKind, luau.AssignmentOperator>([
]);

export function getSimpleAssignmentOperator(
leftType: tsst.SimpleType,
leftType: ts.Type,
operatorKind: ts.AssignmentOperator,
rightType: tsst.SimpleType,
rightType: ts.Type,
) {
// plus
if (operatorKind === ts.SyntaxKind.PlusEqualsToken) {
return isStringSimpleType(leftType) || isStringSimpleType(rightType) ? "..=" : "+=";
return isStringType(leftType) || isStringType(rightType) ? "..=" : "+=";
}

return COMPOUND_OPERATOR_MAP.get(operatorKind);
Expand Down
23 changes: 9 additions & 14 deletions src/TSTransformer/util/createTruthinessChecks.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
import ts from "byots";
import luau from "LuauAST";
import * as tsst from "ts-simple-type";
import { TransformState } from "TSTransformer";
import { binaryExpressionChain } from "TSTransformer/util/expressionChain";
import { isPossiblyEmptyString, isPossiblyNaN, isPossiblyZero } from "TSTransformer/util/types";

export function willCreateTruthinessChecks(state: TransformState, nodeType: ts.Type) {
const simpleType = state.getSimpleType(nodeType);
const isAssignableToZero = tsst.isAssignableToValue(simpleType, 0);
const isAssignableToNaN = tsst.isAssignableToValue(simpleType, NaN);
const isAssignableToEmptyString = tsst.isAssignableToValue(simpleType, "");
return isAssignableToZero || isAssignableToNaN || isAssignableToEmptyString;
export function willCreateTruthinessChecks(type: ts.Type) {
return isPossiblyZero(type) || isPossiblyNaN(type) || isPossiblyEmptyString(type);
}

export function createTruthinessChecks(state: TransformState, exp: luau.Expression, nodeType: ts.Type) {
const checks = new Array<luau.Expression>();

const simpleType = state.getSimpleType(nodeType);
const isAssignableToZero = tsst.isAssignableToValue(simpleType, 0);
const isAssignableToNaN = tsst.isAssignableToValue(simpleType, NaN);
const isAssignableToEmptyString = tsst.isAssignableToValue(simpleType, "");
export function createTruthinessChecks(state: TransformState, exp: luau.Expression, type: ts.Type) {
const isAssignableToZero = isPossiblyZero(type);
const isAssignableToNaN = isPossiblyNaN(type);
const isAssignableToEmptyString = isPossiblyEmptyString(type);

if (isAssignableToZero || isAssignableToNaN || isAssignableToEmptyString) {
exp = state.pushToVarIfComplex(exp);
}

const checks = new Array<luau.Expression>();

if (isAssignableToZero) {
checks.push(luau.binary(exp, "~=", luau.number(0)));
}
Expand Down

0 comments on commit b4618a1

Please sign in to comment.