Skip to content

Commit

Permalink
Merge pull request #24 from mistlog/develop
Browse files Browse the repository at this point in the history
release 0.2.4
  • Loading branch information
mistlog committed Jul 24, 2020
2 parents d2a624b + eafb0f8 commit 4ce0fbf
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "typedraft",
"version": "0.2.3",
"version": "0.2.4",
"description": "TypeDraft is a superset of typescript with built-in support for DSL extension and literate programming.",
"keywords": [
"literate programming",
Expand Down
8 changes: 7 additions & 1 deletion src/code-object/inline-context.tsx
Expand Up @@ -44,6 +44,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";
5 changes: 4 additions & 1 deletion src/code-object/module.tsx
Expand Up @@ -35,7 +35,10 @@ export type Draft = Array<ExportClassCode | MethodCode | LocalContext | InlineCo
if (expression.isStringLiteral()) {
const literal = expression.node.value.trim();
if (literal.startsWith("use") && path.parentPath.isBlockStatement()) {
draft.push(new InlineContext(path.parentPath));
/**
* inline context should be resolved from deepest
*/
draft.unshift(new InlineContext(path.parentPath));
}
}
},
Expand Down
12 changes: 12 additions & 0 deletions test/plug-in/__snapshots__/dsl.test.ts.snap
Expand Up @@ -38,6 +38,18 @@ exports[`nested dsl 1`] = `
}"
`;

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

exports[`no dsl 1`] = `
"export function Main() {
console.log(\\"previous\\");
Expand Down
38 changes: 38 additions & 0 deletions test/plug-in/dsl.test.ts
Expand Up @@ -125,6 +125,44 @@ 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> {
const [use_watch, ...rest] = block;
return [labeledStatement(identifier("$"), blockStatement(rest))];
}
}

//
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 4ce0fbf

Please sign in to comment.