Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 31 additions & 20 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ module.exports = {
"no-useless-return": ["error"],

"import/no-default-export": "error",
// TODO currently only works for direct imports (useless for now) https://github.com/benmosher/eslint-plugin-import/issues/1729
// "import/no-deprecated": "error",

"jest/expect-expect": "off",
"jest/consistent-test-it": ["error", { fn: "test", withinDescribe: "test" }],
Expand All @@ -154,31 +156,33 @@ module.exports = {
"@typescript-eslint/no-require-imports": "off",
"@typescript-eslint/no-unnecessary-condition": "off",
"@typescript-eslint/prefer-for-of": "error",
// TODO: https://github.com/typescript-eslint/typescript-eslint/issues/1265
// "@typescript-eslint/prefer-nullish-coalescing": "error",
"@typescript-eslint/prefer-nullish-coalescing": "error",
"@typescript-eslint/prefer-readonly": "off",
"@typescript-eslint/quotes": ["error", "double", { avoidEscape: true, allowTemplateLiterals: false }],
"@typescript-eslint/require-array-sort-compare": "off",
"@typescript-eslint/camelcase": "off",

// TODO: https://github.com/typescript-eslint/typescript-eslint/issues/1712
// "@typescript-eslint/naming-convention": [
// "error",
// {
// selector: "default",
// format: ["camelCase"],
// leadingUnderscore: "allow",
// },
// {
// selector: "variable",
// format: ["camelCase", "UPPER_CASE"],
// leadingUnderscore: "allow",
// },
// {
// selector: "typeLike",
// format: ["PascalCase"],
// },
// ],
"@typescript-eslint/naming-convention": [
"error",
{
selector: "default",
format: ["camelCase"],
leadingUnderscore: "allow",
},
{
selector: "variable",
format: ["camelCase", "UPPER_CASE"],
leadingUnderscore: "allow",
},
{
selector: "typeLike",
format: ["PascalCase"],
},
{
selector: "enumMember",
format: ["PascalCase"],
},
],
},
},
{
Expand All @@ -187,6 +191,13 @@ module.exports = {
"no-restricted-syntax": ["error", "LabeledStatement", "SequenceExpression"],
"@typescript-eslint/no-throw-literal": "off",
"@typescript-eslint/prefer-optional-chain": "off",
"@typescript-eslint/naming-convention": "off",
},
},
{
files: "language-extensions/index.d.ts",
rules: {
"@typescript-eslint/naming-convention": "off",
},
},
{
Expand Down
2 changes: 2 additions & 0 deletions src/LuaLib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export enum LuaLibFeature {
Unpack = "Unpack",
}

/* eslint-disable @typescript-eslint/naming-convention */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it complaining here, because of the key names?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes PascalCase key names are not allowed. I didn't change them to camelCase because I wasn't sure what the implications of that would be.

const luaLibDependencies: Partial<Record<LuaLibFeature, LuaLibFeature[]>> = {
ArrayConcat: [LuaLibFeature.ArrayIsArray],
ArrayFlat: [LuaLibFeature.ArrayConcat, LuaLibFeature.ArrayIsArray],
Expand All @@ -108,6 +109,7 @@ const luaLibDependencies: Partial<Record<LuaLibFeature, LuaLibFeature[]>> = {
StringSplit: [LuaLibFeature.StringSubstring],
SymbolRegistry: [LuaLibFeature.Symbol],
};
/* eslint-enable @typescript-eslint/naming-convention */

export function loadLuaLibFeatures(features: Iterable<LuaLibFeature>, emitHost: EmitHost): string {
let result = "";
Expand Down
2 changes: 1 addition & 1 deletion src/LuaPrinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ export class LuaPrinter {
}
}
});
return result || false;
return result ?? false;
}

