Skip to content

Commit 9c82606

Browse files
authored
feat(deps): upgrade to eslint@8 (#30)
BREAKING CHANGE: require ESLing 8 as peerDependencies - migrate to TypeScript - upgrade to ESLint 8 - change peerDependencies
1 parent d0be346 commit 9c82606

File tree

8 files changed

+773
-507
lines changed

8 files changed

+773
-507
lines changed

.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: [ 14, 16 ]
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.

README.md

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

33

44
[textlint](https://textlint.github.io/ "textlint official site") rule to lint JavaScript in Markdown with ESLint.

package.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,21 @@
3232
"textlint"
3333
],
3434
"devDependencies": {
35+
"@types/eslint": "^8.2.1",
36+
"@types/node": "^17.0.4",
3537
"@types/structured-source": "^3.0.0",
36-
"eslint": "7.28.0",
38+
"eslint": "8.5.0",
3739
"textlint-plugin-asciidoctor": "^1.1.0",
38-
"textlint-scripts": "^12.1.0"
40+
"textlint-scripts": "^12.1.0",
41+
"ts-node": "^10.4.0",
42+
"typescript": "^4.5.4",
43+
"textlint-tester": "^12.1.0"
3944
},
4045
"peerDependencies": {
41-
"eslint": "^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0"
46+
"eslint": "^8.0.0"
4247
},
4348
"dependencies": {
49+
"@textlint/types": "^12.1.0",
4450
"structured-source": "^3.0.2"
4551
}
4652
}

