Skip to content

Commit 96adc53

Browse files
committed
feat: support TypeScript test case
1 parent fc6f1b8 commit 96adc53

File tree

7 files changed

+58
-21
lines changed

7 files changed

+58
-21
lines changed

bin/cmd.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import meow from "meow";
33
import concat from "concat-stream";
44
import create from "../src/create-textlint-rule-example.js";
55
import fs from "fs";
6+
67
const cli = meow(
78
`
89
Usage
@@ -15,14 +16,18 @@ const cli = meow(
1516
importMeta: import.meta
1617
}
1718
);
18-
const file = process.argv[2];
19-
const input = file && file !== "-" ? fs.createReadStream(process.argv[2]) : process.stdin;
19+
const filePath = process.argv[2];
20+
const input = filePath && filePath !== "-" ? fs.createReadStream(process.argv[2]) : process.stdin;
2021
input.pipe(
2122
concat(function (buf) {
23+
const content = buf.toString("utf8");
2224
console.log(
23-
create(buf.toString("utf8"), {
24-
exampleSeparator: cli.flags.separator
25-
})
25+
create(
26+
{ content, filePath },
27+
{
28+
exampleSeparator: cli.flags.separator
29+
}
30+
)
2631
);
2732
})
2833
);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"dependencies": {
4040
"@babel/core": "^7.0.0",
4141
"@babel/preset-env": "^7.0.0",
42+
"@babel/preset-typescript": "^7.13.0",
4243
"clone": "^2.1.1",
4344
"concat-stream": "^2.0.0",
4445
"lodash.uniq": "^4.5.0",

src/create-textlint-rule-example.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,18 @@ const defaultOptions = {
1111
};
1212
/**
1313
* Create example text from `content`
14-
* @param {string} content
14+
* @param {{content:string, filePath:string}}
1515
* @param {Object} options
1616
* @returns {string}
1717
*/
18-
export default function (content, options = {}) {
18+
export default function ({ content, filePath }, options = {}) {
1919
const prependValidExample = options.prependValidExample || defaultOptions.prependValidExample;
2020
const prependInValidExample = options.prependInValidExample || defaultOptions.prependInValidExample;
2121
const exampleSeparator = (options.exampleSeparator || defaultOptions.exampleSeparator).replace(/\\n/g, "\n");
22-
const { ruleName, valid, invalid } = getRuleTest(content);
22+
const { ruleName, valid, invalid } = getRuleTest({
23+
content,
24+
filePath
25+
});
2326
const plainValid = valid.filter((text) => typeof text === "string");
2427
const plainInvalid = invalid.filter((test) => {
2528
return test.options === undefined && test.ext === undefined;

src/get-rule-test.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@ import babel from "@babel/core";
55
import { calls } from "./mock-textlint-tester.js";
66
import MockTextlintRuleTester from "./mock-textlint-tester.js";
77
import { createRequire } from "module";
8+
import path from "path";
9+
810
/**
911
* Helper for unit testing:
1012
* - load module with mocked dependencies
1113
* - allow accessing private state of the module
1214
*
13-
* @param {string} content
15+
* @param {{content:string, filePath:string}}
1416
* @param {Object=} mocks Hash of mocked dependencies
1517
*/
16-
export default function loadModule(content, mocks = {}) {
18+
export default function loadModule({ content, filePath }, mocks = {}) {
1719
const exports = {};
1820
const context = {
1921
require: function (name) {
@@ -34,7 +36,8 @@ export default function loadModule(content, mocks = {}) {
3436
};
3537
const require = createRequire(import.meta.url);
3638
const result = babel.transform(content, {
37-
presets: [require.resolve("@babel/preset-env")]
39+
filename: path.basename(filePath),
40+
presets: [require.resolve("@babel/preset-env"), require.resolve("@babel/preset-typescript")]
3841
});
3942
vm.runInNewContext(result.code, context);
4043
return calls;

test/create-textlint-rule-example-test.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import assert from "assert";
44
import fs from "fs";
55
import createExample from "../src/create-textlint-rule-example.js";
66
import url from "url";
7+
78
describe("create-textlint-rule-example", () => {
89
it("should return output", () => {
9-
const content = fs.readFileSync(
10-
url.fileURLToPath(new URL("fixtures/no-todo/test/no-todo-test.js", import.meta.url)),
11-
"utf-8"
12-
);
13-
const output = createExample(content);
10+
const filePath = url.fileURLToPath(new URL("fixtures/no-todo/test/no-todo-test.js", import.meta.url));
11+
const content = fs.readFileSync(filePath, "utf-8");
12+
const output = createExample({
13+
content,
14+
filePath
15+
});
1416
assert(typeof output === "string");
1517
assert(output.length > 0);
1618
});

test/get-rule-test-test.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ import fs from "fs";
77

88
describe("get-rule-data", () => {
99
it("should return {valid,invalid,ruleName}", () => {
10-
const content = fs.readFileSync(
11-
url.fileURLToPath(new URL("fixtures/no-todo/test/no-todo-test.js", import.meta.url)),
12-
"utf-8"
13-
);
14-
const calls = getRuleTest(content);
10+
const filePath = url.fileURLToPath(new URL("fixtures/no-todo/test/no-todo-test.js", import.meta.url));
11+
const content = fs.readFileSync(filePath, "utf-8");
12+
const calls = getRuleTest({ content, filePath });
1513
assert(typeof calls.ruleName === "string");
1614
assert(Array.isArray(calls.valid));
1715
assert(Array.isArray(calls.invalid));

yarn.lock

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,13 @@
491491
dependencies:
492492
"@babel/helper-plugin-utils" "^7.12.13"
493493

494+
"@babel/plugin-syntax-typescript@^7.12.13":
495+
version "7.12.13"
496+
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-typescript/-/plugin-syntax-typescript-7.12.13.tgz#9dff111ca64154cef0f4dc52cf843d9f12ce4474"
497+
integrity sha512-cHP3u1JiUiG2LFDKbXnwVad81GvfyIOmCD6HIEId6ojrY0Drfy2q1jw7BwN7dE84+kTnBjLkXoL3IEy/3JPu2w==
498+
dependencies:
499+
"@babel/helper-plugin-utils" "^7.12.13"
500+
494501
"@babel/plugin-transform-arrow-functions@^7.13.0":
495502
version "7.13.0"
496503
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-arrow-functions/-/plugin-transform-arrow-functions-7.13.0.tgz#10a59bebad52d637a027afa692e8d5ceff5e3dae"
@@ -724,6 +731,15 @@
724731
dependencies:
725732
"@babel/helper-plugin-utils" "^7.12.13"
726733

734+
"@babel/plugin-transform-typescript@^7.13.0":
735+
version "7.13.0"
736+
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-typescript/-/plugin-transform-typescript-7.13.0.tgz#4a498e1f3600342d2a9e61f60131018f55774853"
737+
integrity sha512-elQEwluzaU8R8dbVuW2Q2Y8Nznf7hnjM7+DSCd14Lo5fF63C9qNLbwZYbmZrtV9/ySpSUpkRpQXvJb6xyu4hCQ==
738+
dependencies:
739+
"@babel/helper-create-class-features-plugin" "^7.13.0"
740+
"@babel/helper-plugin-utils" "^7.13.0"
741+
"@babel/plugin-syntax-typescript" "^7.12.13"
742+
727743
"@babel/plugin-transform-unicode-escapes@^7.12.13":
728744
version "7.12.13"
729745
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-unicode-escapes/-/plugin-transform-unicode-escapes-7.12.13.tgz#840ced3b816d3b5127dd1d12dcedc5dead1a5e74"
@@ -829,6 +845,15 @@
829845
"@babel/types" "^7.4.4"
830846
esutils "^2.0.2"
831847

848+
"@babel/preset-typescript@^7.13.0":
849+
version "7.13.0"
850+
resolved "https://registry.yarnpkg.com/@babel/preset-typescript/-/preset-typescript-7.13.0.tgz#ab107e5f050609d806fbb039bec553b33462c60a"
851+
integrity sha512-LXJwxrHy0N3f6gIJlYbLta1D9BDtHpQeqwzM0LIfjDlr6UE/D5Mc7W4iDiQzaE+ks0sTjT26ArcHWnJVt0QiHw==
852+
dependencies:
853+
"@babel/helper-plugin-utils" "^7.13.0"
854+
"@babel/helper-validator-option" "^7.12.17"
855+
"@babel/plugin-transform-typescript" "^7.13.0"
856+
832857
"@babel/runtime@^7.8.4":
833858
version "7.14.0"
834859
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.14.0.tgz#46794bc20b612c5f75e62dd071e24dfd95f1cbe6"

0 commit comments

Comments
 (0)