Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite html plugin #23

Merged
merged 23 commits into from
Jan 29, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .babelrc

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/lib
/module
### https://raw.github.com/github/gitignore/608690d6b9a78c2a003affc792e49a84905b3118/Node.gitignore

# Logs
Expand Down
10 changes: 6 additions & 4 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"timeout": "5000",
"require": [
"@babel/register"
]
"$schema": "https://json.schemastore.org/mocharc",
"loader": "ts-node/esm",
"spec": [
"test/**/*.{js,ts}"
],
"timeout": 10000
}
96 changes: 96 additions & 0 deletions grasp.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
const path = require("path");
const shell = require("shelljs");
const testRootDirectory = path.join(__dirname, "test");

// textlint.setupRules({}, {}) => const options
function rewriteSetupRuleWithOption() {
const ruleAndRuleOptionPattern = "textlint.setupRules({ $ruleId: $rule }, { $ruleId: $ruleOption });";
const ruleAndRuleOptionPatternExpected = `const rule = {
ruleId: {{ruleId}},
rule: {{rule}},
options: {{ruleOption}}
};`;
shell.exec(
`npx grasp --in-place -r -e '${ruleAndRuleOptionPattern}' --replace '${ruleAndRuleOptionPatternExpected}' ${testRootDirectory}`
);
}

// textlint.setupRules({}) => const options
function rewriteSetupRule() {
const ruleAndRuleOptionPattern = "textlint.setupRules({ $ruleId: $rule });";
const ruleAndRuleOptionPatternExpected = `const rule = {
ruleId: {{ruleId}},
rule: {{rule}}
};`;
shell.exec(
`npx grasp --in-place -r -e '${ruleAndRuleOptionPattern}' --replace '${ruleAndRuleOptionPatternExpected}' ${testRootDirectory}`
);
}

// textlint.setupFilterRules({}, {}) => const options
function rewriteSetupFilterRuleWithOption() {
const ruleAndRuleOptionPattern = "textlint.setupFilterRules({ $ruleId: $rule }, { $ruleId: $ruleOption });";
const ruleAndRuleOptionPatternExpected = `const filterRule = {
ruleId: {{ruleId}},
rule: {{rule}},
options: {{ruleOption}}
};`;
shell.exec(
`npx grasp --in-place -r -e '${ruleAndRuleOptionPattern}' --replace '${ruleAndRuleOptionPatternExpected}' ${testRootDirectory}`
);
}

// setupPlugin({}) => const options
function rewriteSetupPlugins() {
const ruleAndRuleOptionPattern = "textlint.setupPlugins({ $pluginId: $plugin });";
const ruleAndRuleOptionPatternExpected = `const plugin = {
pluginId: "{{pluginId}}",
plugin: {{plugin}},
};`;
shell.exec(
`npx grasp --in-place -r -e '${ruleAndRuleOptionPattern}' --replace '${ruleAndRuleOptionPatternExpected}' ${testRootDirectory}`
);
}

// textlint.setupRules({}) => const options
function rewriteSetupFilterRule() {
const ruleAndRuleOptionPattern = "textlint.setupFilterRules({ $ruleId: $rule });";
const ruleAndRuleOptionPatternExpected = `const filterRule = {
ruleId: {{ruleId}},
rule: {{rule}}
};`;
shell.exec(
`npx grasp --in-place -r -e '${ruleAndRuleOptionPattern}' --replace '${ruleAndRuleOptionPatternExpected}' ${testRootDirectory}`
);
}

// textlint.lintMarkdown("text") => textlint.lintText(text, options);
function rewriteLintMarkdown() {
const lintMarkdownPattern = `textlint.lintMarkdown( $text )`;
const lintMarkdownPatternExpected = `textlint.lintText({{text}}, { ...options, ext: ".md" })`;
shell.exec(
`npx grasp --in-place -r -e '${lintMarkdownPattern}' --replace '${lintMarkdownPatternExpected}' ${testRootDirectory}`
);
}

// textlint.lintFile(filePath) => textlint.lintFile(text, options);
function rewriteLintFile() {
const pattern = `textlint.lintFile( $filePath )`;
const expected = `textlint.lintText(fs.readFileSync( {{filePath}}, "utf-8"), { ...options, ext: path.extname({{filePath}}), filePath })`;
shell.exec(`npx grasp --in-place -r -e '${pattern}' --replace '${expected}' ${testRootDirectory}`);
}

function rewriteCore() {
const pattern = `textlint = new TextLintCore();`;
const expected = `textlint = new TextlintKernel()`;
shell.exec(`npx grasp --in-place -r -e '${pattern}' --replace '${expected}' ${testRootDirectory}`);
}