protected printStatementArray(statements: lua.Statement[]): SourceChunk[] {
Expand Down
8 changes: 4 additions & 4 deletions src/transformation/visitors/binary-expression/compound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ export function parseAccessExpressionWithEvaluationEffects(
const type = context.checker.getTypeAtLocation(node.expression);
if (isArrayType(context, type)) {
// Offset arrays by one
const oneLit = ts.createNumericLiteral("1");
const exp = ts.createParen(node.argumentExpression);
const addExp = ts.createBinary(exp, ts.SyntaxKind.PlusToken, oneLit);
const oneLit = ts.factory.createNumericLiteral("1");
const exp = ts.factory.createParenthesizedExpression(node.argumentExpression);
const addExp = ts.factory.createBinaryExpression(exp, ts.SyntaxKind.PlusToken, oneLit);
return [node.expression, addExp];
} else {
return [node.expression, node.argumentExpression];
}
} else if (ts.isPropertyAccessExpression(node) && isExpressionWithEvaluationEffect(node.expression)) {
return [node.expression, ts.createStringLiteral(node.name.text)];
return [node.expression, ts.factory.createStringLiteral(node.name.text)];
}

return [];
Expand Down
6 changes: 3 additions & 3 deletions src/transformation/visitors/binary-expression/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export const transformBinaryExpression: FunctionVisitor<ts.BinaryExpression> = (
return transformToImmediatelyInvokedFunctionExpression(
context,
() => ({
statements: context.transformStatements(ts.createExpressionStatement(node.left)),
statements: context.transformStatements(ts.factory.createExpressionStatement(node.left)),
result: context.transformExpression(node.right),
}),
node
Expand Down Expand Up @@ -159,8 +159,8 @@ export function transformBinaryExpressionStatement(
return transformAssignmentStatement(context, expression as ts.AssignmentExpression<ts.EqualsToken>);
} else if (operator === ts.SyntaxKind.CommaToken) {
const statements = [
...context.transformStatements(ts.createExpressionStatement(expression.left)),
...context.transformStatements(ts.createExpressionStatement(expression.right)),
...context.transformStatements(ts.factory.createExpressionStatement(expression.left)),
...context.transformStatements(ts.factory.createExpressionStatement(expression.right)),
];

return lua.createDoStatement(statements, expression);
Expand Down
8 changes: 4 additions & 4 deletions src/transformation/visitors/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ function transformPropertyCall(context: TransformationContext, node: PropertyCal

if (node.expression.expression.kind === ts.SyntaxKind.SuperKeyword) {
// Super calls take the format of super.call(self,...)
const parameters = transformArguments(context, node.arguments, signature, ts.createThis());
const parameters = transformArguments(context, node.arguments, signature, ts.factory.createThis());
return lua.createCallExpression(context.transformExpression(node.expression), parameters);
}

Expand Down Expand Up @@ -267,11 +267,11 @@ export const transformCallExpression: FunctionVisitor<ts.CallExpression> = (node

// Handle super calls properly
if (node.expression.kind === ts.SyntaxKind.SuperKeyword) {
const parameters = transformArguments(context, node.arguments, signature, ts.createThis());
const parameters = transformArguments(context, node.arguments, signature, ts.factory.createThis());

return lua.createCallExpression(
lua.createTableIndexExpression(
context.transformExpression(ts.createSuper()),
context.transformExpression(ts.factory.createSuper()),
lua.createStringLiteral("____constructor")
),
parameters
Expand All @@ -285,7 +285,7 @@ export const transformCallExpression: FunctionVisitor<ts.CallExpression> = (node
if (signatureDeclaration && getDeclarationContextType(context, signatureDeclaration) === ContextType.Void) {
parameters = transformArguments(context, node.arguments, signature);
} else {
const callContext = context.isStrict ? ts.createNull() : ts.createIdentifier("_G");
const callContext = context.isStrict ? ts.factory.createNull() : ts.factory.createIdentifier("_G");
parameters = transformArguments(context, node.arguments, signature, callContext);
}

Expand Down
4 changes: 2 additions & 2 deletions src/transformation/visitors/class/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ function transformClassLikeDeclaration(
// Generate a constructor if none was defined in a base class
const constructorResult = transformConstructorDeclaration(
context,
ts.createConstructor([], [], [], ts.createBlock([], true)),
ts.factory.createConstructorDeclaration([], [], [], ts.factory.createBlock([], true)),
localClassName,
instanceFields,
classDeclaration
Expand All @@ -168,7 +168,7 @@ function transformClassLikeDeclaration(
const superCall = lua.createExpressionStatement(
lua.createCallExpression(
lua.createTableIndexExpression(
context.transformExpression(ts.createSuper()),
context.transformExpression(ts.factory.createSuper()),
lua.createStringLiteral("____constructor")
),
[createSelfIdentifier(), lua.createDotsLiteral()]
Expand Down
4 changes: 2 additions & 2 deletions src/transformation/visitors/literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function createShorthandIdentifier(

const name = isUnsafeName ? createSafeName(propertyName) : propertyName;

let identifier = context.transformExpression(ts.createIdentifier(name));
let identifier = context.transformExpression(ts.factory.createIdentifier(name));
lua.setNodeOriginal(identifier, propertyIdentifier);
if (valueSymbol !== undefined && lua.isIdentifier(identifier)) {
identifier.symbolId = getSymbolIdOfSymbol(context, valueSymbol);
Expand Down Expand Up @@ -137,7 +137,7 @@ const transformObjectLiteralExpression: FunctionVisitor<ts.ObjectLiteralExpressi

const transformArrayLiteralExpression: FunctionVisitor<ts.ArrayLiteralExpression> = (expression, context) => {
const filteredElements = expression.elements.map(e =>
ts.isOmittedExpression(e) ? ts.createIdentifier("undefined") : e
ts.isOmittedExpression(e) ? ts.factory.createIdentifier("undefined") : e
);
const values = flattenSpreadExpressions(context, filteredElements).map(e => lua.createTableFieldExpression(e));

Expand Down
4 changes: 2 additions & 2 deletions src/transformation/visitors/loops/for.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const transformForStatement: FunctionVisitor<ts.ForStatement> = (statemen
// local initializer = value
result.push(...statement.initializer.declarations.flatMap(d => transformVariableDeclaration(context, d)));
} else {
result.push(...context.transformStatements(ts.createExpressionStatement(statement.initializer)));
result.push(...context.transformStatements(ts.factory.createExpressionStatement(statement.initializer)));
}
}

Expand All @@ -25,7 +25,7 @@ export const transformForStatement: FunctionVisitor<ts.ForStatement> = (statemen
const body: lua.Statement[] = transformLoopBody(context, statement);

if (statement.incrementor) {
body.push(...context.transformStatements(ts.createExpressionStatement(statement.incrementor)));
body.push(...context.transformStatements(ts.factory.createExpressionStatement(statement.incrementor)));
}

// while (condition) do ... end
Expand Down
2 changes: 1 addition & 1 deletion src/transformation/visitors/loops/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function getVariableDeclarationBinding(
checkVariableDeclarationList(context, node);

if (node.declarations.length === 0) {
return ts.createIdentifier("____");
return ts.factory.createIdentifier("____");
}

return node.declarations[0].name;
Expand Down
11 changes: 7 additions & 4 deletions src/transformation/visitors/modules/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,20 +129,23 @@ function transformExportSpecifiersFrom(
exportSpecifiers: ts.ExportSpecifier[]
): lua.Statement {
// First transpile as import clause
const importClause = ts.createImportClause(
const importClause = ts.factory.createImportClause(
false,
undefined,
ts.createNamedImports(exportSpecifiers.map(s => ts.createImportSpecifier(s.propertyName, s.name)))
ts.factory.createNamedImports(
exportSpecifiers.map(s => ts.factory.createImportSpecifier(s.propertyName, s.name))
)
);

const importDeclaration = ts.createImportDeclaration(
const importDeclaration = ts.factory.createImportDeclaration(
statement.decorators,
statement.modifiers,
importClause,
moduleSpecifier
);

// Wrap in block to prevent imports from hoisting out of `do` statement
const [block] = transformScopeBlock(context, ts.createBlock([importDeclaration]), ScopeType.Block);
const [block] = transformScopeBlock(context, ts.factory.createBlock([importDeclaration]), ScopeType.Block);
const result = block.statements;

// Now the module is imported, add the imports to the export table
Expand Down
12 changes: 6 additions & 6 deletions src/transformation/visitors/unary-expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function transformUnaryExpressionStatement(
context,
expression,
expression.operand,
ts.createLiteral(1),
ts.factory.createNumericLiteral(1),
replacementOperator
);
} else if (ts.isPostfixUnaryExpression(expression)) {
Expand All @@ -37,7 +37,7 @@ export function transformUnaryExpressionStatement(
context,
expression,
expression.operand,
ts.createLiteral(1),
ts.factory.createNumericLiteral(1),
replacementOperator
);
}
Expand All @@ -50,7 +50,7 @@ export const transformPostfixUnaryExpression: FunctionVisitor<ts.PostfixUnaryExp
context,
expression,
expression.operand,
ts.createLiteral(1),
ts.factory.createNumericLiteral(1),
ts.SyntaxKind.PlusToken,
true
);
Expand All @@ -60,7 +60,7 @@ export const transformPostfixUnaryExpression: FunctionVisitor<ts.PostfixUnaryExp
context,
expression,
expression.operand,
ts.createLiteral(1),
ts.factory.createNumericLiteral(1),
ts.SyntaxKind.MinusToken,
true
);
Expand All @@ -77,7 +77,7 @@ export const transformPrefixUnaryExpression: FunctionVisitor<ts.PrefixUnaryExpre
context,
expression,
expression.operand,
ts.createLiteral(1),
ts.factory.createNumericLiteral(1),
ts.SyntaxKind.PlusToken,
false
);
Expand All @@ -87,7 +87,7 @@ export const transformPrefixUnaryExpression: FunctionVisitor<ts.PrefixUnaryExpre
context,
expression,
expression.operand,
ts.createLiteral(1),
ts.factory.createNumericLiteral(1),
ts.SyntaxKind.MinusToken,
false
);
Expand Down
2 changes: 1 addition & 1 deletion src/transformation/visitors/variable-declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function transformBindingPattern(
// nested binding pattern
const propertyName = ts.isObjectBindingPattern(pattern)
? element.propertyName
: ts.createNumericLiteral(String(index + 1));
: ts.factory.createNumericLiteral(String(index + 1));

if (propertyName !== undefined) {
propertyAccessStack.push(propertyName);
Expand Down
6 changes: 3 additions & 3 deletions src/transpilation/transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ export function getTransformers(

export const noImplicitSelfTransformer: ts.TransformerFactory<ts.SourceFile | ts.Bundle> = () => node => {
const transformSourceFile: ts.Transformer<ts.SourceFile> = node => {
const empty = ts.createNotEmittedStatement(undefined!);
const empty = ts.factory.createNotEmittedStatement(undefined!);
ts.addSyntheticLeadingComment(empty, ts.SyntaxKind.MultiLineCommentTrivia, "* @noSelfInFile ", true);
return ts.updateSourceFileNode(node, [empty, ...node.statements], node.isDeclarationFile);
return ts.factory.updateSourceFile(node, [empty, ...node.statements], node.isDeclarationFile);
};

return ts.isBundle(node)
? ts.updateBundle(node, node.sourceFiles.map(transformSourceFile))
? ts.factory.updateBundle(node, node.sourceFiles.map(transformSourceFile))
: transformSourceFile(node);
};

Expand Down
1 change: 1 addition & 0 deletions test/cli/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ describe("tsconfig", () => {
});

test("should parse options case-sensitively", () => {
// eslint-disable-next-line @typescript-eslint/naming-convention
const result = parseConfigFileContent({ tstl: { NoHeader: true } });

expect(result.errors).toHaveDiagnostics();
Expand Down
8 changes: 4 additions & 4 deletions test/transpile/transformers/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const program = (program: ts.Program, options: { value: any }): ts.Transf
export const config = ({ value }: { value: any }): ts.TransformerFactory<ts.SourceFile> => context => file =>
visitAndReplace(context, file, node => {
if (!ts.isReturnStatement(node)) return;
return ts.updateReturn(node, ts.createLiteral(value));
return ts.factory.updateReturnStatement(node, value ? ts.factory.createTrue() : ts.factory.createFalse());
});

export const checker = (
Expand All @@ -20,13 +20,13 @@ export const checker = (
if (!ts.isReturnStatement(node) || !node.expression) return;
const type = checker.getTypeAtLocation(node.expression);
if ((type.flags & ts.TypeFlags.BooleanLiteral) === 0) return;
return ts.updateReturn(node, ts.createLiteral(value));
return ts.factory.updateReturnStatement(node, value ? ts.factory.createTrue() : ts.factory.createFalse());
});

export const raw: ts.TransformerFactory<ts.SourceFile> = context => file =>
visitAndReplace(context, file, node => {
if (!ts.isReturnStatement(node)) return;
return ts.updateReturn(node, ts.createLiteral(true));
return ts.factory.updateReturnStatement(node, ts.factory.createTrue());
});

export const compilerOptions = (
Expand All @@ -35,6 +35,6 @@ export const compilerOptions = (
assert(options.plugins?.length === 1);
return visitAndReplace(context, file, node => {
if (!ts.isReturnStatement(node)) return;
return ts.updateReturn(node, ts.createLiteral(true));
return ts.factory.updateReturnStatement(node, ts.factory.createTrue());
});
};
1 change: 1 addition & 0 deletions test/transpile/transformers/transformers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ test("transformer resolution error", () => {
.expectToHaveDiagnostics();
});

// This tests plugin transformers by transforming the `return false` statement to a `return true` statement.
describe("factory types", () => {
test.each(["program", "config", "checker", "raw", "compilerOptions"] as const)("%s", type => {
util.testFunction`
Expand Down
Loading