From 8024b12cadb0c3ecb59e5a9bd52692216261bed1 Mon Sep 17 00:00:00 2001 From: mistlog Date: Thu, 25 Jun 2020 15:49:41 +0800 Subject: [PATCH 1/4] use dsl match: instanceof --- package-lock.json | 24 +++------------ package.json | 2 +- src/plug-in/draft-plugin-refresh.tsx | 44 +++++++++++++++------------- typedraft.config.js | 5 ++++ 4 files changed, 33 insertions(+), 42 deletions(-) create mode 100644 typedraft.config.js diff --git a/package-lock.json b/package-lock.json index 5b218ec..7637e0e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2080,28 +2080,12 @@ } }, "draft-dsl-match": { - "version": "0.0.3", - "resolved": "https://registry.npmjs.org/draft-dsl-match/-/draft-dsl-match-0.0.3.tgz", - "integrity": "sha512-eMkSrvLLQr/KfbBQtVKii0CsG88mB0Wiy9KR4WJWfaCMVWeRmtSnqkLxv6AM5Asil8NkJ0gKPEHcQy5CBx6Xpw==", + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/draft-dsl-match/-/draft-dsl-match-0.1.0.tgz", + "integrity": "sha512-Fg6gDsN+eX7TOICiu6jPjoS5MV8jKYfkcwPkLqiCCm8M1h4XT2FEVqpKexFKMFezanArJItNa6au+AfqgPS0TA==", "dev": true, "requires": { - "typedraft": "^0.1.10" - }, - "dependencies": { - "typedraft": { - "version": "0.1.13", - "resolved": "https://registry.npmjs.org/typedraft/-/typedraft-0.1.13.tgz", - "integrity": "sha512-PWf/IVt8D40uvLUFhEDK/llBr0EL3ncAOutPQZYCc9dE7TX48AVUUItaRghuoNR/koF5O6VMlyWCyKQidXMgMw==", - "dev": true, - "requires": { - "@babel/core": "^7.6.2", - "commander": "^4.0.1", - "filewalker": "^0.1.3", - "fs-extra": "^8.1.0", - "node-watch": "^0.6.3", - "toposort": "2.0.2" - } - } + "typedraft": "0.2.0" } }, "duplexify": { diff --git a/package.json b/package.json index 5be07ce..d8d6de6 100644 --- a/package.json +++ b/package.json @@ -59,7 +59,7 @@ "@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", diff --git a/src/plug-in/draft-plugin-refresh.tsx b/src/plug-in/draft-plugin-refresh.tsx index 6455c22..4bbd3f8 100644 --- a/src/plug-in/draft-plugin-refresh.tsx +++ b/src/plug-in/draft-plugin-refresh.tsx @@ -1,17 +1,15 @@ -import { IPlugin, ITranscriber } from "../core/transcriber"; +import { ITranscriber } from "../core/transcriber"; import { ExportClassCode } from "../code-object/export-class"; import { MethodCode } from "../code-object/method"; import { LocalContext } from "../code-object/local-context"; import { InlineContext } from "../code-object/inline-context"; -export class RefreshDraftPlugin implements IPlugin { +export class RefreshDraftPlugin { m_Transcriber: ITranscriber; +} - constructor(transcriber: ITranscriber) { - this.m_Transcriber = transcriber; - } - - Transcribe() { + + + function Transcribe(this: RefreshDraftPlugin) { /** * clear */ @@ -25,19 +23,23 @@ export class RefreshDraftPlugin implements IPlugin { */ const draft = this.m_Transcriber.m_Module.ToDraft(); draft.forEach(each => { - if (each instanceof ExportClassCode) { - this.m_Transcriber.m_ClassMap.set(each.m_Name, each); - } else if (each instanceof MethodCode) { - const class_name = each.m_ClassName; - const methods = this.m_Transcriber.m_MethodMap.get(class_name); - methods - ? methods.push(each) - : this.m_Transcriber.m_MethodMap.set(class_name, [each]); - } else if (each instanceof LocalContext) { - this.m_Transcriber.m_ContextMap.set(each.m_Name, each); - } else if (each instanceof InlineContext) { - this.m_Transcriber.m_InlineContextMap.set(each.m_ID, each); + { + "use match"; + (each: ExportClassCode) => this.m_Transcriber.m_ClassMap.set(each.m_Name, each); + (each: LocalContext) => this.m_Transcriber.m_ContextMap.set(each.m_Name, each); + (each: InlineContext) => this.m_Transcriber.m_InlineContextMap.set(each.m_ID, each); + (each: MethodCode) => { + const class_name = each.m_ClassName; + const methods = this.m_Transcriber.m_MethodMap.get(class_name); + methods + ? methods.push(each) + : this.m_Transcriber.m_MethodMap.set(class_name, [each]); + }; } }); - } -} + }; + + + + function constructor(transcriber: ITranscriber) { + this.m_Transcriber = transcriber; + }; diff --git a/typedraft.config.js b/typedraft.config.js new file mode 100644 index 0000000..12274db --- /dev/null +++ b/typedraft.config.js @@ -0,0 +1,5 @@ +const { PatternMatch } = require("draft-dsl-match"); + +module.exports = { + DSLs: [{ name: "match", dsl: () => new PatternMatch(true) }], +}; From 220c7dfb10147749a824504b5d4c8b4459292b56 Mon Sep 17 00:00:00 2001 From: mistlog Date: Fri, 26 Jun 2020 17:57:38 +0800 Subject: [PATCH 2/4] refine code and add test case --- src/code-object/inline-context.tsx | 8 ++++---- src/code-object/local-context.tsx | 2 +- src/code-object/method.tsx | 2 +- src/code-object/module.tsx | 18 +++++++++--------- src/plug-in/draft-plugin-class.tsx | 13 +++++-------- src/plug-in/draft-plugin-dsl.tsx | 21 ++++++--------------- src/plug-in/draft-plugin-filter.tsx | 9 ++------- src/plug-in/draft-plugin-local-context.tsx | 9 +++++---- src/plug-in/draft-plugin-refresh.tsx | 6 ++---- test/code-object/inline-context.test.ts | 15 +++++++++++++-- test/code-object/local-context.test.ts | 8 ++++---- 11 files changed, 52 insertions(+), 59 deletions(-) diff --git a/src/code-object/inline-context.tsx b/src/code-object/inline-context.tsx index 3b1b197..2b7db54 100644 --- a/src/code-object/inline-context.tsx +++ b/src/code-object/inline-context.tsx @@ -30,13 +30,13 @@ export class 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; }; @@ -44,6 +44,6 @@ export interface IInlineContext { ToStatements: () => Array; } -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"; diff --git a/src/code-object/local-context.tsx b/src/code-object/local-context.tsx index 3843aef..1662904 100644 --- a/src/code-object/local-context.tsx +++ b/src/code-object/local-context.tsx @@ -36,7 +36,7 @@ export class 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(" "); diff --git a/src/code-object/method.tsx b/src/code-object/method.tsx index 5ed73f3..66da45d 100644 --- a/src/code-object/method.tsx +++ b/src/code-object/method.tsx @@ -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; diff --git a/src/code-object/module.tsx b/src/code-object/module.tsx index d274486..a8d1d63 100644 --- a/src/code-object/module.tsx +++ b/src/code-object/module.tsx @@ -102,15 +102,15 @@ export function IsLocalContext(path: NodePath): path is NodePath { - 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; } diff --git a/src/plug-in/draft-plugin-class.tsx b/src/plug-in/draft-plugin-class.tsx index 5e777eb..473a246 100644 --- a/src/plug-in/draft-plugin-class.tsx +++ b/src/plug-in/draft-plugin-class.tsx @@ -11,12 +11,9 @@ export class 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()) + ) + ); }; diff --git a/src/plug-in/draft-plugin-dsl.tsx b/src/plug-in/draft-plugin-dsl.tsx index 4eddc13..73ecf13 100644 --- a/src/plug-in/draft-plugin-dsl.tsx +++ b/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; @@ -11,19 +13,8 @@ export class DSLPlugin { + function Transcribe(this: DSLPlugin) { - this.m_Transcriber.TraverseInlineContext(context => { - const context_name = context.GetContextName(); - const dsl = this.m_Transcriber.GetDSL(context_name); - 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); - } - }); + const ResolveDSL = (context: InlineContext | LocalContext) => + context.Resolve(this.m_Transcriber.GetDSL(context.GetDSLName())); + this.m_Transcriber.TraverseInlineContext(ResolveDSL); + this.m_Transcriber.TraverseLocalContext(ResolveDSL); }; diff --git a/src/plug-in/draft-plugin-filter.tsx b/src/plug-in/draft-plugin-filter.tsx index 093745b..3f7c34b 100644 --- a/src/plug-in/draft-plugin-filter.tsx +++ b/src/plug-in/draft-plugin-filter.tsx @@ -11,11 +11,6 @@ export class FilterPlugin { + function Transcribe(this: FilterPlugin) { - this.m_Transcriber.TraverseLocalContext(context => { - context.m_Path.remove(); - }); - - this.m_Transcriber.TraverseMethod(methods => { - methods.forEach(each => each.m_Path.remove()); - }); + this.m_Transcriber.TraverseLocalContext(context => context.m_Path.remove()); + this.m_Transcriber.TraverseMethod(methods => methods.forEach(each => each.m_Path.remove())); }; diff --git a/src/plug-in/draft-plugin-local-context.tsx b/src/plug-in/draft-plugin-local-context.tsx index abccf72..2879f84 100644 --- a/src/plug-in/draft-plugin-local-context.tsx +++ b/src/plug-in/draft-plugin-local-context.tsx @@ -22,10 +22,11 @@ As context can be nested, we will translate them in order. Then find references // find and replace to_transcribe.forEach(name => { const context = this.m_Transcriber.GetLocalContext(name); - context.m_Refs.forEach(path => { - const parent = path.findParent(path => path.isExpressionStatement()); - parent.replaceWithMultiple(context.ToStatements()); - }); + context.m_Refs.forEach(path => + path + .findParent(path => path.isExpressionStatement()) + .replaceWithMultiple(context.ToStatements()) + ); }); }; diff --git a/src/plug-in/draft-plugin-refresh.tsx b/src/plug-in/draft-plugin-refresh.tsx index 4bbd3f8..d9d4e5c 100644 --- a/src/plug-in/draft-plugin-refresh.tsx +++ b/src/plug-in/draft-plugin-refresh.tsx @@ -30,10 +30,8 @@ export class RefreshDraftPlugin { (each: InlineContext) => this.m_Transcriber.m_InlineContextMap.set(each.m_ID, each); (each: MethodCode) => { const class_name = each.m_ClassName; - const methods = this.m_Transcriber.m_MethodMap.get(class_name); - methods - ? methods.push(each) - : this.m_Transcriber.m_MethodMap.set(class_name, [each]); + const methods = this.m_Transcriber.m_MethodMap.get(class_name) ?? []; + this.m_Transcriber.m_MethodMap.set(class_name, [...methods, each]); }; } }); diff --git a/test/code-object/inline-context.test.ts b/test/code-object/inline-context.test.ts index 47cf339..791d0a3 100644 --- a/test/code-object/inline-context.test.ts +++ b/test/code-object/inline-context.test.ts @@ -1,7 +1,7 @@ import { ToNodePath, ToAst, InlineContext } from "../../src"; import { Statement } from "@babel/types"; -test("get context name", () => { +test("get dsl name", () => { const context = new InlineContext( ToNodePath(` { @@ -10,7 +10,18 @@ test("get context name", () => { } `) ); - expect(context.GetContextName()).toEqual("foo"); + expect(context.GetDSLName()).toEqual("foo"); +}); + +test("get dsl name: empty", () => { + const context = new InlineContext( + ToNodePath(` + { + console.log("previous"); + } + `) + ); + expect(context.GetDSLName()).toEqual(""); }); test("to statement", () => { diff --git a/test/code-object/local-context.test.ts b/test/code-object/local-context.test.ts index 6f606c3..7d0c40a 100644 --- a/test/code-object/local-context.test.ts +++ b/test/code-object/local-context.test.ts @@ -21,17 +21,17 @@ describe("local context", () => { expect(context.ToStatements()).toEqual(expected); }); - test("get context name: empty", () => { + test("get dsl name: empty", () => { const context = new LocalContext( ToBinding(` function Test(value:number){ } `) ); - expect(context.GetContextName()).toEqual(""); + expect(context.GetDSLName()).toEqual(""); }); - test("get context name", () => { + test("get dsl name", () => { const context = new LocalContext( ToBinding(` function Test(value:number){ @@ -39,6 +39,6 @@ describe("local context", () => { } `) ); - expect(context.GetContextName()).toEqual("match"); + expect(context.GetDSLName()).toEqual("match"); }); }); From 08f55dd7e37aa393466aef7fe9e0081c2c468461 Mon Sep 17 00:00:00 2001 From: mistlog Date: Sat, 11 Jul 2020 12:14:12 +0800 Subject: [PATCH 3/4] support config file in typescript --- cli/cli.ts | 33 +++++++++++++++++++++------------ package-lock.json | 46 ++++++++++++++++++++++++++-------------------- package.json | 7 ++++--- 3 files changed, 51 insertions(+), 35 deletions(-) diff --git a/cli/cli.ts b/cli/cli.ts index 154721c..7f28bb8 100644 --- a/cli/cli.ts +++ b/cli/cli.ts @@ -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); @@ -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); + } + }); } } diff --git a/package-lock.json b/package-lock.json index 7637e0e..ce129b6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -221,6 +221,17 @@ "minimist": "^1.2.0" } }, + "@endemolshinegroup/cosmiconfig-typescript-loader": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/@endemolshinegroup/cosmiconfig-typescript-loader/-/cosmiconfig-typescript-loader-3.0.0.tgz", + "integrity": "sha512-Zvt4SaczUOoYH+x3IaPh82CMkm2S6ek5H1DZIjT87lIStJTz1dozC/0IvmvhljV3V8hHDhgdxj7G0XEGwaQpMg==", + "requires": { + "lodash.get": "^4", + "make-error": "^1", + "ts-node": "^8", + "tslib": "^1" + } + }, "@fortawesome/fontawesome-free": { "version": "5.13.0", "resolved": "https://registry.npmjs.org/@fortawesome/fontawesome-free/-/fontawesome-free-5.13.0.tgz", @@ -839,8 +850,7 @@ "arg": { "version": "4.1.3", "resolved": "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz", - "integrity": "sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==", - "dev": true + "integrity": "sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==" }, "argparse": { "version": "1.0.10", @@ -1373,8 +1383,7 @@ "buffer-from": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.1.tgz", - "integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==", - "dev": true + "integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==" }, "buffer-xor": { "version": "1.0.3", @@ -2036,8 +2045,7 @@ "diff": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz", - "integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==", - "dev": true + "integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==" }, "diff-sequences": { "version": "24.9.0", @@ -4238,6 +4246,11 @@ "integrity": "sha1-3bG7s+8HRYwBd7oH3hRCLLAz/5s=", "dev": true }, + "lodash.get": { + "version": "4.4.2", + "resolved": "https://registry.npmjs.org/lodash.get/-/lodash.get-4.4.2.tgz", + "integrity": "sha1-LRd/ZS+jHpObRDjVNBSZ36OCXpk=" + }, "lodash.kebabcase": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/lodash.kebabcase/-/lodash.kebabcase-4.0.1.tgz", @@ -4305,8 +4318,7 @@ "make-error": { "version": "1.3.6", "resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.6.tgz", - "integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==", - "dev": true + "integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==" }, "makeerror": { "version": "1.0.11", @@ -6082,7 +6094,6 @@ "version": "0.5.19", "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.19.tgz", "integrity": "sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw==", - "dev": true, "requires": { "buffer-from": "^1.0.0", "source-map": "^0.6.0" @@ -6091,8 +6102,7 @@ "source-map": { "version": "0.6.1", "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", - "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", - "dev": true + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==" } } }, @@ -6671,7 +6681,6 @@ "version": "8.10.1", "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-8.10.1.tgz", "integrity": "sha512-bdNz1L4ekHiJul6SHtZWs1ujEKERJnHs4HxN7rjTyyVOFf3HaJ6sLqe6aPG62XTzAB/63pKRh5jTSWL0D7bsvw==", - "dev": true, "requires": { "arg": "^4.1.0", "diff": "^4.0.1", @@ -6683,8 +6692,7 @@ "tslib": { "version": "1.13.0", "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.13.0.tgz", - "integrity": "sha512-i/6DQjL8Xf3be4K/E6Wgpekn5Qasl1usyw++dAA35Ue5orEn65VIxOA+YvNNl9HV3qv70T7CNwjODHZrLwvd1Q==", - "dev": true + "integrity": "sha512-i/6DQjL8Xf3be4K/E6Wgpekn5Qasl1usyw++dAA35Ue5orEn65VIxOA+YvNNl9HV3qv70T7CNwjODHZrLwvd1Q==" }, "tty-browserify": { "version": "0.0.0", @@ -6738,10 +6746,9 @@ } }, "typescript": { - "version": "3.9.3", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.9.3.tgz", - "integrity": "sha512-D/wqnB2xzNFIcoBG9FG8cXRDjiqSTbG2wd8DMZeQyJlP1vfTkIxH4GKveWaEBYySKIg+USu+E+EDIR47SqnaMQ==", - "dev": true + "version": "3.8.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.8.3.tgz", + "integrity": "sha512-MYlEfn5VrLNsgudQTVJeNaQFUAI7DkhnOjdpAp4T+ku1TfQClewlbSuTVHiA+8skNBgaf02TL/kLOvig4y3G8w==" }, "uc.micro": { "version": "1.0.6", @@ -7333,8 +7340,7 @@ "yn": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/yn/-/yn-3.1.1.tgz", - "integrity": "sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==", - "dev": true + "integrity": "sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==" } } } diff --git a/package.json b/package.json index d8d6de6..0b031a9 100644 --- a/package.json +++ b/package.json @@ -47,12 +47,14 @@ "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", @@ -67,8 +69,7 @@ "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.0" }, "husky": { "hooks": { From 0b64099562feb717001babf27f4f18a7e61f66de Mon Sep 17 00:00:00 2001 From: mistlog Date: Sat, 11 Jul 2020 15:39:48 +0800 Subject: [PATCH 4/4] release 0.2.2 1. use config file in ts; 2. fix crash when dsl is empty --- package-lock.json | 29 +++++++++++++++++---- package.json | 4 +-- src/plug-in/draft-plugin-dsl.tsx | 11 ++++++-- test/plug-in/__snapshots__/dsl.test.ts.snap | 6 +++++ test/plug-in/dsl.test.ts | 16 ++++++++++++ typedraft.config.js | 5 ---- typedraft.config.ts | 5 ++++ 7 files changed, 62 insertions(+), 14 deletions(-) delete mode 100644 typedraft.config.js create mode 100644 typedraft.config.ts diff --git a/package-lock.json b/package-lock.json index ce129b6..945e193 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "typedraft", - "version": "0.2.0", + "version": "0.2.2", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -2094,6 +2094,23 @@ "dev": true, "requires": { "typedraft": "0.2.0" + }, + "dependencies": { + "typedraft": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/typedraft/-/typedraft-0.2.0.tgz", + "integrity": "sha512-hjVKfGGh4GzxWzTJkcd+pVVSMD8bat+yn0ItnGezY6GgTW8c15TUr2TPVkFw/lioebu98t6afss6JQOCuH+O5g==", + "dev": true, + "requires": { + "@babel/core": "^7.6.2", + "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" + } + } } }, "duplexify": { @@ -6731,18 +6748,20 @@ "dev": true }, "typedraft": { - "version": "0.2.0", - "resolved": "https://registry.npmjs.org/typedraft/-/typedraft-0.2.0.tgz", - "integrity": "sha512-hjVKfGGh4GzxWzTJkcd+pVVSMD8bat+yn0ItnGezY6GgTW8c15TUr2TPVkFw/lioebu98t6afss6JQOCuH+O5g==", + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/typedraft/-/typedraft-0.2.2.tgz", + "integrity": "sha512-HI+Z8PlVfKvjCcMqpLeZxjvVtgJqFyIpBZ88oZ2tVvDtuWpB6k0MKtuSr+p0gwaXDjGcrL6a4D21FtFR48tlCw==", "dev": true, "requires": { "@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" } }, "typescript": { diff --git a/package.json b/package.json index 0b031a9..3528ec2 100644 --- a/package.json +++ b/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", @@ -69,7 +69,7 @@ "pretty-quick": "^2.0.1", "ts-jest": "^24.0.2", "ts-node": "^8.3.0", - "typedraft": "0.2.0" + "typedraft": "0.2.2" }, "husky": { "hooks": { diff --git a/src/plug-in/draft-plugin-dsl.tsx b/src/plug-in/draft-plugin-dsl.tsx index 73ecf13..590ea75 100644 --- a/src/plug-in/draft-plugin-dsl.tsx +++ b/src/plug-in/draft-plugin-dsl.tsx @@ -13,8 +13,15 @@ export class DSLPlugin { + function Transcribe(this: DSLPlugin) { - const ResolveDSL = (context: InlineContext | LocalContext) => - context.Resolve(this.m_Transcriber.GetDSL(context.GetDSLName())); + 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.TraverseInlineContext(ResolveDSL); this.m_Transcriber.TraverseLocalContext(ResolveDSL); }; diff --git a/test/plug-in/__snapshots__/dsl.test.ts.snap b/test/plug-in/__snapshots__/dsl.test.ts.snap index 94a13a0..f7a2e18 100644 --- a/test/plug-in/__snapshots__/dsl.test.ts.snap +++ b/test/plug-in/__snapshots__/dsl.test.ts.snap @@ -38,6 +38,12 @@ exports[`nested dsl 1`] = ` }" `; +exports[`no dsl 1`] = ` +"export function Main() { + console.log(\\"previous\\"); +}" +`; + exports[`simple 1`] = ` "export function Main() { console.log(\\"current\\"); diff --git a/test/plug-in/dsl.test.ts b/test/plug-in/dsl.test.ts index 0fc8191..6efbdd3 100644 --- a/test/plug-in/dsl.test.ts +++ b/test/plug-in/dsl.test.ts @@ -43,6 +43,22 @@ test("simple", () => { expect(result).toMatchSnapshot(); }); +test("no dsl", () => { + // + const code = ` + export function Main(){ + ; + } + + function Test(){ + console.log("previous"); + } + `; + const transcriber = MakeDefaultTranscriber(code); + const result = transcriber.Transcribe(); + expect(result).toMatchSnapshot(); +}); + /** * is not used as local context, but after DSL "match" resolved, * it will be used as local context, diff --git a/typedraft.config.js b/typedraft.config.js deleted file mode 100644 index 12274db..0000000 --- a/typedraft.config.js +++ /dev/null @@ -1,5 +0,0 @@ -const { PatternMatch } = require("draft-dsl-match"); - -module.exports = { - DSLs: [{ name: "match", dsl: () => new PatternMatch(true) }], -}; diff --git a/typedraft.config.ts b/typedraft.config.ts new file mode 100644 index 0000000..df828f9 --- /dev/null +++ b/typedraft.config.ts @@ -0,0 +1,5 @@ +import { PatternMatch } from "draft-dsl-match"; + +export default { + DSLs: [{ name: "match", dsl: () => new PatternMatch(true) }], +};