Skip to content

Commit

Permalink
feat(util): add #wrapNode function
Browse files Browse the repository at this point in the history
#wrapNode use Tagged template strings and return ast
  • Loading branch information
azu committed Aug 24, 2019
1 parent b33d7fa commit 36baa81
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 28 deletions.
1 change: 1 addition & 0 deletions packages/comment-to-assert/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
],
"dependencies": {
"escodegen": "^1.6.1",
"espree": "^2.2.3",
"esprima": "^2.5.0",
"estraverse": "^4.1.0",
"esutils": "^2.0.2"
Expand Down
60 changes: 60 additions & 0 deletions packages/comment-to-assert/src/ast-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// LICENSE : MIT
"use strict";
import {parse} from "esprima"
import {generate} from "escodegen"
const commentCodeRegExp = /=>\s*?(.*?)$/i;
export function tryGetCodeFromComments(comments) {
if (comments.length === 0) {
return;
}
var comment = comments[0];
if (comment.type === "Line") {
var matchResult = comment.value.match(commentCodeRegExp);
}
if (matchResult && matchResult[1]) {
return matchResult[1];
}
}

function astToCode(expression) {
return generate({
"type": "Program",
"body": [
expression
]
}, {
format: {
indent: {
style: ' ',
base: 0,
adjustMultilineComment: false
},
newline: '\n',
space: ' ',
json: false,
renumber: false,
hexadecimal: false,
quotes: 'single',
escapeless: false,
compact: false,
parentheses: true,
semicolons: false,
safeConcatenation: false
}
});
}
function extractionBody(ast) {
return ast.body[0];
}
function tag(strings, ...values) {
var concatCode = strings.map((string, index) => {
var code = (values[index] ? astToCode(values[index]) : "");
return string + code;
}).join("");
var concatAST = parse(concatCode);
return extractionBody(concatAST);
}

export function wrapNode(node) {
return tag`assert(${node})`;
}
15 changes: 15 additions & 0 deletions packages/comment-to-assert/src/comment-to-assert.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
// LICENSE : MIT
"use strict";
import {parse} from "esprima"
import {generate} from "escodegen"
import estraverse from "estraverse"
import {tryGetCodeFromComments} from "./ast-utils"
export function commentToAssertFromCode(code) {
return code;
}
export function commentToAssertFromAST(ast) {
estraverse.traverse(ast, {
enter: function (node, parent) {
if (node.trailingComments) {
let commentExpression = tryGetCodeFromComments(node.trailingComments);
if (commentExpression) {
console.log(commentExpression);

}
}
}
});
return ast;
}
16 changes: 16 additions & 0 deletions packages/comment-to-assert/test/ast-utils-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// LICENSE : MIT
"use strict";
import assert from "power-assert"
import {wrapNode} from "../src/ast-utils"
import {parse} from "esprima"
import {astEqual} from "./lib/ast-equal"
describe("ast-utils", function () {
describe("#wrapNode", function () {
it("should return ast", function () {
var expressionNode = parse("1");
var results = wrapNode(expressionNode);
var expected = "assert(1)";
astEqual(results, expected);
});
});
});
45 changes: 17 additions & 28 deletions packages/comment-to-assert/test/comment-to-assert-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ import {
import {parse} from "esprima"
import {astEqual} from "./lib/ast-equal"

function parseToAST(code) {
var parseOption = {
loc: true,
range: true,
comment: true,
attachComment: true
};
return parse(code, parseOption);
}
describe("comment-to-assert", function () {
describe("#commentToAssertFromCode", function () {
it("should return code", function () {
Expand Down Expand Up @@ -33,59 +42,39 @@ describe("comment-to-assert", function () {
astEqual(result, AST);
});
it("should keep code mean", function () {
var AST = parse("var a = 1;// comment", {
loc: true,
range: true,
comment: true
});
var AST = parseToAST("var a = 1;// comment");
var result = commentToAssertFromAST(AST);
assert(typeof result === "object");
astEqual(result, AST);
});
});
describe("convert logic", function () {
it("could handle primitive number", function () {
var AST = parse(`var a = 1;
a;// => 1`, {
loc: true,
range: true,
comment: true
});
var AST = parseToAST(`var a = 1;
a;// => 1`);
var result = commentToAssertFromAST(AST);
var expected = `var a = 1;
assert.equal(a, 1);`;
astEqual(result, expected);
});
it("could handle string", function () {
var AST = parse(`var a = "str";
a;// => "str"`, {
loc: true,
range: true,
comment: true
});
var AST = parseToAST(`var a = "str";
a;// => "str"`);
var result = commentToAssertFromAST(AST);
var expected = `var a = "str";
assert.equal(a, "str");`;
astEqual(result, expected);
});
it("could handle object", function () {
var AST = parse(`var a = [1];
a;// => [1]`, {
loc: true,
range: true,
comment: true
});
var AST = parseToAST(`var a = [1];
a;// => [1]`);
var result = commentToAssertFromAST(AST);
var expected = `var a = [1];
assert.deepEqual(a, [1]);`;
astEqual(result, expected);
});
it("could handle Error", function () {
var AST = parse(`throw new Error("error");// => Error`, {
loc: true,
range: true,
comment: true
});
var AST = parseToAST(`throw new Error("error");// => Error`);
var result = commentToAssertFromAST(AST);
var expected = `assert.throws(function() {
throw new Error("error");
Expand Down

0 comments on commit 36baa81

Please sign in to comment.