Skip to content

Commit

Permalink
WIP: generate global fn with comment
Browse files Browse the repository at this point in the history
  • Loading branch information
mahaker committed Aug 27, 2024
1 parent bd49a6e commit 9ccd4bb
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"deno.enable": true,
"deno.enablePaths": ["mod.ts", "build.ts", "test/deno"]
"deno.enablePaths": ["mod.ts", "lib", "build.ts", "test/deno"]
}
5 changes: 5 additions & 0 deletions build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ await build({
version: "^2.1.0",
peerDependency: false,
},
"https://esm.sh/typescript@5.5.4": {
name: "typescript",
version: "^5.5.4",
peerDependency: false,
},
},
postBuild() {
Deno.copyFileSync("LICENSE", "npm/LICENSE");
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"lock": false,
"tasks": {
"build": "deno run -A build.ts",
"test:deno": "cd test/deno ; deno test -A --unstable",
"test:deno": "cd test/deno ; deno test -A",
"test:node": "cd test/node ; npm i && npm run test"
}
}
Expand Down
54 changes: 54 additions & 0 deletions lib/gas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import * as TS from "https://esm.sh/typescript@5.5.4";
// @deno-types="../generate.d.ts"
import { generate } from "https://esm.sh/gas-entry-generator@2.1.0";

export function getVersion(): string {
return TS.version;
}

export function getGasEntryPointFunctions(code: string): string {
const gas = generate(code, { comment: true });
return gas.entryPointFunctions;
}

/**
* TODO: find function definitions with comment.
* ref: https://stackoverflow.com/questions/47429792/is-it-possible-to-get-comments-as-nodes-in-the-ast-using-the-typescript-compiler
* TODO: save scanned file path. if scanned file and ignore this.
*/
// export function getEntryPointFunctions(code: string): void {
// const source = TS.createSourceFile("hoge.ts", code, TS.ScriptTarget.ES2015)
export async function getEntryPointFunctions(path: string): Promise<void> {
const code = await Deno.readTextFile(path)
const source = TS.createSourceFile("hoge.ts", code, TS.ScriptTarget.ES2015)

source.forEachChild(node => {
visitExpressionStatement(node)
});
}

function visitExpressionStatement(node: TS.Node): void {
if (!TS.isExpressionStatement(node)) return;

node.forEachChild(expression => {
visitBinaryExpression(expression)
});
}

function visitBinaryExpression(node: TS.Node): void {
if (!TS.isBinaryExpression(node)) return;

node.forEachChild(expression => {
visitPropertyAccessExpression(expression)
});
}

function visitPropertyAccessExpression(node: TS.Node): void {
if (
TS.isPropertyAccessExpression(node)
&& TS.isIdentifier(node.expression)
&& node.expression.escapedText === "global"
) {
console.log("Global Assignment!!", node.name.escapedText)
}
}
14 changes: 8 additions & 6 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { PluginBuild } from "https://deno.land/x/esbuild@v0.18.4/mod.d.ts";
import { StringReader, readLines } from "https://deno.land/std@0.190.0/io/mod.ts";
// @deno-types="./generate.d.ts"
import { generate } from "https://esm.sh/gas-entry-generator@2.1.0";
import { getGasEntryPointFunctions, getEntryPointFunctions } from "./lib/gas.ts";

async function countLines(s: string) {
const reader = new StringReader(s)
Expand Down Expand Up @@ -29,7 +28,11 @@ async function deleteBanner(code: string, banner: string) {

export const GasPlugin = {
name: "gas-plugin",
setup({ onEnd, initialOptions }: PluginBuild) {
setup({ onLoad, onEnd, initialOptions }: PluginBuild) {
onLoad({ filter: /\.ts$/ }, async (args) => {
await getEntryPointFunctions(args.path)
return null;
}),
onEnd(async () => {
if (initialOptions.outfile === undefined) {
throw Error(
Expand All @@ -39,18 +42,17 @@ export const GasPlugin = {

const jsBanner = initialOptions.banner?.js;
const code = await Deno.readTextFile(initialOptions.outfile);
const gas = generate(code, { comment: true });

if (jsBanner === undefined) {
await Deno.writeTextFile(
initialOptions.outfile,
`let global = this;\n${gas.entryPointFunctions}\n${code}`,
`let global = this;\n${getGasEntryPointFunctions(code)}\n${code}`,
);
} else {
const bannerDeleted = await deleteBanner(code, jsBanner);
await Deno.writeTextFile(
initialOptions.outfile,
`${jsBanner}\nlet global = this;\n${gas.entryPointFunctions}${bannerDeleted}`,
`${jsBanner}\nlet global = this;\n${getGasEntryPointFunctions(code)}${bannerDeleted}`,
);
}
});
Expand Down

0 comments on commit 9ccd4bb

Please sign in to comment.