@neo-one/smart-contract-compiler
Version:
NEO•ONE TypeScript smart contract compiler.
391 lines (389 loc) • 17.3 kB
JavaScript
import { addressToScriptHash, common } from '@neo-one/client-common';
import { symbolKey, tsUtils } from '@neo-one/ts-utils';
import { utils } from '@neo-one/utils';
import ts from 'typescript';
import { isOnlyArray, isOnlyBoolean, isOnlyBuffer, isOnlyMap, isOnlyNull, isOnlyNumber, isOnlySet, isOnlyString, isOnlySymbol, isOnlyUndefined, } from '../compile/helper/types';
import { DiagnosticCode } from '../DiagnosticCode';
import { DiagnosticMessage } from '../DiagnosticMessage';
import { createMemoized, nodeKey, typeKey } from '../utils';
export const DEFAULT_DIAGNOSTIC_OPTIONS = {
error: true,
warning: false,
};
export class AnalysisService {
constructor(context) {
this.context = context;
this.memoized = createMemoized();
}
getFunctionReturnType(node, options = DEFAULT_DIAGNOSTIC_OPTIONS) {
if (ts.isAccessor(node)) {
return this.getType(node);
}
const typeNode = tsUtils.type_.getTypeNode(node);
if (typeNode !== undefined) {
return this.getNotAnyType(typeNode, tsUtils.type_.getTypeFromTypeNode(this.context.typeChecker, typeNode));
}
const signatureTypes = this.extractSignature(node, options);
return signatureTypes === undefined ? undefined : signatureTypes.returnType;
}
extractAllSignatures(node, options = DEFAULT_DIAGNOSTIC_OPTIONS) {
return this.extractAllSignaturesForType(node, this.getType(node), options);
}
extractSignature(node, options = DEFAULT_DIAGNOSTIC_OPTIONS) {
return this.extractSignatureForType(node, this.getType(node), options);
}
getSignatures(node) {
const signature = this.context.typeChecker.getResolvedSignature(node);
if (signature !== undefined && !tsUtils.signature.isFailure(signature)) {
return [signature];
}
const expr = tsUtils.expression.getExpressionForCall(node);
const type = this.getType(expr);
if (type === undefined) {
return undefined;
}
return tsUtils.type_.getCallSignatures(type);
}
extractAllSignaturesForType(node, type, options = DEFAULT_DIAGNOSTIC_OPTIONS) {
const signatures = type === undefined ? undefined : tsUtils.type_.getCallSignatures(type);
if (signatures === undefined) {
return [];
}
return signatures.map((signature) => this.extractSignatureTypes(node, signature, options)).filter(utils.notNull);
}
extractSignatureForType(node, type, options = DEFAULT_DIAGNOSTIC_OPTIONS) {
const signatureTypes = this.extractAllSignaturesForType(node, type, options);
if (signatureTypes.length === 0) {
return undefined;
}
if (signatureTypes.length !== 1) {
this.report(options, node, DiagnosticCode.MultipleSignatures, DiagnosticMessage.MultipleSignatures);
return undefined;
}
return signatureTypes[0];
}
extractSignaturesForCall(node, options = DEFAULT_DIAGNOSTIC_OPTIONS) {
const signatures = this.getSignatures(node);
if (signatures === undefined) {
return undefined;
}
return signatures.map((signature) => this.extractSignatureTypes(node, signature, options)).filter(utils.notNull);
}
extractSignatureTypes(node, signature, options = DEFAULT_DIAGNOSTIC_OPTIONS) {
const params = tsUtils.signature.getParameters(signature);
const paramTypes = params.map((param) => this.getTypeOfSymbol(param, node));
const paramDeclsNullable = params.map((param) => tsUtils.symbol.getValueDeclaration(param));
const nullParamIndex = paramDeclsNullable.indexOf(undefined);
if (nullParamIndex !== -1) {
const nullParam = params[nullParamIndex];
this.report(options, node, DiagnosticCode.Invariant, DiagnosticMessage.MissingParameterDeclaration, tsUtils.symbol.getName(nullParam));
return undefined;
}
const paramDecls = paramDeclsNullable.filter(utils.notNull).filter(ts.isParameter);
const declToType = new Map();
for (const [paramDecl, paramType] of utils.zip(paramDecls, paramTypes)) {
declToType.set(paramDecl, paramType);
}
return {
paramDecls,
paramTypes: declToType,
returnType: this.getNotAnyType(node, tsUtils.signature.getReturnType(signature)),
};
}
extractLiteralAddress(original) {
return this.memoized('extract-literal-address', nodeKey(original), () => this.extractLiteral(original, 'AddressConstructor', (value) => {
try {
return common.stringToUInt160(addressToScriptHash(value));
}
catch {
return common.stringToUInt160(value);
}
}, common.bufferToUInt160));
}
extractLiteralHash256(original) {
return this.extractLiteral(original, 'Hash256Constructor', common.stringToUInt256, common.bufferToUInt256);
}
extractLiteralPublicKey(original) {
return this.extractLiteral(original, 'PublicKeyConstructor', common.stringToECPoint, common.bufferToECPoint);
}
getType(node, options = {}) {
return this.memoized('get-type', nodeKey(node), () => this.getNotAnyType(node, tsUtils.type_.getType(this.context.typeChecker, node), options));
}
getTypeOfSymbol(symbol, node) {
if (symbol === undefined) {
return undefined;
}
return this.memoized('get-type-of-symbol', `${symbolKey(symbol)}:${nodeKey(node)}`, () => this.getNotAnyType(node, tsUtils.type_.getTypeAtLocation(this.context.typeChecker, symbol, node)));
}
getSymbol(node) {
return this.memoized('symbol', nodeKey(node), () => {
const symbol = tsUtils.node.getSymbol(this.context.typeChecker, node);
if (symbol === undefined) {
return undefined;
}
const aliased = tsUtils.symbol.getAliasedSymbol(this.context.typeChecker, symbol);
if (aliased !== undefined) {
return aliased;
}
return symbol;
});
}
getTypeSymbol(node) {
return this.memoized('get-type-symbol', nodeKey(node), () => {
const type = this.getType(node);
return this.getSymbolForType(node, type);
});
}
getSymbolForType(_node, type) {
if (type === undefined) {
return undefined;
}
return this.memoized('get-symbol-for-type', typeKey(type), () => {
let symbol = tsUtils.type_.getAliasSymbol(type);
if (symbol === undefined) {
symbol = tsUtils.type_.getSymbol(type);
}
if (symbol === undefined) {
return undefined;
}
const aliased = tsUtils.symbol.getAliasedSymbol(this.context.typeChecker, symbol);
if (aliased !== undefined) {
return aliased;
}
return symbol;
});
}
getNotAnyType(node, type, { error = true } = {}) {
if (type !== undefined && tsUtils.type_.isAny(type)) {
if (error && !tsUtils.type_.isErrorType(type)) {
this.context.reportTypeError(node);
}
return undefined;
}
if (type !== undefined) {
const constraintType = tsUtils.type_.getConstraint(type);
if (constraintType !== undefined) {
return constraintType;
}
}
return type;
}
extractStorageKey(node) {
return this.memoized('extract-storage-key', nodeKey(node), () => {
const smartContract = tsUtils.node.getFirstAncestorByTest(node, ts.isClassDeclaration);
if (smartContract === undefined || !this.isSmartContract(smartContract)) {
return undefined;
}
const decl = tsUtils.node.getFirstAncestorByTest(node, ts.isPropertyDeclaration);
if (decl === undefined) {
return undefined;
}
return tsUtils.node.getName(decl);
});
}
isSmartContract(node) {
return this.memoized('is-smart-contract', nodeKey(node), () => {
const extendsExpr = tsUtils.class_.getExtends(node);
const isSmartContract = extendsExpr !== undefined &&
this.context.builtins.isValue(tsUtils.expression.getExpression(extendsExpr), 'SmartContract');
if (isSmartContract) {
return true;
}
const baseClasses = tsUtils.class_.getBaseClasses(this.context.typeChecker, node);
if (baseClasses.some((value) => this.context.builtins.isValue(value, 'SmartContract'))) {
return true;
}
const baseClass = tsUtils.class_.getBaseClass(this.context.typeChecker, node);
return baseClass !== undefined && this.isSmartContract(baseClass);
});
}
isSmartContractNode(node) {
return this.memoized('is-smart-contract-node', nodeKey(node), () => {
const symbol = this.getSymbol(node);
if (symbol === undefined) {
return false;
}
const decls = tsUtils.symbol.getDeclarations(symbol);
if (decls.length === 0) {
return false;
}
const decl = decls[0];
return ts.isClassDeclaration(decl) && this.isSmartContract(decl);
});
}
getSymbolAndAllInheritedSymbols(node) {
return this.memoized('get-symbol-and-all-inherited-symbols', nodeKey(node), () => {
const symbol = this.getSymbol(node);
const symbols = [symbol].filter(utils.notNull);
if (ts.isClassDeclaration(node) || ts.isClassExpression(node) || ts.isInterfaceDeclaration(node)) {
const baseTypes = tsUtils.class_.getBaseTypesFlattened(this.context.typeChecker, node);
return symbols.concat(baseTypes.map((baseType) => this.getSymbolForType(node, baseType)).filter(utils.notNull));
}
return symbols;
});
}
isValidStorageType(node, type) {
return !tsUtils.type_.hasType(type, (tpe) => !tsUtils.type_.isOnlyType(tpe, (tp) => isOnlyUndefined(this.context, node, tp) ||
isOnlyNull(this.context, node, tp) ||
isOnlyBoolean(this.context, node, tp) ||
isOnlyNumber(this.context, node, tp) ||
isOnlyString(this.context, node, tp) ||
isOnlySymbol(this.context, node, tp) ||
isOnlyBuffer(this.context, node, tp) ||
this.isValidStorageArray(node, tp) ||
this.isValidStorageMap(node, tp) ||
this.isValidStorageSet(node, tp)));
}
findReferencesAsNodes(node) {
return this.memoized('find-references-as-nodes', nodeKey(node), () => tsUtils.reference
.findReferencesAsNodes(this.context.program, this.context.languageService, node)
.filter((found) => this.context.sourceFiles.has(tsUtils.node.getSourceFile(found))));
}
isSmartContractMixinFunction(node) {
const parameters = tsUtils.parametered.getParameters(node);
if (parameters.length !== 1) {
return false;
}
const signatureTypess = this.extractAllSignatures(node);
if (signatureTypess.length !== 1) {
return false;
}
const signatureTypes = signatureTypess[0];
const firstParam = signatureTypes.paramDecls[0];
const firstParamType = signatureTypes.paramTypes.get(firstParam);
if (firstParamType === undefined || tsUtils.type_.getConstructSignatures(firstParamType).length !== 1) {
return false;
}
const constructSignatureTypes = this.extractSignatureTypes(firstParam, tsUtils.type_.getConstructSignatures(firstParamType)[0]);
if (constructSignatureTypes === undefined) {
return false;
}
const returnTypeSymbol = this.getSymbolForType(firstParam, constructSignatureTypes.returnType);
return returnTypeSymbol !== undefined && returnTypeSymbol === this.context.builtins.getValueSymbol('SmartContract');
}
isValidStorageArray(node, type) {
if (!isOnlyArray(this.context, node, type)) {
return false;
}
const typeArguments = tsUtils.type_.getTypeArgumentsArray(type);
if (typeArguments.length !== 1) {
return true;
}
return this.isValidStorageType(node, typeArguments[0]);
}
isValidStorageMap(node, type) {
if (!isOnlyMap(this.context, node, type)) {
return false;
}
const typeArguments = tsUtils.type_.getTypeArgumentsArray(type);
if (typeArguments.length !== 2) {
return true;
}
return this.isValidStorageType(node, typeArguments[0]) && this.isValidStorageType(node, typeArguments[1]);
}
isValidStorageSet(node, type) {
if (!isOnlySet(this.context, node, type)) {
return false;
}
const typeArguments = tsUtils.type_.getTypeArgumentsArray(type);
if (typeArguments.length !== 1) {
return true;
}
return this.isValidStorageType(node, typeArguments[0]);
}
extractLiteral(original, name, processText, processBuffer) {
return this.traceIdentifier(original, (node) => {
if (!ts.isCallExpression(node) && !ts.isTaggedTemplateExpression(node)) {
return undefined;
}
const expr = ts.isCallExpression(node) ? tsUtils.expression.getExpression(node) : tsUtils.template.getTag(node);
const symbol = this.getSymbol(expr);
const hash256From = this.context.builtins.getOnlyMemberSymbol(name, 'from');
const bufferFrom = this.context.builtins.getOnlyMemberSymbol('BufferConstructor', 'from');
if (symbol === hash256From) {
const arg = ts.isCallExpression(node)
? tsUtils.argumented.getArguments(node)[0]
: tsUtils.template.getTemplate(node);
if (ts.isTaggedTemplateExpression(node) &&
!ts.isNoSubstitutionTemplateLiteral(tsUtils.template.getTemplate(node))) {
return undefined;
}
if (arg === undefined) {
return undefined;
}
return this.traceIdentifier(arg, (value) => {
if (ts.isStringLiteral(value) || ts.isNoSubstitutionTemplateLiteral(value)) {
try {
return processText(tsUtils.literal.getLiteralValue(value));
}
catch {
}
}
return undefined;
});
}
if (symbol === bufferFrom && ts.isCallExpression(node)) {
const arg = tsUtils.argumented.getArguments(node)[0];
if (arg === undefined) {
return undefined;
}
return this.traceIdentifier(arg, (value) => {
if (!ts.isStringLiteral(value)) {
return undefined;
}
try {
return processBuffer(Buffer.from(tsUtils.literal.getLiteralValue(value), 'hex'));
}
catch {
return undefined;
}
});
}
return undefined;
});
}
traceIdentifier(nodeIn, extractValue) {
const node = this.unwrapExpression(nodeIn);
if (ts.isIdentifier(node)) {
const symbol = this.getSymbol(node);
if (symbol === undefined) {
return undefined;
}
const decl = tsUtils.symbol.getValueDeclaration(symbol);
if (decl === undefined) {
return undefined;
}
if (!ts.isVariableDeclaration(decl)) {
return undefined;
}
const parent = tsUtils.node.getParent(decl);
if (!ts.isVariableDeclarationList(parent) || !tsUtils.modifier.isConst(parent)) {
return undefined;
}
const initializer = tsUtils.initializer.getInitializer(parent);
if (initializer === undefined) {
return undefined;
}
return this.traceIdentifier(initializer, extractValue);
}
return extractValue(node);
}
unwrapExpression(node) {
if (ts.isParenthesizedExpression(node)) {
return tsUtils.expression.getExpression(node);
}
if (ts.isAsExpression(node)) {
return tsUtils.expression.getExpression(node);
}
return node;
}
report(options, node, code, message, ...args) {
if (options.error) {
this.context.reportError(node, code, message, ...args);
}
else if (options.warning) {
this.context.reportWarning(node, code, message, ...args);
}
}
}
//# sourceMappingURL=AnalysisService.js.map