Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
});
if (typeof options.filePath === 'string') {
const tsx = options.filePath.endsWith('.tsx');
if (tsx || options.filePath.endsWith('.ts')) {
parserOptions.jsx = tsx;
}
}
/**
* Allow the user to suppress the warning from typescript-estree if they are using an unsupported
* version of TypeScript
*/
const warnOnUnsupportedTypeScriptVersion = validateBoolean(options.warnOnUnsupportedTypeScriptVersion, true);
if (!warnOnUnsupportedTypeScriptVersion) {
parserOptions.loggerFn = false;
}
const { ast, services } = typescript_estree_1.parseAndGenerateServices(code, parserOptions);
ast.sourceType = options.sourceType;
simple_traverse_1.simpleTraverse(ast, {
enter(node) {
switch (node.type) {
// Function#body cannot be null in ESTree spec.
case 'FunctionExpression':
if (!node.body) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
node.type = `TSEmptyBody${node.type}`;
}
break;
// no default
}
},
});
const scopeManager = analyze_scope_1.analyzeScope(ast, options);
}
}
/**
* Allow the user to suppress the warning from typescript-estree if they are using an unsupported
* version of TypeScript
*/
const warnOnUnsupportedTypeScriptVersion = validateBoolean(
options.warnOnUnsupportedTypeScriptVersion,
true,
);
if (!warnOnUnsupportedTypeScriptVersion) {
parserOptions.loggerFn = false;
}
const { ast, services } = parseAndGenerateServices(code, parserOptions);
ast.sourceType = options.sourceType;
simpleTraverse(ast, {
enter(node) {
switch (node.type) {
// Function#body cannot be null in ESTree spec.
case 'FunctionExpression':
if (!node.body) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
node.type = `TSEmptyBody${node.type}` as any;
}
break;
// no default
}
},
});
function LuaMaker() {
this.noBraceTypes = [typescript_estree_1.AST_NODE_TYPES.MemberExpression, typescript_estree_1.AST_NODE_TYPES.ThisExpression, typescript_estree_1.AST_NODE_TYPES.Identifier, typescript_estree_1.AST_NODE_TYPES.CallExpression, typescript_estree_1.AST_NODE_TYPES.TSAsExpression, typescript_estree_1.AST_NODE_TYPES.TSTypeAssertion, typescript_estree_1.AST_NODE_TYPES.Super];
// TODO: Typeof's return value may be different between ts and lua
this.tsType2luaType = {
'undefined': 'nil',
'object': 'table'
};
this.ignoreExpressionType = [typescript_estree_1.AST_NODE_TYPES.MemberExpression, typescript_estree_1.AST_NODE_TYPES.Identifier];
this.luaKeyWords = ['and', 'break', 'do', 'else', 'elseif', 'end', 'false', 'for', 'function', 'if', 'in', 'local', 'nil', 'not', 'or', 'repeat', 'return', 'then', 'true', 'until', 'while'];
this.pv = 0;
this.operatorPriorityMap = {};
this.isDevMode = false;
this.funcReplConf = {};
this.regexReplConf = {};
this.usedIdMapByClass = {};
this.importAsts = [];
this.imports = [];
this.importMapByClass = {};
const analyze_scope_1 = require("./analyze-scope");
const simple_traverse_1 = require("./simple-traverse");
const visitor_keys_1 = require("./visitor-keys");
// note - cannot migrate this to an import statement because it will make TSC copy the package.json to the dist folder
const packageJSON = require('../package.json');
function validateBoolean(value, fallback = false) {
if (typeof value !== 'boolean') {
return fallback;
}
return value;
}
//------------------------------------------------------------------------------
// Public
//------------------------------------------------------------------------------
exports.version = packageJSON.version;
exports.Syntax = Object.freeze(typescript_estree_1.AST_NODE_TYPES);
function parse(code, options) {
return parseForESLint(code, options).ast;
}
exports.parse = parse;
function parseForESLint(code, options) {
if (!options || typeof options !== 'object') {
options = {};
}
// https://eslint.org/docs/user-guide/configuring#specifying-parser-options
// if sourceType is not provided by default eslint expect that it will be set to "script"
if (options.sourceType !== 'module' && options.sourceType !== 'script') {
options.sourceType = 'script';
}
if (typeof options.ecmaFeatures !== 'object') {
options.ecmaFeatures = {};
}
ast.program.body = ast.program.body.filter((i: any) => {
// export function a() {}
// export interface Bang {}
// export type Foo = string | number;
if (
i.type === AST_NODE_TYPES.ExportNamedDeclaration &&
(i.declaration.type === AST_NODE_TYPES.TSTypeAliasDeclaration ||
i.declaration.type === AST_NODE_TYPES.FunctionDeclaration ||
i.declaration.type === AST_NODE_TYPES.TSInterfaceDeclaration)
) {
return allowed.includes(i.declaration.id.name);
}
// export const manager = {};
// export const {f,g: h} = {f: 4, g:5};
if (
i.type === AST_NODE_TYPES.ExportNamedDeclaration &&
i.declaration.type === AST_NODE_TYPES.VariableDeclaration
) {
// MUTATION!
// eslint-disable-next-line no-param-reassign
i.declaration.declarations = i.declaration.declarations.filter((d: any) => {
// export function a() {}
// export interface Bang {}
// export type Foo = string | number;
if (
i.type === AST_NODE_TYPES.ExportNamedDeclaration &&
(i.declaration.type === AST_NODE_TYPES.TSTypeAliasDeclaration ||
i.declaration.type === AST_NODE_TYPES.FunctionDeclaration ||
i.declaration.type === AST_NODE_TYPES.TSInterfaceDeclaration)
) {
return allowed.includes(i.declaration.id.name);
}
// export const manager = {};
// export const {f,g: h} = {f: 4, g:5};
if (
i.type === AST_NODE_TYPES.ExportNamedDeclaration &&
i.declaration.type === AST_NODE_TYPES.VariableDeclaration
) {
// MUTATION!
// eslint-disable-next-line no-param-reassign
i.declaration.declarations = i.declaration.declarations.filter((d: any) => {
if (d.id.type === AST_NODE_TYPES.ObjectPattern) {
// MUTATION!
// eslint-disable-next-line no-param-reassign
d.id.properties = d.id.properties.filter(
(p: any) =>
p.type === AST_NODE_TYPES.Property &&
p.value.type === AST_NODE_TYPES.Identifier &&
allowed.includes(p.value.name)
);
return !!d.id.properties.length;
}
import util = require('util');
import path = require('path');
import { TsClassInfo, TsEnumInfo, TsModuleInfo } from './TsCollector';
import { TranslateOption } from './TranslateOption';
import { stringify } from 'querystring';
export class LuaMaker {
private readonly noBraceTypes = [AST_NODE_TYPES.MemberExpression, AST_NODE_TYPES.ThisExpression, AST_NODE_TYPES.Identifier, AST_NODE_TYPES.CallExpression, AST_NODE_TYPES.TSAsExpression, AST_NODE_TYPES.TSTypeAssertion, AST_NODE_TYPES.Super];
// TODO: Typeof's return value may be different between ts and lua
private readonly tsType2luaType = {
'undefined': 'nil',
'object': 'table'
};
private readonly ignoreExpressionType = [AST_NODE_TYPES.MemberExpression, AST_NODE_TYPES.Identifier];
private readonly luaKeyWords = ['and', 'break', 'do', 'else', 'elseif', 'end', 'false', 'for', 'function', 'if', 'in', 'local', 'nil', 'not', 'or', 'repeat', 'return', 'then', 'true', 'until', 'while'];
private pv = 0;
private readonly operatorPriorityMap: { [opt: string]: number } = {};
constructor() {
this.setPriority(['( … )'], this.pv++);
this.setPriority(['… . …', '… [ … ]', 'new … ( … )', '… ( … )'], this.pv++);
this.setPriority(['new …'], this.pv++);
this.setPriority(['… ++', '… --'], this.pv++);
this.setPriority(['! …', '~ …', '+ …', '- …', '++ …', '-- …', 'typeof …', 'void …', 'delete …', 'await …'], this.pv++);
this.setPriority(['… ** …'], this.pv++);
this.setPriority(['… * …', '… / …', '… % …'], this.pv++);
this.setPriority(['… + …', '… - …'], this.pv++);
this.setPriority(['… << …', '… >> …', '… >>> …'], this.pv++);
} else if(optStr == '!=') {
optStr = '~=';
} else if(optStr == '===') {
optStr = '==';
} else if(optStr == '!==') {
optStr = '~=';
}
if(optStr == 'instanceof') {
return left + ':instanceof(' + right + ')';
}
let str = '';
let astType = (ast as any).type;
if(astType == AST_NODE_TYPES.AssignmentExpression) {
if(ast.right.type == AST_NODE_TYPES.AssignmentExpression) {
// 处理 a = b = c
str = right + '\n';
right = this.codeFromAST((ast.right as AssignmentExpression).left);
} else if(ast.right.type == AST_NODE_TYPES.UpdateExpression && (ast.right as UpdateExpression).prefix) {
// 处理 a = ++b
str = right + '\n';
right = this.codeFromAST((ast.right as UpdateExpression).argument);
}
}
if(isSelfOperator) {
return str + left + ' = ' + left + ' ' + optStr + ' ' + right;
}
return str + left + ' ' + optStr + ' ' + right;
}
}
// export { n as b, m as c };
if (i.type === AST_NODE_TYPES.ExportNamedDeclaration && i.specifiers) {
return false;
}
// export default class Foo {};
// @ts-ignore (remove after https://github.com/typescript-eslint/typescript-eslint/pull/378 is merged)
if (i.type === AST_NODE_TYPES.ExportDefaultDeclaration) {
throw new Error('ExportAllDeclaration is not supported in Storybook config');
/* This is not supported because we don't have a config property called 'default' */
}
// export * from 'foo';
if (i.type === AST_NODE_TYPES.ExportAllDeclaration) {
throw new Error('ExportDefaultDeclaration is not supported in Storybook config');
/* This is not supported because we'd have to recurse into the modules which would add a lot of complexity */
/* The solution is the make exports explicit */
}
return true;
}, []);
return allowed.includes(d.id.name);
}
return false;
}, []);
return !!i.declaration.declarations.length;
}
// export { n as b, m as c };
if (i.type === AST_NODE_TYPES.ExportNamedDeclaration && i.specifiers) {
return false;
}
// export default class Foo {};
// @ts-ignore (remove after https://github.com/typescript-eslint/typescript-eslint/pull/378 is merged)
if (i.type === AST_NODE_TYPES.ExportDefaultDeclaration) {
throw new Error('ExportAllDeclaration is not supported in Storybook config');
/* This is not supported because we don't have a config property called 'default' */
}
// export * from 'foo';
if (i.type === AST_NODE_TYPES.ExportAllDeclaration) {
throw new Error('ExportDefaultDeclaration is not supported in Storybook config');
/* This is not supported because we'd have to recurse into the modules which would add a lot of complexity */
/* The solution is the make exports explicit */
}
return true;
}, []);