Skip to content

Commit cdb4dd6

Browse files
committed
Use transform classes
1 parent 024d9de commit cdb4dd6

File tree

6 files changed

+60
-12
lines changed

6 files changed

+60
-12
lines changed

cli/asc.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -212,15 +212,27 @@ exports.main = function main(argv, options, callback) {
212212
// Set up transforms
213213
const transforms = [];
214214
if (args.transform) {
215-
args.transform.forEach(transform =>
216-
transforms.push(
217-
require(
218-
path.isAbsolute(transform = transform.trim())
219-
? transform
220-
: path.join(process.cwd(), transform)
221-
)
222-
)
223-
);
215+
class Transform {
216+
baseDir = baseDir;
217+
writeFile = writeFile;
218+
readFile = readFile;
219+
listFiles = listFiles;
220+
}
221+
args.transform.forEach(filename => {
222+
const ctor = require(
223+
path.isAbsolute(filename = filename.trim())
224+
? filename
225+
: path.join(process.cwd(), filename)
226+
);
227+
let impl;
228+
if (typeof ctor === "function") {
229+
Object.setPrototypeOf(ctor, Transform);
230+
impl = new ctor();
231+
} else {
232+
impl = ctor; // legacy module
233+
}
234+
transforms.push(impl);
235+
});
224236
}
225237
function applyTransform(name, ...args) {
226238
transforms.forEach(transform => {

cli/transform.d.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,22 @@
33
* @module cli/transform
44
*//***/
55

6-
import { Parser } from "../src/parser";
6+
import { Parser } from "..";
7+
8+
export abstract class Transform {
9+
10+
/** Base directory. */
11+
readonly baseDir: string;
12+
13+
/** Writes a file to disk. */
14+
writeFile(filename: string, contents: string | Uint8Array, baseDir: string): boolean;
15+
16+
/** Reads a file from disk. */
17+
readFile(filename: string, baseDir: string): string | null;
18+
19+
/** Lists all files in a directory. */
20+
listFiles(dirname: string, baseDir: string): string[] | null;
721

8-
export interface Transform {
922
/** Called when parsing is complete, before a program is instantiated from the AST. */
10-
afterParse(parser: Parser): void;
23+
afterParse?(parser: Parser): void;
1124
}

cli/transform.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Transform {
2+
// becomes replaced with the actual base by asc
3+
}
4+
5+
exports.Transform = Transform;

examples/transform/assembly/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// empty

examples/transform/mytransform.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const { Transform } = require("../../cli/transform"); // "assemblyscript/cli/transform"
2+
const { SourceKind } = require("../.."); // "assemblyscript"
3+
4+
class MyTransform extends Transform {
5+
afterParse(parser) {
6+
console.error("afterParse called, baseDir = " + this.baseDir);
7+
var sources = parser.program.sources;
8+
sources.forEach(source => console.error(" " + source.internalPath + " [" + SourceKind[source.sourceKind] + "]"));
9+
}
10+
}
11+
12+
module.exports = MyTransform;

examples/transform/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"scripts": {
3+
"test": "asc assembly/index.ts --runtime none --transform mytransform.js"
4+
}
5+
}

0 commit comments

Comments
 (0)