starto
This commit is contained in:
parent
4c202261c8
commit
ddc92a2b6d
10 changed files with 2147 additions and 10 deletions
59
.gitignore
vendored
59
.gitignore
vendored
|
@ -1,78 +1,101 @@
|
||||||
|
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
|
||||||
|
|
||||||
# Logs
|
# Logs
|
||||||
|
|
||||||
logs
|
logs
|
||||||
*.log
|
_.log
|
||||||
npm-debug.log*
|
npm-debug.log_
|
||||||
yarn-debug.log*
|
yarn-debug.log*
|
||||||
yarn-error.log*
|
yarn-error.log*
|
||||||
lerna-debug.log*
|
lerna-debug.log*
|
||||||
.pnpm-debug.log*
|
.pnpm-debug.log*
|
||||||
|
|
||||||
# Diagnostic reports (https://nodejs.org/api/report.html)
|
# Diagnostic reports (https://nodejs.org/api/report.html)
|
||||||
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
|
||||||
|
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
|
||||||
|
|
||||||
# Runtime data
|
# Runtime data
|
||||||
|
|
||||||
pids
|
pids
|
||||||
*.pid
|
_.pid
|
||||||
*.seed
|
_.seed
|
||||||
*.pid.lock
|
\*.pid.lock
|
||||||
|
|
||||||
# Directory for instrumented libs generated by jscoverage/JSCover
|
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||||
|
|
||||||
lib-cov
|
lib-cov
|
||||||
|
|
||||||
# Coverage directory used by tools like istanbul
|
# Coverage directory used by tools like istanbul
|
||||||
|
|
||||||
coverage
|
coverage
|
||||||
*.lcov
|
\*.lcov
|
||||||
|
|
||||||
# nyc test coverage
|
# nyc test coverage
|
||||||
|
|
||||||
.nyc_output
|
.nyc_output
|
||||||
|
|
||||||
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
|
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
|
||||||
|
|
||||||
.grunt
|
.grunt
|
||||||
|
|
||||||
# Bower dependency directory (https://bower.io/)
|
# Bower dependency directory (https://bower.io/)
|
||||||
|
|
||||||
bower_components
|
bower_components
|
||||||
|
|
||||||
# node-waf configuration
|
# node-waf configuration
|
||||||
|
|
||||||
.lock-wscript
|
.lock-wscript
|
||||||
|
|
||||||
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
||||||
|
|
||||||
build/Release
|
build/Release
|
||||||
|
|
||||||
# Dependency directories
|
# Dependency directories
|
||||||
|
|
||||||
node_modules/
|
node_modules/
|
||||||
jspm_packages/
|
jspm_packages/
|
||||||
|
|
||||||
# Snowpack dependency directory (https://snowpack.dev/)
|
# Snowpack dependency directory (https://snowpack.dev/)
|
||||||
|
|
||||||
web_modules/
|
web_modules/
|
||||||
|
|
||||||
# TypeScript cache
|
# TypeScript cache
|
||||||
*.tsbuildinfo
|
|
||||||
|
\*.tsbuildinfo
|
||||||
|
|
||||||
# Optional npm cache directory
|
# Optional npm cache directory
|
||||||
|
|
||||||
.npm
|
.npm
|
||||||
|
|
||||||
# Optional eslint cache
|
# Optional eslint cache
|
||||||
|
|
||||||
.eslintcache
|
.eslintcache
|
||||||
|
|
||||||
# Optional stylelint cache
|
# Optional stylelint cache
|
||||||
|
|
||||||
.stylelintcache
|
.stylelintcache
|
||||||
|
|
||||||
# Microbundle cache
|
# Microbundle cache
|
||||||
|
|
||||||
.rpt2_cache/
|
.rpt2_cache/
|
||||||
.rts2_cache_cjs/
|
.rts2_cache_cjs/
|
||||||
.rts2_cache_es/
|
.rts2_cache_es/
|
||||||
.rts2_cache_umd/
|
.rts2_cache_umd/
|
||||||
|
|
||||||
# Optional REPL history
|
# Optional REPL history
|
||||||
|
|
||||||
.node_repl_history
|
.node_repl_history
|
||||||
|
|
||||||
# Output of 'npm pack'
|
# Output of 'npm pack'
|
||||||
*.tgz
|
|
||||||
|
\*.tgz
|
||||||
|
|
||||||
# Yarn Integrity file
|
# Yarn Integrity file
|
||||||
|
|
||||||
.yarn-integrity
|
.yarn-integrity
|
||||||
|
|
||||||
# dotenv environment variable files
|
# dotenv environment variable files
|
||||||
|
|
||||||
.env
|
.env
|
||||||
.env.development.local
|
.env.development.local
|
||||||
.env.test.local
|
.env.test.local
|
||||||
|
@ -80,51 +103,67 @@ web_modules/
|
||||||
.env.local
|
.env.local
|
||||||
|
|
||||||
# parcel-bundler cache (https://parceljs.org/)
|
# parcel-bundler cache (https://parceljs.org/)
|
||||||
|
|
||||||
.cache
|
.cache
|
||||||
.parcel-cache
|
.parcel-cache
|
||||||
|
|
||||||
# Next.js build output
|
# Next.js build output
|
||||||
|
|
||||||
.next
|
.next
|
||||||
out
|
out
|
||||||
|
|
||||||
# Nuxt.js build / generate output
|
# Nuxt.js build / generate output
|
||||||
|
|
||||||
.nuxt
|
.nuxt
|
||||||
dist
|
dist
|
||||||
|
|
||||||
# Gatsby files
|
# Gatsby files
|
||||||
|
|
||||||
.cache/
|
.cache/
|
||||||
|
|
||||||
# Comment in the public line in if your project uses Gatsby and not Next.js
|
# Comment in the public line in if your project uses Gatsby and not Next.js
|
||||||
|
|
||||||
# https://nextjs.org/blog/next-9-1#public-directory-support
|
# https://nextjs.org/blog/next-9-1#public-directory-support
|
||||||
|
|
||||||
# public
|
# public
|
||||||
|
|
||||||
# vuepress build output
|
# vuepress build output
|
||||||
|
|
||||||
.vuepress/dist
|
.vuepress/dist
|
||||||
|
|
||||||
# vuepress v2.x temp and cache directory
|
# vuepress v2.x temp and cache directory
|
||||||
|
|
||||||
.temp
|
.temp
|
||||||
.cache
|
.cache
|
||||||
|
|
||||||
# Docusaurus cache and generated files
|
# Docusaurus cache and generated files
|
||||||
|
|
||||||
.docusaurus
|
.docusaurus
|
||||||
|
|
||||||
# Serverless directories
|
# Serverless directories
|
||||||
|
|
||||||
.serverless/
|
.serverless/
|
||||||
|
|
||||||
# FuseBox cache
|
# FuseBox cache
|
||||||
|
|
||||||
.fusebox/
|
.fusebox/
|
||||||
|
|
||||||
# DynamoDB Local files
|
# DynamoDB Local files
|
||||||
|
|
||||||
.dynamodb/
|
.dynamodb/
|
||||||
|
|
||||||
# TernJS port file
|
# TernJS port file
|
||||||
|
|
||||||
.tern-port
|
.tern-port
|
||||||
|
|
||||||
# Stores VSCode versions used for testing VSCode extensions
|
# Stores VSCode versions used for testing VSCode extensions
|
||||||
|
|
||||||
.vscode-test
|
.vscode-test
|
||||||
|
|
||||||
# yarn v2
|
# yarn v2
|
||||||
|
|
||||||
.yarn/cache
|
.yarn/cache
|
||||||
.yarn/unplugged
|
.yarn/unplugged
|
||||||
.yarn/build-state.yml
|
.yarn/build-state.yml
|
||||||
.yarn/install-state.gz
|
.yarn/install-state.gz
|
||||||
.pnp.*
|
.pnp.\*
|
||||||
|
|
3
.prettierrc
Normal file
3
.prettierrc
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
"tabWidth": 4
|
||||||
|
}
|
BIN
bun.lockb
Executable file
BIN
bun.lockb
Executable file
Binary file not shown.
1804
package-lock.json
generated
Normal file
1804
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
15
package.json
Normal file
15
package.json
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
{
|
||||||
|
"name": "didactic-chainsaw",
|
||||||
|
"module": "index.ts",
|
||||||
|
"type": "module",
|
||||||
|
"devDependencies": {
|
||||||
|
"bun-types": "latest",
|
||||||
|
"@swc/cli": "^0.1.62",
|
||||||
|
"@swc/core": "^1.3.83",
|
||||||
|
"@types/node": "^20.5.9",
|
||||||
|
"typescript": "^5.2.2"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"ts-node": "^10.9.1"
|
||||||
|
}
|
||||||
|
}
|
81
src/index.ts
Normal file
81
src/index.ts
Normal file
|
@ -0,0 +1,81 @@
|
||||||
|
import swc from "@swc/core";
|
||||||
|
import { MatchScript } from "./types";
|
||||||
|
|
||||||
|
const PATTERN_PATH = "src/patterns/test.json";
|
||||||
|
|
||||||
|
const main = async () => {
|
||||||
|
console.log(Bun.version);
|
||||||
|
|
||||||
|
let inputFile = await Bun.file("src/test_files/simple.js").text();
|
||||||
|
|
||||||
|
console.log(
|
||||||
|
"=====================\nCurrent file to be transformed : \n" +
|
||||||
|
inputFile +
|
||||||
|
"\n===================="
|
||||||
|
);
|
||||||
|
swc.parseFile("src/test_files/simple.js", {
|
||||||
|
syntax: "ecmascript",
|
||||||
|
jsx: false,
|
||||||
|
|
||||||
|
target: "es2022",
|
||||||
|
|
||||||
|
isModule: false,
|
||||||
|
}).then((module) => {
|
||||||
|
//console.log(module);
|
||||||
|
// swc.print(module).then((o: swc.Output) => {
|
||||||
|
// console.log(o);
|
||||||
|
// });
|
||||||
|
|
||||||
|
console.log(module.body);
|
||||||
|
|
||||||
|
matchStatements(module).then((a) => {
|
||||||
|
console.log(
|
||||||
|
"================\nOutput code: \n" +
|
||||||
|
a.code +
|
||||||
|
"\n========================"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const matchStatements = async (module: swc.Script) => {
|
||||||
|
const patternFile = Bun.file(PATTERN_PATH);
|
||||||
|
const [from, to]: [MatchScript, MatchScript] = JSON.parse(
|
||||||
|
await patternFile.text()
|
||||||
|
);
|
||||||
|
return await swc.printSync(match(from, to, module));
|
||||||
|
};
|
||||||
|
|
||||||
|
const match = (from: any, to: any, module: swc.Script): swc.Script => {
|
||||||
|
console.log(to);
|
||||||
|
console.log(module);
|
||||||
|
console.log(from);
|
||||||
|
|
||||||
|
for (const obj of module.body) {
|
||||||
|
let allPresent = true;
|
||||||
|
for (const key in obj) {
|
||||||
|
if (!(key in from)) {
|
||||||
|
allPresent = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (allPresent) {
|
||||||
|
console.log("Found first match!");
|
||||||
|
for (const [key, val] of Object.entries(obj)) {
|
||||||
|
match(from["key"], to, val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return module;
|
||||||
|
};
|
||||||
|
|
||||||
|
const matchAndReplace = (
|
||||||
|
statement: swc.Statement,
|
||||||
|
from: Object,
|
||||||
|
to: Object
|
||||||
|
) => {
|
||||||
|
for (const [key, value] of Object.entries(from)) {
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
main();
|
76
src/patterns/patterns.ts
Normal file
76
src/patterns/patterns.ts
Normal file
|
@ -0,0 +1,76 @@
|
||||||
|
import { Statement, VariableDeclarator } from "@swc/types";
|
||||||
|
import { MatchStatement } from "../types";
|
||||||
|
|
||||||
|
export const from: MatchStatement = {
|
||||||
|
type: "VariableDeclaration",
|
||||||
|
span: {
|
||||||
|
start: 1,
|
||||||
|
end: 13,
|
||||||
|
ctxt: 0,
|
||||||
|
},
|
||||||
|
kind: "var",
|
||||||
|
declare: false,
|
||||||
|
declarations: [
|
||||||
|
{
|
||||||
|
type: "VariableDeclarator",
|
||||||
|
span: {
|
||||||
|
start: 5,
|
||||||
|
end: 12,
|
||||||
|
ctxt: 0,
|
||||||
|
},
|
||||||
|
id: {
|
||||||
|
type: "Identifier",
|
||||||
|
span: {
|
||||||
|
start: 5,
|
||||||
|
end: 6,
|
||||||
|
ctxt: 2,
|
||||||
|
},
|
||||||
|
value: "a",
|
||||||
|
optional: false,
|
||||||
|
},
|
||||||
|
init: {
|
||||||
|
type: "NumericLiteral",
|
||||||
|
span: {
|
||||||
|
start: 9,
|
||||||
|
end: 12,
|
||||||
|
ctxt: 0,
|
||||||
|
},
|
||||||
|
value: 100,
|
||||||
|
raw: "100",
|
||||||
|
},
|
||||||
|
definite: false,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
export const to: VariableDeclarator[] = [
|
||||||
|
{
|
||||||
|
type: "VariableDeclarator",
|
||||||
|
span: {
|
||||||
|
start: 5,
|
||||||
|
end: 12,
|
||||||
|
ctxt: 0,
|
||||||
|
},
|
||||||
|
id: {
|
||||||
|
type: "Identifier",
|
||||||
|
span: {
|
||||||
|
start: 5,
|
||||||
|
end: 6,
|
||||||
|
ctxt: 2,
|
||||||
|
},
|
||||||
|
value: "a",
|
||||||
|
optional: false,
|
||||||
|
},
|
||||||
|
init: {
|
||||||
|
type: "NumericLiteral",
|
||||||
|
span: {
|
||||||
|
start: 9,
|
||||||
|
end: 12,
|
||||||
|
ctxt: 0,
|
||||||
|
},
|
||||||
|
value: 100,
|
||||||
|
raw: "100",
|
||||||
|
},
|
||||||
|
definite: false,
|
||||||
|
},
|
||||||
|
];
|
1
src/test_files/simple.js
Normal file
1
src/test_files/simple.js
Normal file
|
@ -0,0 +1 @@
|
||||||
|
var a = 100 + 100;
|
105
src/types.ts
Normal file
105
src/types.ts
Normal file
|
@ -0,0 +1,105 @@
|
||||||
|
import {
|
||||||
|
BinaryExpression,
|
||||||
|
BlockStatement,
|
||||||
|
BreakStatement,
|
||||||
|
ClassDeclaration,
|
||||||
|
ContinueStatement,
|
||||||
|
DebuggerStatement,
|
||||||
|
Declaration,
|
||||||
|
DoWhileStatement,
|
||||||
|
EmptyStatement,
|
||||||
|
Expression,
|
||||||
|
ExpressionStatement,
|
||||||
|
ForInStatement,
|
||||||
|
ForOfStatement,
|
||||||
|
ForStatement,
|
||||||
|
FunctionDeclaration,
|
||||||
|
IfStatement,
|
||||||
|
LabeledStatement,
|
||||||
|
ReturnStatement,
|
||||||
|
Script,
|
||||||
|
SwitchStatement,
|
||||||
|
ThrowStatement,
|
||||||
|
TryStatement,
|
||||||
|
VariableDeclaration,
|
||||||
|
WhileStatement,
|
||||||
|
WithStatement,
|
||||||
|
} from "@swc/core";
|
||||||
|
|
||||||
|
export type MatchStatement =
|
||||||
|
| MatchBlockSatement
|
||||||
|
| MatchEmptyStatement
|
||||||
|
| MatchDebuggerStatement
|
||||||
|
| MatchWithStatement
|
||||||
|
| MatchReturnStatement
|
||||||
|
| MatchLabeledStatement
|
||||||
|
| MatchBreakStatement
|
||||||
|
| MatchContinueStatement
|
||||||
|
| matchIfStatement
|
||||||
|
| MatchSwitchStatement
|
||||||
|
| MatchThrowStatement
|
||||||
|
| MatchTryStatement
|
||||||
|
| MatchWhileStatement
|
||||||
|
| MatchDoWhileStatement
|
||||||
|
| MatchForInStatement
|
||||||
|
| MatchForStatement
|
||||||
|
| MatchForOfStatement
|
||||||
|
| MatchDeclaration
|
||||||
|
| MatchExpressionStatement;
|
||||||
|
|
||||||
|
type MatchDeclaration =
|
||||||
|
| MatchClassDeclaration
|
||||||
|
| MatchFunctionDeclaration
|
||||||
|
| MatchVariableDeclaration;
|
||||||
|
|
||||||
|
export enum Transformation {
|
||||||
|
ANYTHING,
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface MatchScript extends Script {}
|
||||||
|
|
||||||
|
export interface MatchBlockSatement extends BlockStatement {}
|
||||||
|
|
||||||
|
export interface MatchEmptyStatement extends EmptyStatement {}
|
||||||
|
|
||||||
|
export interface MatchDebuggerStatement extends DebuggerStatement {}
|
||||||
|
|
||||||
|
export interface MatchWithStatement extends WithStatement {}
|
||||||
|
|
||||||
|
export interface MatchReturnStatement extends ReturnStatement {}
|
||||||
|
|
||||||
|
export interface MatchLabeledStatement extends LabeledStatement {}
|
||||||
|
|
||||||
|
export interface MatchBreakStatement extends BreakStatement {}
|
||||||
|
|
||||||
|
export interface MatchContinueStatement extends ContinueStatement {}
|
||||||
|
|
||||||
|
export interface MatchSwitchStatement extends SwitchStatement {}
|
||||||
|
|
||||||
|
export interface MatchThrowStatement extends ThrowStatement {}
|
||||||
|
|
||||||
|
export interface MatchTryStatement extends TryStatement {}
|
||||||
|
|
||||||
|
export interface MatchWhileStatement extends WhileStatement {}
|
||||||
|
|
||||||
|
export interface MatchDoWhileStatement extends DoWhileStatement {}
|
||||||
|
|
||||||
|
export interface MatchForStatement extends ForStatement {}
|
||||||
|
|
||||||
|
export interface MatchForInStatement extends ForInStatement {}
|
||||||
|
|
||||||
|
export interface MatchForOfStatement extends ForOfStatement {}
|
||||||
|
|
||||||
|
export interface MatchExpressionStatement extends ExpressionStatement {}
|
||||||
|
|
||||||
|
export interface matchIfStatement extends IfStatement {}
|
||||||
|
|
||||||
|
export interface matchBinaryExpression extends BlockStatement {
|
||||||
|
__stmts: Transformation;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface MatchClassDeclaration extends ClassDeclaration {}
|
||||||
|
|
||||||
|
export interface MatchFunctionDeclaration extends FunctionDeclaration {}
|
||||||
|
|
||||||
|
export interface MatchVariableDeclaration extends VariableDeclaration {}
|
13
tsconfig.json
Normal file
13
tsconfig.json
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"rootDir": "src",
|
||||||
|
"outDir": "dist",
|
||||||
|
"strict": true,
|
||||||
|
"target": "es6",
|
||||||
|
"module": "commonjs",
|
||||||
|
"sourceMap": true,
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"moduleResolution": "node",
|
||||||
|
"types": ["bun-types"]
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue