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
2 changes: 1 addition & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"access": "public",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"ignore": ["tester"]
"ignore": ["tester", "app-with-prop-access"]
}
12 changes: 12 additions & 0 deletions .changeset/real-laws-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
"@plutolang/static-generator": patch
"@plutolang/static-deducer": patch
"@plutolang/pluto-infra": patch
"@plutolang/pluto": patch
"@plutolang/base": patch
"@plutolang/cli": patch
---

feat: support for transfering the values generated by compile-time to runtime

Note: it hasn't supported for simulation testing yet, but it already can be used on the cloud platform.
3 changes: 1 addition & 2 deletions apps/cli/src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ async function confirmArch(archRef: arch.Architecture, confirmed: boolean): Prom
for (const relat of archRef.relationships) {
if (relat.from.type == "Root") continue;

const typ = relat.type == "access" ? "Access" : "Create";
relatData.push([relat.from.name, relat.to.name, typ, relat.operation]);
relatData.push([relat.from.name, relat.to.name, relat.type, relat.operation]);
}

// To display the relationship table, which includes the relationships among resources in the arch ref.
Expand Down
3 changes: 3 additions & 0 deletions components/deducers/static/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**/*.d.ts
**/*.js
**/*.js.map
1 change: 1 addition & 0 deletions components/deducers/static/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as default } from "./src";
12 changes: 8 additions & 4 deletions components/deducers/static/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,31 @@
"directory": "components/deducers/static"
},
"author": "plutolang",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist/"
"dist"
],
"scripts": {
"build": "tsc",
"watch": "npm run build -- --watch",
"test": "vitest run --coverage",
"test:watch": "vitest --coverage",
"lint": "eslint ."
"lint": "eslint .",
"clean": "tsc --build --clean"
},
"dependencies": {
"@plutolang/base": "workspace:^",
"fs-extra": "^11.1.1",
"js-yaml": "^4.1.0",
"typescript": "^5.2.2"
},
"devDependencies": {
"@plutolang/pluto": "workspace:^",
"@types/fs-extra": "^11.0.4",
"@types/node": "^20.8.4",
"@vitest/coverage-v8": "^0.34.6",
"vite": "4",
"vitest": "^0.34.6"
}
}
5 changes: 4 additions & 1 deletion components/deducers/static/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export const RESOUCE_TYPE_NAME = "Resource";
export const RESOUCE_TYPE_NAME = "IResource";
export const FN_RESOURCE_TYPE_NAME = "FnResource";
export const PLUTO_BASE_PKG = "@plutolang/base";
export const CAPTURED_PROPS_IFACE_NAME = "IResourceCapturedProps";
export const CLIENT_API_IFACE_NAME = "IResourceClientApi";
export const INFRA_API_IFACE_NAME = "IResourceInfraApi";
4 changes: 2 additions & 2 deletions components/deducers/static/src/deducer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as ts from "typescript";
import * as path from "path";
import ts from "typescript";
import path from "path";
import { arch, core } from "@plutolang/base";
import { genImportStats } from "./imports";
import { ResourceRelationshipInfo, ResourceVariableInfo } from "./types";
Expand Down
61 changes: 0 additions & 61 deletions components/deducers/static/src/utils-test.ts

This file was deleted.

174 changes: 174 additions & 0 deletions components/deducers/static/src/utils/belong-to-iface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
import { dirname, resolve } from "path";
import { existsSync, readFileSync, statSync } from "fs";
import ts from "typescript";
import {
CAPTURED_PROPS_IFACE_NAME,
CLIENT_API_IFACE_NAME,
INFRA_API_IFACE_NAME,
PLUTO_BASE_PKG,
} from "../constants";

export function methodBelongsToClientApi(
callExpression: ts.CallExpression,
typeChecker: ts.TypeChecker
): boolean {
return methodBelongsToIface(callExpression, typeChecker, PLUTO_BASE_PKG, CLIENT_API_IFACE_NAME);
}

export function methodBelongsToInfraApi(
callExpression: ts.CallExpression,
typeChecker: ts.TypeChecker
): boolean {
return methodBelongsToIface(callExpression, typeChecker, PLUTO_BASE_PKG, INFRA_API_IFACE_NAME);
}

export function propBelongsToCapturedProps(
propAccessExpression: ts.PropertyAccessExpression,
typeChecker: ts.TypeChecker
): boolean {
return propBelongsToIface(
propAccessExpression,
typeChecker,
PLUTO_BASE_PKG,
CAPTURED_PROPS_IFACE_NAME
);
}

/**
* Checks if the given call expression belongs to a specified interface.
*
* @param {ts.CallExpression} callExpression - The call expression node representing the method invocation.
* @param {ts.TypeChecker} typeChecker - The type checker instance used to resolve types and symbols.
* @param {string} targetIfaceName - The name of the target interface to check against.
* @returns {boolean} - True if the method belongs to the target interface, false otherwise.
*/
function methodBelongsToIface(
callExpression: ts.CallExpression,
typeChecker: ts.TypeChecker,
targetPkgName: string,
targetIfaceName: string
): boolean {
// Resolve the call signature from the call expression.
const signature = typeChecker.getResolvedSignature(callExpression);
if (!signature) {
return false;
}

// Get the declaration of the given call expression.
const declaration = signature.getDeclaration();
if (!declaration) {
return false;
}
return declarationBelongsToIface(declaration, typeChecker, targetPkgName, targetIfaceName);
}

