Skip to content

Commit

Permalink
feat(power-doctest): support ES2020
Browse files Browse the repository at this point in the history
can parse BigInt
  • Loading branch information
azu committed Jun 20, 2020
1 parent bd78988 commit ef624cb
Show file tree
Hide file tree
Showing 59 changed files with 2,698 additions and 1,861 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/snapshots/**
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"husky": "^3.0.4",
"lerna": "^3.16.4",
"lint-staged": "^9.2.3",
"prettier": "^1.18.2"
"prettier": "^2.0.5"
},
"scripts": {
"bootstrap": "lerna bootstrap && yarn run build",
Expand Down
5 changes: 5 additions & 0 deletions packages/@power-doctest/asciidoctor/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": [
"ts-node-test-register"
]
}
16 changes: 8 additions & 8 deletions packages/@power-doctest/asciidoctor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@
"structured-source": "^3.0.2"
},
"devDependencies": {
"@types/mocha": "^5.2.7",
"@types/node": "^12.7.3",
"@types/mocha": "^7.0.2",
"@types/node": "^14.0.13",
"@types/structured-source": "^3.0.0",
"cross-env": "^5.2.0",
"mocha": "^6.2.0",
"prettier": "^1.18.2",
"rimraf": "^3.0.0",
"ts-node": "^8.3.0",
"cross-env": "^7.0.2",
"mocha": "^8.0.1",
"prettier": "^2.0.5",
"rimraf": "^3.0.2",
"ts-node": "^8.10.2",
"ts-node-test-register": "^8.0.1",
"typescript": "^3.6.2"
"typescript": "^3.9.5"
},
"publishConfig": {
"access": "public"
Expand Down
23 changes: 14 additions & 9 deletions packages/@power-doctest/asciidoctor/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const Asciidoctor = require("asciidoctor");
const asciidoctor = Asciidoctor();
type Attributes = {
[index: string]: string;
}
};
const getState = (attributes: Attributes): "none" | "enabled" | "disabled" => {
const state = attributes["doctest-state"] || attributes["doctest"];
if (!state) {
Expand Down Expand Up @@ -77,10 +77,13 @@ const inlineCode = (code: string, baseFilePath: string): string => {
export function parse(args: ParserArgs): ParsedResults {
const structuredSource = new StructuredSource(args.content);
const doc = asciidoctor.load(args.content);
return doc.getBlocks()
return doc
.getBlocks()
.filter((block: any) => {
const attributes = block.getAttributes();
return attributes.style === "source" && (attributes.language === "js" || attributes.language === "javascript");
return (
attributes.style === "source" && (attributes.language === "js" || attributes.language === "javascript")
);
})
.map((block: any) => {
// FIXME: workaround get lineno
Expand All @@ -101,12 +104,14 @@ export function parse(args: ParserArgs): ParsedResults {
end: endPostion
},
metadata: meta,
doctestOptions: doctestOptions ? {
filePath: args.filePath,
...doctestOptions
} : {
filePath: args.filePath
}
doctestOptions: doctestOptions
? {
filePath: args.filePath,
...doctestOptions
}
: {
filePath: args.filePath
}
};
return parsedCode;
});
Expand Down
1 change: 0 additions & 1 deletion packages/@power-doctest/asciidoctor/test/mocha.opts

This file was deleted.

64 changes: 32 additions & 32 deletions packages/@power-doctest/asciidoctor/test/snapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,40 @@ const trimUndefinedProperty = <T>(o: T, baseDir: string): T => {
return JSON.parse(stringify(o, baseDir));
};
const stringify = (o: {}, baseDir: string): string => {
return JSON.stringify(o, (key: string, value: any) => {
if (key === "filePath" && typeof value === "string") {
return path.relative(baseDir, value);
} else {
return value;
}
}, 4);
return JSON.stringify(
o,
(key: string, value: any) => {
if (key === "filePath" && typeof value === "string") {
return path.relative(baseDir, value);
} else {
return value;
}
},
4
);
};
describe("Snapshot testing", () => {
fs.readdirSync(fixturesDir)
.map(caseName => {
const normalizedTestName = caseName.replace(/-/g, " ");
it(`Test ${normalizedTestName}`, async function() {
const fixtureDir = path.join(fixturesDir, caseName);
const actualFilePath = path.join(fixtureDir, "input.adoc");
const actualContent = fs.readFileSync(actualFilePath, "utf-8");
const results = parse({
content: actualContent,
filePath: actualFilePath
});
const expectedFilePath = path.join(fixtureDir, "output.json");
// Usage: update snapshots
// UPDATE_SNAPSHOT=1 npm test
if (!fs.existsSync(expectedFilePath) || process.env.UPDATE_SNAPSHOT) {
fs.writeFileSync(expectedFilePath, stringify(results, fixtureDir));
this.skip(); // skip when updating snapshots
return;
}
// compare input and output
const expectedContent = JSON.parse(fs.readFileSync(expectedFilePath, "utf-8"));
assert.deepStrictEqual(
trimUndefinedProperty(results, fixtureDir),
expectedContent
);
fs.readdirSync(fixturesDir).map(caseName => {
const normalizedTestName = caseName.replace(/-/g, " ");
it(`Test ${normalizedTestName}`, async function() {
const fixtureDir = path.join(fixturesDir, caseName);
const actualFilePath = path.join(fixtureDir, "input.adoc");
const actualContent = fs.readFileSync(actualFilePath, "utf-8");
const results = parse({
content: actualContent,
filePath: actualFilePath
});
const expectedFilePath = path.join(fixtureDir, "output.json");
// Usage: update snapshots
// UPDATE_SNAPSHOT=1 npm test
if (!fs.existsSync(expectedFilePath) || process.env.UPDATE_SNAPSHOT) {
fs.writeFileSync(expectedFilePath, stringify(results, fixtureDir));
this.skip(); // skip when updating snapshots
return;
}
// compare input and output
const expectedContent = JSON.parse(fs.readFileSync(expectedFilePath, "utf-8"));
assert.deepStrictEqual(trimUndefinedProperty(results, fixtureDir), expectedContent);
});
});
});
5 changes: 5 additions & 0 deletions packages/@power-doctest/core/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": [
"ts-node-test-register"
]
}
30 changes: 15 additions & 15 deletions packages/@power-doctest/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
"watch": "tsc -p . --watch"
},
"dependencies": {
"@babel/core": "^7.5.5",
"@babel/parser": "^7.5.5",
"@babel/template": "^7.4.4",
"@babel/traverse": "^7.5.5",
"@babel/types": "^7.5.5",
"@babel/core": "^7.10.3",
"@babel/parser": "^7.10.3",
"@babel/template": "^7.10.3",
"@babel/traverse": "^7.10.3",
"@babel/types": "^7.10.3",
"ast-source": "^3.0.0",
"babel-plugin-espower": "^3.0.1",
"comment-to-assert": "^5.0.1",
Expand All @@ -40,21 +40,21 @@
"power-assert": "^1.6.1"
},
"devDependencies": {
"@babel/generator": "^7.5.5",
"@types/babel__core": "^7.1.2",
"@types/babel__generator": "^7.0.2",
"@babel/generator": "^7.10.3",
"@types/babel__core": "^7.1.8",
"@types/babel__generator": "^7.6.1",
"@types/babel__template": "^7.0.2",
"@types/babel__traverse": "^7.0.7",
"@types/mocha": "^5.2.7",
"@types/node": "^12.7.2",
"@types/babel__traverse": "^7.0.12",
"@types/mocha": "^7.0.2",
"@types/node": "^14.0.13",
"ast-equal": "^1.0.2",
"babel-preset-jsdoc-to-assert": "^5.0.0",
"babel-preset-power-assert": "^3.0.0",
"cross-env": "^5.2.0",
"mocha": "^6.2.0",
"ts-node": "^8.3.0",
"cross-env": "^7.0.2",
"mocha": "^8.0.1",
"ts-node": "^8.10.2",
"ts-node-test-register": "^8.0.1",
"typescript": "^3.5.3"
"typescript": "^3.9.5"
},
"peerDependecies": {},
"tags": [
Expand Down
10 changes: 5 additions & 5 deletions packages/@power-doctest/core/src/inject-assert.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// LICENSE : MIT
"use strict";
import traverse from "@babel/traverse"
import template from "@babel/template"

type Node = import("@babel/traverse").Node
import traverse from "@babel/traverse";
import template from "@babel/template";
import { Node } from "@babel/types";

export function injectAssertModule(AST: Node) {
// @ts-ignore
traverse(AST, {
Program: {
enter(path) {
(path as any).unshiftContainer("body", template`var assert = require("power-assert")`())
(path as any).unshiftContainer("body", template`var assert = require("power-assert")`());
}
}
});
Expand Down
18 changes: 11 additions & 7 deletions packages/@power-doctest/core/src/power-doctest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import generate from "@babel/generator";
import assert from "assert";
import { toAssertFromAST } from "comment-to-assert";
import { injectAssertModule } from "./inject-assert";

const babelPluginEspower = require("babel-plugin-espower");

export interface convertCodeOption {
filePath: string;
babel?: ParserOptions;
Expand All @@ -23,7 +25,7 @@ export interface convertCodeOption {
export function convertCode(code: string, options: convertCodeOption): string {
const AST = parse(code, {
sourceType: "module",
...options.babel ? options.babel : {}
...(options.babel ? options.babel : {})
});
const output = convertAST(AST, {
assertBeforeCallbackName: options.assertBeforeCallbackName,
Expand All @@ -49,7 +51,6 @@ export interface convertASTOptions {
*/
export function convertAST<T extends File>(AST: T, options: convertASTOptions): T {
const boundEspower = (AST: T) => {
// FIXME: AST to AST
const { code } = generate(AST, {
comments: true
});
Expand All @@ -72,9 +73,12 @@ export function convertAST<T extends File>(AST: T, options: convertASTOptions):
return toAssertFromAST(AST, options);
};
const modifyMapFunctionList: ((ast: any) => any)[] = [commentToAssert, injectAssertModule, boundEspower];
return modifyMapFunctionList.reduce((AST, modify, index) => {
const result = modify(AST);
assert(result != null, modifyMapFunctionList[index].name + " return wrong result. result: " + result);
return result;
}, AST as T);
return modifyMapFunctionList.reduce(
(AST, modify, index) => {
const result = modify(AST);
assert(result != null, modifyMapFunctionList[index].name + " return wrong result. result: " + result);
return result;
},
AST as T
);
}
1 change: 0 additions & 1 deletion packages/@power-doctest/core/test/mocha.opts

