Skip to content

Commit

Permalink
refactor InlineContext.Resolve to support nested dsl merge
Browse files Browse the repository at this point in the history
  • Loading branch information
mistlog committed Jul 24, 2020
1 parent 0f61495 commit 287cc14
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/code-object/inline-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ export class InlineContext {

<InlineContext /> +
function Resolve(this: InlineContext & IInlineContext, dsl: IDSL) {
const resolved = dsl.Transcribe(this.ToStatements(), this.m_Path);
if (dsl.m_Merge) {
this.m_Path.replaceWithMultiple(dsl.Transcribe(this.ToStatements(), this.m_Path));
this.m_Path.insertAfter(resolved);
} else {
this.m_Code.body = dsl.Transcribe(this.ToStatements(), this.m_Path);
this.m_Path.insertAfter(blockStatement(resolved));
}
this.m_Path.remove();
};

<InlineContext /> +
Expand All @@ -44,6 +46,12 @@ export interface IInlineContext {
ToStatements: () => Array<Statement>;
}

import { BlockStatement, ExpressionStatement, Statement, isStringLiteral } from "@babel/types";
import {
BlockStatement,
ExpressionStatement,
Statement,
isStringLiteral,
blockStatement,
} from "@babel/types";
import { NodePath } from "@babel/traverse";
import { IDSL } from "../core/transcriber";
14 changes: 14 additions & 0 deletions test/plug-in/__snapshots__/dsl.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ exports[`nested dsl 1`] = `
}"
`;

exports[`nested dsl: merge 1`] = `
"export function Main() {
$: {
\\"use watch\\";
if (value === \\"a\\") {
console.log(\\"value is a\\");
}
b = value + 1;
}
}"
`;

exports[`no dsl 1`] = `
"export function Main() {
console.log(\\"previous\\");
Expand Down
37 changes: 37 additions & 0 deletions test/plug-in/dsl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,43 @@ test("nested dsl", () => {
expect(result).toMatchSnapshot();
});

test("nested dsl: merge", () => {
//
class Watch implements IDSL {
m_Merge: boolean;
constructor() {
this.m_Merge = true;
}
Transcribe(block: Array<Statement>): Array<Statement> {
return [labeledStatement(identifier("$"), blockStatement(block))];
}
}

//
const code = `
export function Main(){
{
"use watch";
{
"use match";
(value: "a") =>
{
console.log("value is a");
};
}
b = value + 1;
}
}
`;

const transcriber = MakeDefaultTranscriber(code);
transcriber.AddDSL("watch", new Watch());
transcriber.AddDSL("match", new PatternMatch(true));
const result = transcriber.Transcribe();
expect(result).toMatchSnapshot();
});

test("use path param", () => {
//
class Foo implements IDSL {
Expand Down

0 comments on commit 287cc14

Please sign in to comment.