Skip to content

Commit fc6f1b8

Browse files
authored
refactor: use ESM instead of Babel (#2)
* refactor: use ESM instead of Babel * style: add prettier * chore: use import.meta
1 parent 36a7685 commit fc6f1b8

File tree

19 files changed

+2627
-123
lines changed

19 files changed

+2627
-123
lines changed

.babelrc

Lines changed: 0 additions & 13 deletions
This file was deleted.

.githooks/pre-commit

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
npx --no-install lint-staged

.github/workflows/test.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: test
2+
on: [push, pull_request]
3+
jobs:
4+
test:
5+
name: "Test on Node.js ${{ matrix.node-version }}"
6+
runs-on: ubuntu-latest
7+
strategy:
8+
matrix:
9+
node-version: [12, 14]
10+
steps:
11+
- name: checkout
12+
uses: actions/checkout@v2
13+
- name: setup Node.js ${{ matrix.node-version }}
14+
uses: actions/setup-node@v2
15+
with:
16+
node-version: ${{ matrix.node-version }}
17+
- name: Install
18+
run: yarn install
19+
- name: Test
20+
run: yarn test

.travis.yml

Lines changed: 0 additions & 3 deletions
This file was deleted.

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2017 azu
1+
Copyright (c) 2021 azu
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy
44
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# create-textlint-rule-example [![Build Status](https://travis-ci.org/textlint/create-textlint-rule-example.svg?branch=master)](https://travis-ci.org/textlint/create-textlint-rule-example)
1+
# create-textlint-rule-example [![Actions Status: test](https://github.com/textlint/create-textlint-rule-example/workflows/test/badge.svg)](https://github.com/textlint/create-textlint-rule-example/actions?query=workflow%3A"test")
22

33
Create textlint rule example from test codes([textlint-tester](https://github.com/textlint/textlint-tester "textlint-tester")).
44

bin/cmd.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
#!/usr/bin/env node
2-
3-
const meow = require('meow');
4-
const cli = meow(`
2+
import meow from "meow";
3+
import concat from "concat-stream";
4+
import create from "../src/create-textlint-rule-example.js";
5+
import fs from "fs";
6+
const cli = meow(
7+
`
58
Usage
69
$ create-textlint-rule-example <file-path>
710
811
Options
912
--separator separator between each examples
10-
`);
11-
const create = require("../lib/create-textlint-rule-example");
12-
const concat = require('concat-stream');
13-
const fs = require('fs');
13+
`,
14+
{
15+
importMeta: import.meta
16+
}
17+
);
1418
const file = process.argv[2];
15-
const input = file && file !== '-'
16-
? fs.createReadStream(process.argv[2])
17-
: process.stdin;
18-
input.pipe(concat(function(buf) {
19-
console.log(create(buf.toString('utf8'), {
20-
exampleSeparator: cli.flags.separator
21-
}));
22-
}));
19+
const input = file && file !== "-" ? fs.createReadStream(process.argv[2]) : process.stdin;
20+
input.pipe(
21+
concat(function (buf) {
22+
console.log(
23+
create(buf.toString("utf8"), {
24+
exampleSeparator: cli.flags.separator
25+
})
26+
);
27+
})
28+
);

package.json

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,64 @@
11
{
2-
"directories": {
3-
"test": "test"
4-
},
5-
"author": "azu",
6-
"license": "MIT",
7-
"files": [
8-
"bin/",
9-
"lib/",
10-
"src/"
11-
],
122
"name": "create-textlint-rule-example",
133
"version": "1.1.0",
14-
"description": "Create textlint rule example from test codes.",
15-
"main": "lib/index.js",
16-
"bin": {
17-
"create-textlint-rule-example": "bin/cmd.js"
18-
},
19-
"scripts": {
20-
"test": "mocha test/",
21-
"build": "cross-env NODE_ENV=production babel src --out-dir lib --source-maps",
22-
"watch": "babel src --out-dir lib --watch --source-maps",
23-
"prepublish": "npm run --if-present build"
24-
},
4+
"description": "A command line tool that create examples from textlint rule's test cases.",
255
"keywords": [
266
"textlint",
277
"tester",
288
"document",
299
"readme",
3010
"example"
3111
],
12+
"homepage": "https://github.com/textlint/create-textlint-rule-example",
13+
"bugs": {
14+
"url": "https://github.com/textlint/create-textlint-rule-example/issues"
15+
},
3216
"repository": {
3317
"type": "git",
3418
"url": "https://github.com/textlint/create-textlint-rule-example.git"
3519
},
36-
"bugs": {
37-
"url": "https://github.com/textlint/create-textlint-rule-example/issues"
20+
"license": "MIT",
21+
"author": "azu",
22+
"main": "lib/index.js",
23+
"type": "module",
24+
"bin": {
25+
"create-textlint-rule-example": "bin/cmd.js"
3826
},
39-
"homepage": "https://github.com/textlint/create-textlint-rule-example",
40-
"devDependencies": {
41-
"babel-cli": "^6.24.0",
42-
"babel-register": "^6.24.0",
43-
"babel-preset-jsdoc-to-assert": "^4.0.0",
44-
"babel-preset-power-assert": "^1.0.0",
45-
"cross-env": "^3.2.4",
46-
"mocha": "^3.2.0",
47-
"power-assert": "^1.4.2"
27+
"directories": {
28+
"test": "test"
29+
},
30+
"files": [
31+
"bin/",
32+
"src/"
33+
],
34+
"scripts": {
35+
"test": "mocha test/",
36+
"format": "prettier --write \"**/*.{js,jsx,ts,tsx,css}\"",
37+
"prepare": "git config --local core.hooksPath .githooks"
4838
},
4939
"dependencies": {
50-
"babel-core": "^6.24.0",
51-
"babel-preset-latest": "^6.24.0",
40+
"@babel/core": "^7.0.0",
41+
"@babel/preset-env": "^7.0.0",
5242
"clone": "^2.1.1",
53-
"concat-stream": "^1.6.0",
43+
"concat-stream": "^2.0.0",
5444
"lodash.uniq": "^4.5.0",
55-
"meow": "^3.7.0"
45+
"meow": "^10.0.0"
46+
},
47+
"devDependencies": {
48+
"cross-env": "^7.0.3",
49+
"lint-staged": "^11.0.0",
50+
"mocha": "^8.4.0",
51+
"prettier": "^2.3.0"
52+
},
53+
"prettier": {
54+
"singleQuote": false,
55+
"printWidth": 120,
56+
"tabWidth": 4,
57+
"trailingComma": "none"
58+
},
59+
"lint-staged": {
60+
"*.{js,jsx,ts,tsx,css}": [
61+
"prettier --write"
62+
]
5663
}
5764
}
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// MIT © 2017 azu
22
"use strict";
3-
const getRuleTest = require("./get-rule-test");
4-
const unique = require("lodash.uniq");
3+
import unique from "lodash.uniq";
4+
import getRuleTest from "./get-rule-test.js";
5+
56
const defaultOptions = {
67
prependValidExample: "**OK**:",
78
prependInValidExample: "**NG**:",
@@ -14,13 +15,13 @@ const defaultOptions = {
1415
* @param {Object} options
1516
* @returns {string}
1617
*/
17-
module.exports = function(content, options = {}) {
18+
export default function (content, options = {}) {
1819
const prependValidExample = options.prependValidExample || defaultOptions.prependValidExample;
1920
const prependInValidExample = options.prependInValidExample || defaultOptions.prependInValidExample;
2021
const exampleSeparator = (options.exampleSeparator || defaultOptions.exampleSeparator).replace(/\\n/g, "\n");
2122
const { ruleName, valid, invalid } = getRuleTest(content);
22-
const plainValid = valid.filter(text => typeof text === "string");
23-
const plainInvalid = invalid.filter(test => {
23+
const plainValid = valid.filter((text) => typeof text === "string");
24+
const plainInvalid = invalid.filter((test) => {
2425
return test.options === undefined && test.ext === undefined;
2526
});
2627

@@ -31,12 +32,12 @@ ${unique(plainValid).join(exampleSeparator)}
3132
`;
3233
const invalidExample = `
3334
\`\`\`
34-
${unique(plainInvalid.map(test => test.text)).join(exampleSeparator)}
35+
${unique(plainInvalid.map((test) => test.text)).join(exampleSeparator)}
3536
\`\`\`
3637
`;
3738

3839
return `${prependValidExample}
3940
${validExample}
4041
${prependInValidExample}
41-
${invalidExample}`
42-
};
42+
${invalidExample}`;
43+
}

src/get-rule-test.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
// MIT © 2017 azu
22
"use strict";
3-
const vm = require("vm");
4-
const fs = require("fs");
5-
const path = require("path");
6-
const babel = require("babel-core");
7-
const calls = require("./mock-textlint-tester").calls;
8-
const MockTextlintRuleTester = require("./mock-textlint-tester");
3+
import vm from "vm";
4+
import babel from "@babel/core";
5+
import { calls } from "./mock-textlint-tester.js";
6+
import MockTextlintRuleTester from "./mock-textlint-tester.js";
7+
import { createRequire } from "module";
98
/**
109
* Helper for unit testing:
1110
* - load module with mocked dependencies
@@ -14,10 +13,10 @@ const MockTextlintRuleTester = require("./mock-textlint-tester");
1413
* @param {string} content
1514
* @param {Object=} mocks Hash of mocked dependencies
1615
*/
17-
module.exports = function loadModule(content, mocks = {}) {
16+
export default function loadModule(content, mocks = {}) {
1817
const exports = {};
1918
const context = {
20-
require: function(name) {
19+
require: function (name) {
2120
if (mocks[name]) {
2221
return mocks[name];
2322
}
@@ -33,13 +32,10 @@ module.exports = function loadModule(content, mocks = {}) {
3332
exports: exports
3433
}
3534
};
36-
35+
const require = createRequire(import.meta.url);
3736
const result = babel.transform(content, {
38-
"presets": [
39-
require.resolve("babel-preset-latest")
40-
]
41-
}
42-
);
37+
presets: [require.resolve("@babel/preset-env")]
38+
});
4339
vm.runInNewContext(result.code, context);
4440
return calls;
45-
};
41+
}

0 commit comments

Comments
 (0)