Skip to content

Commit

Permalink
Merge pull request #15 from mistlog/develop
Browse files Browse the repository at this point in the history
release 0.2.1 & 0.2.2
  • Loading branch information
mistlog committed Jul 11, 2020
2 parents 681beab + 0b64099 commit bda94c4
Show file tree
Hide file tree
Showing 17 changed files with 170 additions and 122 deletions.
33 changes: 21 additions & 12 deletions cli/cli.ts
Expand Up @@ -9,7 +9,8 @@ import {
} from "./literator";
import { resolve } from "path";
import { readJSONSync, lstatSync } from "fs-extra";
import { cosmiconfigSync } from "cosmiconfig";
import { cosmiconfig } from "cosmiconfig";
import { default as tsLoader } from "@endemolshinegroup/cosmiconfig-typescript-loader";

const package_json = readJSONSync(resolve(__dirname, "../../package.json"));
program.version(package_json.version);
Expand All @@ -27,17 +28,25 @@ if (args.length === 0) {
const path = resolve(working_directory, target);

// find config
const config_info = cosmiconfigSync("typedraft").search();
let config: ITypeDraftConfig = { DSLs: [], DraftPlugins: [] };
if (config_info && !config_info.isEmpty) {
config = { ...config, ...config_info.config };
}
const explorer = cosmiconfig("typedraft", {
searchPlaces: [`typedraft.config.ts`],
loaders: {
".ts": tsLoader,
},
});

//
if (lstatSync(path).isDirectory()) {
program.watch ? InspectDirectory(path, config) : ComposeDirectory(path, config);
} else {
program.watch ? InspectFile(path, config) : ComposeFile(path, config);
}
explorer.search().then(config_info => {
let config: ITypeDraftConfig = { DSLs: [], DraftPlugins: [] };
if (config_info && !config_info.isEmpty) {
config = { ...config, ...config_info.config };
}

//
if (lstatSync(path).isDirectory()) {
program.watch ? InspectDirectory(path, config) : ComposeDirectory(path, config);
} else {
program.watch ? InspectFile(path, config) : ComposeFile(path, config);
}
});
}
}
73 changes: 41 additions & 32 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "typedraft",
"version": "0.2.0",
"version": "0.2.2",
"description": "TypeDraft is a superset of typescript with built-in support for DSL extension and literate programming.",
"keywords": [
"literate programming",
Expand Down Expand Up @@ -47,28 +47,29 @@
"license": "MIT",
"dependencies": {
"@babel/core": "^7.6.2",
"@endemolshinegroup/cosmiconfig-typescript-loader": "^3.0.0",
"commander": "^4.0.1",
"cosmiconfig": "^6.0.0",
"filewalker": "^0.1.3",
"fs-extra": "^8.1.0",
"node-watch": "^0.6.3",
"toposort": "2.0.2"
"toposort": "2.0.2",
"typescript": "^3.8.3"
},
"devDependencies": {
"@types/fs-extra": "^8.0.1",
"@types/jest": "^24.0.18",
"@types/node": "^12.12.14",
"@types/toposort": "2.0.3",
"draft-dsl-match": "0.0.3",
"draft-dsl-match": "^0.1.0",
"husky": "^4.2.5",
"jest": "^24.9.0",
"litscript": "^1.1.6",
"prettier": "^2.0.5",
"pretty-quick": "^2.0.1",
"ts-jest": "^24.0.2",
"ts-node": "^8.3.0",
"typedraft": "0.2.0",
"typescript": "^3.8.3"
"typedraft": "0.2.2"
},
"husky": {
"hooks": {
Expand Down
8 changes: 4 additions & 4 deletions src/code-object/inline-context.tsx
Expand Up @@ -30,20 +30,20 @@ export class InlineContext {
};

<InlineContext /> +
function GetContextName(this: InlineContext) {
function GetDSLName(this: InlineContext) {
const statement = this.m_Path.node.body[0] as ExpressionStatement;
if (!statement) {
if (!statement || !isStringLiteral(statement.expression)) {
return "";
}

const [, dsl_name] = (statement.expression as StringLiteral).value.trim().split(" ");
const [, dsl_name] = statement.expression.value.trim().split(" ");
return dsl_name;
};

export interface IInlineContext {
ToStatements: () => Array<Statement>;
}

import { BlockStatement, StringLiteral, ExpressionStatement, Statement } from "@babel/types";
import { BlockStatement, ExpressionStatement, Statement, isStringLiteral } from "@babel/types";
import { NodePath } from "@babel/traverse";
import { IDSL } from "../core/transcriber";
2 changes: 1 addition & 1 deletion src/code-object/local-context.tsx
Expand Up @@ -36,7 +36,7 @@ export class LocalContext {
};

<LocalContext /> +
function GetContextName(this: LocalContext) {
function GetDSLName(this: LocalContext) {
const [directive] = this.m_Code.body.directives;
if (directive) {
const [, context_name] = directive.value.value.split(" ");
Expand Down
2 changes: 1 addition & 1 deletion src/code-object/method.tsx
Expand Up @@ -56,7 +56,7 @@ export class Foo {
* remove param "this":
*/
const params = raw_params.filter(param => isIdentifier(param) && param.name !== "this");
const kind = id.name === "constructor" ? "constructor" : "method";
const kind = id.name === "constructor" ? id.name : "method";

const class_method = classMethod(kind, id, params, body);
return class_method;
Expand Down
18 changes: 9 additions & 9 deletions src/code-object/module.tsx
Expand Up @@ -102,15 +102,15 @@ export function IsLocalContext(path: NodePath<any>): path is NodePath<FunctionDe
}

const [directive]: [string] = path.node.body.directives;
const has_context = Boolean(directive);

const name = path.node.id.name;
const binding = path.scope.parent.getBinding(name);
const is_local_context = binding.referencePaths.some(path => {
const used_as_jsx = path.parentPath?.parentPath?.isJSXElement();
const used_as_statement = path.parentPath?.parentPath?.parentPath?.isExpressionStatement();
return (has_context && used_as_jsx) || (used_as_jsx && used_as_statement);
});

const is_local_context = path.scope.parent
.getBinding(path.node.id.name)
.referencePaths.some(path => {
const used_as_jsx = path.parentPath?.parentPath?.isJSXElement();
const used_as_statement = path.parentPath?.parentPath?.parentPath?.isExpressionStatement();
const has_context = Boolean(directive);
return used_as_jsx && (has_context || used_as_statement);
});
return is_local_context;
}

Expand Down
13 changes: 5 additions & 8 deletions src/plug-in/draft-plugin-class.tsx
Expand Up @@ -11,12 +11,9 @@ export class ClassPlugin {

<ClassPlugin /> +
function Transcribe(this: ClassPlugin) {
const transcriber = this.m_Transcriber;

transcriber.TraverseMethod((methods, class_name) => {
const target_class = transcriber.GetClass(class_name);
if (target_class) {
methods.forEach(method => target_class.AddMember(method.ToClassMethod()));
}
});
this.m_Transcriber.TraverseMethod((methods, class_name) =>
methods.forEach(method =>
this.m_Transcriber.GetClass(class_name).AddMember(method.ToClassMethod())
)
);
};
22 changes: 10 additions & 12 deletions src/plug-in/draft-plugin-dsl.tsx
@@ -1,4 +1,6 @@
import { ITranscriber } from "../core/transcriber";
import { InlineContext } from "../code-object/inline-context";
import { LocalContext } from "../code-object/local-context";

export class DSLPlugin {
m_Transcriber: ITranscriber;
Expand All @@ -11,19 +13,15 @@ export class DSLPlugin {

<DSLPlugin /> +
function Transcribe(this: DSLPlugin) {
this.m_Transcriber.TraverseInlineContext(context => {
const context_name = context.GetContextName();
const dsl = this.m_Transcriber.GetDSL(context_name);
const ResolveDSL = (context: InlineContext | LocalContext) => {
const dsl = this.m_Transcriber.GetDSL(context.GetDSLName());
/**
* DSL name can be "" in local context, then dsl will be undefined
*/
if (dsl) {
context.Resolve(dsl);
}
});

this.m_Transcriber.TraverseLocalContext(context => {
const context_name = context.GetContextName();
const dsl = this.m_Transcriber.GetDSL(context_name);
if (dsl) {
context.Resolve(dsl);
}
});
};
this.m_Transcriber.TraverseInlineContext(ResolveDSL);
this.m_Transcriber.TraverseLocalContext(ResolveDSL);
};

0 comments on commit bda94c4

Please sign in to comment.