This file was deleted.

29 changes: 20 additions & 9 deletions packages/@power-doctest/core/test/power-doctest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,38 @@ describe("core", function() {
var result = convertCode(code, {
filePath: "test.js"
});
assert.strictEqual(result, `var assert = require("power-assert");
assert.strictEqual(
result,
`var assert = require("power-assert");
function addPrefix(text, prefix = "デフォルト:") {
return prefix + text;
}`.trim());
}`.trim()
);
});
});
describe("#convertAST", function() {
it("add assert module to header", function() {
var code = "var a = 1;";
var resultAST = parseAndConvert(code);
astEqual(resultAST, `
astEqual(
resultAST,
`
var assert = require("power-assert");
var a = 1;
`);
`
);
});
it("add assert module to ", function() {
var code = "var a = 1;";
var resultAST = parseAndConvert(code);
astEqual(resultAST, `
astEqual(
resultAST,
`
var assert = require("power-assert");
var a = 1;
`);
`
);
});
it("module type", function() {
var code = `
Expand All @@ -60,12 +69,15 @@ function addPrefix(text, prefix = "デフォルト:") {
}
`;
var resultAST = parseAndConvert(code);
astEqual(resultAST, `
astEqual(
resultAST,
`
var assert = require("power-assert");
export default function hello() {
var a = 1;
}
`);
`
);
});

it("should support async function", function() {
Expand Down Expand Up @@ -104,4 +116,3 @@ console.log(a); // => 1
});
});
});

0 comments on commit ef624cb

Please sign in to comment.