src/textlint-rule-eslint.js renamed to src/textlint-rule-eslint.ts

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
// LICENSE : MIT
22
"use strict";
3-
const path = require("path");
4-
const Source = require("structured-source");
5-
const CLIEngine = require("eslint").CLIEngine;
3+
import path from "path";
4+
import type { TextlintRuleContext, TextlintRuleModule } from "@textlint/types"
5+
import Source from "structured-source";
6+
import { ESLint } from "eslint";
7+
68
const defaultOptions = {
79
// path to .eslintrc file
810
"configFile": null,
911
// recognize lang of CodeBlock
1012
"langs": ["js", "javascript", "node", "jsx"]
1113
};
12-
const getConfigBaseDir = (context) => {
14+
const getConfigBaseDir = (context: TextlintRuleContext & { config?: any }) => {
1315
if (typeof context.getConfigBaseDir === "function") {
1416
return context.getConfigBaseDir();
1517
}
@@ -20,31 +22,40 @@ const getConfigBaseDir = (context) => {
2022
return textlintRcFilePath ? path.dirname(textlintRcFilePath) : process.cwd();
2123
};
2224

23-
const reporter = (context, options) => {
24-
const { Syntax, RuleError, report, fixer, getSource } = context;
25+
export type Options = {
26+
configFile: string;
27+
langs: string[]
28+
}
29+
const reporter: TextlintRuleModule<Options> = (context, options) => {
30+
const { Syntax, RuleError, report, fixer, getSource, getFilePath} = context;
31+
if (!options) {
32+
throw new Error(`Require options: { "configFile": "path/to/.eslintrc" }`);
33+
}
2534
if (!options.configFile) {
2635
throw new Error(`Require options: { "configFile": "path/to/.eslintrc" }`);
2736
}
2837
const availableLang = options.langs || defaultOptions.langs;
2938
const textlintRCDir = getConfigBaseDir(context);
30-
const ESLintOptions = {
31-
configFile: path.resolve(textlintRCDir, options.configFile)
32-
};
33-
const engine = new CLIEngine(ESLintOptions);
39+
const esLintConfigFilePath = textlintRCDir ? path.resolve(textlintRCDir, options.configFile) : options.configFile;
40+
const engine = new ESLint({
41+
useEslintrc: false,
42+
overrideConfigFile: esLintConfigFilePath
43+
});
3444
return {
35-
[Syntax.CodeBlock](node) {
45+
async [Syntax.CodeBlock](node) {
3646
if (availableLang.indexOf(node.lang) === -1) {
3747
return;
3848
}
3949
const raw = getSource(node);
4050
const code = getUntrimmedCode(node, raw);
4151
const source = new Source(code);
42-
const resultLinting = engine.executeOnText(code, node.lang);
43-
if (resultLinting.errorCount === 0) {
52+
const resultLinting = await engine.lintText(code, {
53+
filePath: `test.${node.lang}`
54+
});
55+
if (resultLinting.length === 0) {
4456
return;
4557
}
46-
const results = resultLinting.results;
47-
results.forEach(result => {
58+
resultLinting.forEach(result => {
4859
result.messages.forEach(message => {
4960
/*
5061
@@ -63,15 +74,15 @@ const reporter = (context, options) => {
6374
const fixedRange = message.fix.range;
6475
const fixedText = message.fix.text;
6576
const sourceBlockDiffIndex = (raw !== node.value) ? raw.indexOf(code) : 0;
66-
const fixedWithPadding = [fixedRange[0] + sourceBlockDiffIndex, fixedRange[1] + sourceBlockDiffIndex];
77+
const fixedWithPadding = [fixedRange[0] + sourceBlockDiffIndex, fixedRange[1] + sourceBlockDiffIndex] as const;
6778
const index = source.positionToIndex({
6879
line: message.line,
6980
column: message.column
7081
});
7182
const adjustedIndex = index + sourceBlockDiffIndex - 1;
7283
report(node, new RuleError(`${prefix}${message.message}`, {
7384
index: adjustedIndex,
74-
fix: fixer.replaceTextRange(fixedWithPadding, fixedText)
85+
fix: fixer.replaceTextRange(fixedWithPadding as [number, number], fixedText)
7586
}));
7687
} else {
7788
const sourceBlockDiffIndex = (raw !== node.value) ? raw.indexOf(code) : 0;
@@ -97,7 +108,7 @@ const reporter = (context, options) => {
97108
* @param {string} raw raw value include CodeBlock syntax
98109
* @returns {string}
99110
*/
100-
function getUntrimmedCode(node, raw) {
111+
function getUntrimmedCode(node: any, raw: string) {
101112
if (node.type !== "CodeBlock") {
102113
return node.value
103114
}
@@ -123,7 +134,7 @@ function getUntrimmedCode(node, raw) {
123134
return codeLines.join("\n") + "\n";
124135
}
125136

126-
module.exports = {
137+
export default {
127138
linter: reporter,
128139
fixer: reporter
129140
};

test/textlint-rule-eslint-test.js renamed to test/textlint-rule-eslint-test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// LICENSE : MIT
22
"use strict";
33
import rule from "../src/textlint-rule-eslint";
4-
5-
const path = require("path");
64
import TextLintTester from "textlint-tester";
5+
import path from "path";
76
const tester = new TextLintTester();
87
const configFilePath = path.join(__dirname, "fixtures/style.eslintconfig.js");
98
const WrongCode1 = "var a = 1";
109
const WrongCode2 = "var a = 1; var b = 2";
10+
// @ts-expect-error
1111
tester.run("textlint-rule-eslint", rule, {
1212
valid: [
1313
{

tsconfig.json

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
{
2+
"compilerOptions": {
3+
/* Visit https://aka.ms/tsconfig.json to read more about this file */
4+
5+
/* Projects */
6+
// "incremental": true, /* Enable incremental compilation */
7+
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
8+
// "tsBuildInfoFile": "./", /* Specify the folder for .tsbuildinfo incremental compilation files. */
9+
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects */
10+
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
11+
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
12+
13+
/* Language and Environment */
14+
"target": "es2016",
15+
"noEmit": true,
16+
/* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
17+
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
18+
// "jsx": "preserve", /* Specify what JSX code is generated. */
19+
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
20+
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
21+
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h' */
22+
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
23+
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using `jsx: react-jsx*`.` */
24+
// "reactNamespace": "", /* Specify the object invoked for `createElement`. This only applies when targeting `react` JSX emit. */
25+
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
26+
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
27+
28+
/* Modules */
29+
"module": "commonjs",
30+
/* Specify what module code is generated. */
31+
// "rootDir": "./", /* Specify the root folder within your source files. */
32+
// "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
33+
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
34+
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
35+
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
36+
// "typeRoots": [], /* Specify multiple folders that act like `./node_modules/@types`. */
37+
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
38+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
39+
// "resolveJsonModule": true, /* Enable importing .json files */
40+
// "noResolve": true, /* Disallow `import`s, `require`s or `<reference>`s from expanding the number of files TypeScript should add to a project. */
41+
42+
/* JavaScript Support */
43+
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */
44+
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
45+
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from `node_modules`. Only applicable with `allowJs`. */
46+
47+
/* Emit */
48+
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
49+
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
50+
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
51+
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
52+
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */
53+
// "outDir": "./", /* Specify an output folder for all emitted files. */
54+
// "removeComments": true, /* Disable emitting comments. */
55+
// "noEmit": true, /* Disable emitting files from a compilation. */
56+
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
57+
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */
58+
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
59+
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
60+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
61+
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
62+
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
63+
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
64+
// "newLine": "crlf", /* Set the newline character for emitting files. */
65+
// "stripInternal": true, /* Disable emitting declarations that have `@internal` in their JSDoc comments. */
66+
// "noEmitHelpers": true, /* Disable generating custom helper functions like `__extends` in compiled output. */
67+
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
68+
// "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */
69+
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
70+
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
71+
72+
/* Interop Constraints */
73+
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
74+
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
75+
"esModuleInterop": true,
76+
/* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */
77+
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
78+
"forceConsistentCasingInFileNames": true,
79+
/* Ensure that casing is correct in imports. */
80+
81+
/* Type Checking */
82+
"strict": true,
83+
/* Enable all strict type-checking options. */
84+
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied `any` type.. */
85+
// "strictNullChecks": true, /* When type checking, take into account `null` and `undefined`. */
86+
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
87+
// "strictBindCallApply": true, /* Check that the arguments for `bind`, `call`, and `apply` methods match the original function. */
88+
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
89+
// "noImplicitThis": true, /* Enable error reporting when `this` is given the type `any`. */
90+
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
91+
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
92+
// "noUnusedLocals": true, /* Enable error reporting when a local variables aren't read. */
93+
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read */
94+
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
95+
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
96+
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
97+
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
98+
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
99+
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type */
100+
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
101+
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
102+
103+
/* Completeness */
104+
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
105+
"skipLibCheck": true
106+
/* Skip type checking all .d.ts files. */
107+
}
108+
}

0 commit comments

Comments
 (0)