Skip to content
This repository has been archived by the owner on Sep 9, 2019. It is now read-only.

Commit

Permalink
fix: array and directive string expression support
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Jan 19, 2019
1 parent 8c12db5 commit 4927f3c
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 14 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
},
"dependencies": {
"@babel/core": "^7.2.2",
"@babel/parser": "^7.2.3",
"@babel/template": "^7.2.2",
"@babel/traverse": "^7.2.3",
"@babel/types": "^7.2.2",
Expand Down
26 changes: 21 additions & 5 deletions src/ast-utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
// LICENSE : MIT
"use strict";
import * as assert from "assert";
import { CallExpression, Comment, isCallExpression, isIdentifier, isLiteral, isNullLiteral } from "@babel/types";
import {
CallExpression,
Comment,
isCallExpression,
isIdentifier,
isLiteral,
isNullLiteral,
isDirective
} from "@babel/types";
import template from "@babel/template";

const commentCodeRegExp = /=>\s*?(.*?)$/i;
Expand Down Expand Up @@ -85,10 +93,18 @@ export function wrapAssert(actualNode: any, expectedNode: any, options: wrapAsse
ACTUAL_NODE
});
} else if (isLiteral(expectedNode)) {
return template`assert.strictEqual(ACTUAL_NODE, EXPECTED_NODE)`({
ACTUAL_NODE,
EXPECTED_NODE
});
// Handle Directive Prorogue as string literal
if (isDirective(ACTUAL_NODE)) {
return template`assert.strictEqual(ACTUAL_NODE, EXPECTED_NODE)`({
ACTUAL_NODE: (ACTUAL_NODE.value as any).extra.raw,
EXPECTED_NODE
});
} else {
return template`assert.strictEqual(ACTUAL_NODE, EXPECTED_NODE)`({
ACTUAL_NODE,
EXPECTED_NODE
});
}
} else {
return template`assert.deepStrictEqual(ACTUAL_NODE, EXPECTED_NODE)`({
ACTUAL_NODE,
Expand Down
16 changes: 7 additions & 9 deletions src/comment-to-assert.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// LICENSE : MIT
"use strict";
import { ERROR_COMMENT_PATTERN, PROMISE_COMMENT_PATTERN, tryGetCodeFromComments, wrapAssert } from "./ast-utils";
import { parse, ParseResult, transformFromAstSync } from "@babel/core";
import { identifier, isExpressionStatement } from "@babel/types";
import * as template from "@babel/template";
import { transformFromAstSync } from "@babel/core";
import { identifier, isExpressionStatement, File } from "@babel/types";
import { parse, parseExpression, ParserOptions } from "@babel/parser";
import traverse from "@babel/traverse";

function getExpressionNodeFromCommentValue(string: string): { type: string } & { [index: string]: any } {
Expand All @@ -26,7 +26,7 @@ function getExpressionNodeFromCommentValue(string: string): { type: string } & {
};
}
try {
return template.expression(string)();
return parseExpression(string);
} catch (e) {
console.error(`Can't parse comments // => expression`);
throw e;
Expand All @@ -35,9 +35,7 @@ function getExpressionNodeFromCommentValue(string: string): { type: string } & {

export interface toAssertFromSourceOptions {
asyncCallbackName?: string;
babel?: {
plugins: string[];
};
babel?: ParserOptions;
}

/**
Expand All @@ -48,7 +46,7 @@ export function toAssertFromSource(code: string, options?: toAssertFromSourceOpt
const ast = parse(code, {
// parse in strict mode and allow module declarations
sourceType: "module",
plugins: (options && options.babel && options.babel.plugins) || []
...options && options.babel ? options.babel : {}
});
if (!ast) {
throw new Error("Can not parse the code");
Expand All @@ -74,7 +72,7 @@ export interface toAssertFromASTOptions {
/**
* transform AST to asserted AST.
*/
export function toAssertFromAST(ast: ParseResult, options: toAssertFromASTOptions = {}) {
export function toAssertFromAST(ast: File, options: toAssertFromASTOptions = {}) {
const replaceSet = new Set();
traverse(ast, {
exit(path) {
Expand Down
6 changes: 6 additions & 0 deletions test/snapshots/Array-strings/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const array = ["A", "B", "C"];
array.unshift("S"); // "S"を先頭に追加
console.log(array); // => ["S", "A", "B", "C"]
const shiftedItem = array.shift(); // 先頭の要素を削除
console.log(shiftedItem); // => "S"
console.log(array); // => ["A", "B", "C"]
10 changes: 10 additions & 0 deletions test/snapshots/Array-strings/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const array = ["A", "B", "C"];
array.unshift("S"); // "S"を先頭に追加

assert.deepStrictEqual(array, ["S", "A", "B", "C"]); // => ["S", "A", "B", "C"]

const shiftedItem = array.shift(); // 先頭の要素を削除

assert.strictEqual(shiftedItem, "S"); // => "S"

assert.deepStrictEqual(array, ["A", "B", "C"]); // => ["A", "B", "C"]
3 changes: 3 additions & 0 deletions test/snapshots/Number-String/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
console.log(0b0000000000000000000000000001001); // => 9
// Number#toStringメソッドを使うことで2進数表記の文字列を取得できる
console.log((9).toString(2)); // => "1001"
4 changes: 4 additions & 0 deletions test/snapshots/Number-String/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
assert.strictEqual(0b0000000000000000000000000001001, 9); // => 9
// Number#toStringメソッドを使うことで2進数表記の文字列を取得できる

assert.strictEqual(9 .toString(2), "1001"); // => "1001"
3 changes: 3 additions & 0 deletions test/snapshots/directive-string-prologue/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"文字列"; // => "文字列"
"\u6587\u5b57\u5217"; // => "文字列"
"𩸽"[0]; // => "\uD867"
5 changes: 5 additions & 0 deletions test/snapshots/directive-string-prologue/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
assert.strictEqual("文字列", "文字列"); // => "文字列"

assert.strictEqual("\u6587\u5b57\u5217", "文字列"); // => "文字列"

assert.strictEqual("𩸽"[0], "\uD867"); // => "\uD867"

0 comments on commit 4927f3c

Please sign in to comment.