/**
* Checks if the given property access expression belongs to a specified interface.
*
* @param {ts.PropertyAccessExpression} propAccessExpression - The property access expression node representing the method invocation.
* @param {ts.TypeChecker} typeChecker - The type checker instance used to resolve types and symbols.
* @param {string} targetIfaceName - The name of the target interface to check against.
* @returns {boolean} - True if the method belongs to the target interface, false otherwise.
*/
function propBelongsToIface(
propAccessExpression: ts.PropertyAccessExpression,
typeChecker: ts.TypeChecker,
targetPkgName: string,
targetIfaceName: string
): boolean {
const symbol = typeChecker.getSymbolAtLocation(propAccessExpression);
const declarations = symbol?.declarations;
if (!declarations || declarations.length === 0) {
return false;
}

for (const declaration of declarations) {
if (declarationBelongsToIface(declaration, typeChecker, targetPkgName, targetIfaceName)) {
return true;
}
}
return false;
}

function declarationBelongsToIface(
declaration: ts.Declaration,
typeChecker: ts.TypeChecker,
targetPkgName: string,
targetIfaceName: string
): boolean {
if (
!ts.isClassDeclaration(declaration.parent) &&
!ts.isInterfaceDeclaration(declaration.parent)
) {
return false;
}

// Recursively inspect the type inheritance chain to determine whether the target interface exists within it.
const typeChain: ts.Type[] = [];
typeChain.push(typeChecker.getTypeAtLocation(declaration.parent));
let belongsToTargetIface = false;
while (typeChain.length > 0) {
const currentType = typeChain.pop()!;
if (!currentType.isClassOrInterface()) {
continue;
}

const symbol = currentType.getSymbol();
if (symbol && symbol.name === targetIfaceName && isSymbolFromPackage(symbol, targetPkgName)) {
belongsToTargetIface = true;
break;
}

const baseTypes = typeChecker.getBaseTypes(currentType);
typeChain.push(...baseTypes);
}
return belongsToTargetIface;
}

/**
* Check if a symbol comes from a specific package.
*
* @param {ts.Symbol} symbol - The symbol to check.
* @param {string} packageName - The name of the package to check against.
* @returns {boolean} - True if the symbol is from the package, false otherwise.
*/
function isSymbolFromPackage(symbol: ts.Symbol, packageName: string): boolean {
// Get the declaration(s) of the symbol
const declarations = symbol.getDeclarations();

// If there are no declarations available, the symbol does not belong to any package
if (!declarations || declarations.length === 0) {
return false;
}

// Check each declaration to see if any come from the target package
for (const declaration of declarations) {
const sourceFile = declaration.getSourceFile();
const filepath = sourceFile.fileName;
if (packageName === findPackageName(filepath)) {
return true;
}
}
// The symbol does not come from the target package
return false;
}

function findPackageName(filepath: string): string | undefined {
let dirpath = filepath;
if (statSync(dirpath).isFile()) {
dirpath = dirname(dirpath);
}

let packageName: string | undefined;
while (dirpath.length >= 1) {
const pkgJsonPath = resolve(dirpath, "package.json");
if (existsSync(pkgJsonPath)) {
const pkgJsonContent = readFileSync(pkgJsonPath).toString();
const pkgJsonData = JSON.parse(pkgJsonContent);
packageName = pkgJsonData["name"];
break;
}
dirpath = dirname(dirpath);
}
return packageName;
}
2 changes: 2 additions & 0 deletions components/deducers/static/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./belong-to-iface";
export * from "./type-checker";
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ts from "typescript";
import { Location } from "./types";
import { ImportStore, extractImportElements } from "./imports";
import { FN_RESOURCE_TYPE_NAME, PLUTO_BASE_PKG, RESOUCE_TYPE_NAME } from "./constants";
import { Location } from "../types";
import { ImportStore, extractImportElements } from "../imports";
import { FN_RESOURCE_TYPE_NAME, PLUTO_BASE_PKG, RESOUCE_TYPE_NAME } from "../constants";

export function isPrimitive(val: string): boolean {
const primitiveTypes = [
Expand Down
2 changes: 1 addition & 1 deletion components/deducers/static/src/visit-expression.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as ts from "typescript";
import ts from "typescript";
import { arch } from "@plutolang/base";
import {
ParameterInfo,
Expand Down
Loading