rewriteSetupRuleWithOption();
rewriteSetupRule();
rewriteSetupFilterRuleWithOption();
rewriteSetupFilterRule();
rewriteSetupPlugins();
rewriteLintMarkdown();
rewriteLintFile()
rewriteCore();
97 changes: 61 additions & 36 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,53 +1,78 @@
{
"name": "textlint-plugin-html",
"version": "0.3.0",
"description": "textlint HTML processsor plugin.",
"keywords": [
"textlint",
"plugin",
"html",
"lint"
],
"homepage": "https://github.com/textlint/textlint-plugin-html",
"bugs": {
"url": "https://github.com/textlint/textlint-plugin-html/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/textlint/textlint-plugin-html.git"
},
"author": "azu",
"email": "[email protected]",
"homepage": "https://github.com/textlint/textlint-plugin-html",
"license": "MIT",
"bugs": {
"url": "https://github.com/textlint/textlint-plugin-html/issues"
"author": "azu",
"type": "module",
"exports": {
".": {
"import": {
"types": "./module/index.d.ts",
"default": "./module/index.js"
},
"default": "./module/index.js"
},
"./package.json": "./package.json"
},
"version": "0.3.0",
"description": "textlint HTML processsor plugin.",
"main": "lib/index.js",
"files": [
"lib",
"src"
],
"main": "./module/index.js",
"types": "./module/index.d.ts",
"directories": {
"test": "test"
},
"files": [
"bin/",
"module/",
"src/"
],
"scripts": {
"build": "NODE_ENV=production babel src --out-dir lib --source-maps",
"watch": "babel src --out-dir lib --watch --source-maps",
"build": "tsc --build",
"clean": "tsc --build --clean",
"prepublishOnly": "npm run clean && npm run build",
"prepublish": "npm run --if-present build",
"test": "mocha"
"test": "mocha",
"updateSnapshot": "UPDATE_SNAPSHOT=1 npm test",
"watch": "tsc --build --watch"
},
"dependencies": {
"@textlint/ast-node-types": "^13.0.5",
"rehype-parse": "^8.0.4",
"structured-source": "^4.0.0",
"traverse": "^0.6.7",
"unified": "^10.1.2"
},
"keywords": [
"textlint",
"plugin",
"html",
"lint"
],
"devDependencies": {
"@babel/cli": "^7.0.0",
"@babel/core": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@babel/register": "^7.0.0",
"@textlint/ast-tester": "^12.1.0",
"@textlint/module-interop": "^12.1.0",
"glob": "^7.1.1",
"mocha": "^9.1.3",
"textlint": "^12.1.0",
"textlint-rule-no-todo": "^2.0.0"
"@textlint/ast-tester": "^13.0.5",
"@textlint/kernel": "^13.0.5",
"@textlint/module-interop": "^13.0.5",
"@textlint/types": "^13.0.5",
"@types/glob": "^8.0.1",
"@types/mocha": "^10.0.1",
"@types/node": "^18.11.18",
"@types/traverse": "^0.6.32",
"glob": "^8.1.0",
"grasp": "^0.6.0",
"mocha": "^10.2.0",
"shelljs": "^0.8.5",
"textlint": "^13.0.5",
"textlint-rule-no-todo": "^2.0.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
},
"dependencies": {
"hast": "0.0.2",
"structured-source": "^3.0.2",
"traverse": "^0.6.6"
}
"packageManager": "[email protected]",
"email": "[email protected]"
}
30 changes: 0 additions & 30 deletions src/HTMLProcessor.js

This file was deleted.

37 changes: 37 additions & 0 deletions src/HTMLProcessor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// LICENSE : MIT
import { TextlintPluginProcessor } from "@textlint/types";
import { parse } from "./html-to-ast.js";

export type HTMLProcessorOptions = {
extensions?: string[];
}
export default class HTMLProcessor implements TextlintPluginProcessor {
config: HTMLProcessorOptions;
extensions: string[];

constructor(config: HTMLProcessorOptions) {
this.config = config;
this.extensions = this.config.extensions ? this.config.extensions : [];
}

availableExtensions() {
return [
".htm",
".html"
].concat(this.extensions);
}

processor(_ext: string) {
return {
preProcess(text: string, _filePath: string) {
return parse(text);
},
postProcess(messages: Array<any>, filePath?: string) {
return {
messages,
filePath: filePath ? filePath : "<html>"
};
}
};
}
}
90 changes: 0 additions & 90 deletions src/html-to-ast.js

This file was deleted.

Loading