-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#wrapNode use Tagged template strings and return ast
- Loading branch information
Showing
5 changed files
with
109 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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})`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters