Skip to content

Commit 99bf488

Browse files
committed
Update src/textlint-rule-eslint.ts
1 parent 1e9d89b commit 99bf488

File tree

1 file changed

+100
-109
lines changed

1 file changed

+100
-109
lines changed

src/textlint-rule-eslint.ts

Lines changed: 100 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -26,121 +26,116 @@ export type Options = {
2626
configFile: string;
2727
langs: string[];
2828
};
29-
const createReporter = ({ fix }: { fix: boolean }): TextlintRuleModule<Options> => {
30-
return (context, options) => {
31-
const { Syntax, RuleError, report, fixer, getSource, locator } = context;
32-
if (!options) {
33-
throw new Error(`Require options: { "configFile": "path/to/.eslintrc" }`);
34-
}
35-
if (!options.configFile) {
36-
throw new Error(`Require options: { "configFile": "path/to/.eslintrc" }`);
37-
}
38-
const availableLang = options.langs || defaultOptions.langs;
39-
const textlintRCDir = getConfigBaseDir(context);
40-
const esLintConfigFilePath = textlintRCDir
41-
? path.resolve(textlintRCDir, options.configFile)
42-
: options.configFile;
43-
const engine = new ESLint({
44-
overrideConfigFile: esLintConfigFilePath,
45-
ignore: false
46-
});
47-
return {
48-
async [Syntax.CodeBlock](node) {
49-
if (!node.lang) {
50-
return;
51-
}
52-
if (availableLang.indexOf(node.lang) === -1) {
53-
return;
54-
}
55-
const raw = getSource(node);
56-
const code = getUntrimmedCode(node, raw);
57-
const source = new StructuredSource(code);
58-
const resultLinting = await engine.lintText(code, {
59-
filePath: `test.js`
60-
});
61-
if (resultLinting.length === 0) {
62-
return;
63-
}
64-
resultLinting.forEach((result) => {
65-
result.messages.forEach((message) => {
66-
/*
29+
const reporter: TextlintRuleModule<Options> = (context, options) => {
30+
const { Syntax, RuleError, report, fixer, getSource, locator } = context;
31+
if (!options) {
32+
throw new Error(`Require options: { "configFile": "path/to/.eslintrc" }`);
33+
}
34+
if (!options.configFile) {
35+
throw new Error(`Require options: { "configFile": "path/to/.eslintrc" }`);
36+
}
37+
const availableLang = options.langs || defaultOptions.langs;
38+
const textlintRCDir = getConfigBaseDir(context);
39+
const esLintConfigFilePath = textlintRCDir ? path.resolve(textlintRCDir, options.configFile) : options.configFile;
40+
const engine = new ESLint({
41+
overrideConfigFile: esLintConfigFilePath,
42+
ignore: false
43+
});
44+
return {
45+
async [Syntax.CodeBlock](node) {
46+
if (!node.lang) {
47+
return;
48+
}
49+
if (availableLang.indexOf(node.lang) === -1) {
50+
return;
51+
}
52+
const raw = getSource(node);
53+
const code = getUntrimmedCode(node, raw);
54+
const source = new StructuredSource(code);
55+
const resultLinting = await engine.lintText(code, {
56+
filePath: `test.js`
57+
});
58+
if (resultLinting.length === 0) {
59+
return;
60+
}
61+
resultLinting.forEach((result) => {
62+
result.messages.forEach((message) => {
63+
/*
6764
68-
1. ```js
69-
2. CODE
70-
3. ```
65+
1. ```js
66+
2. CODE
67+
3. ```
7168
72-
ESLint message line and column start with 1
73-
*/
74-
if (options.ignoreParsingErrors && message.message.includes("Parsing error")) {
75-
return;
76-
}
69+
ESLint message line and column start with 1
70+
*/
71+
if (options.ignoreParsingErrors && message.message.includes("Parsing error")) {
72+
return;
73+
}
7774

78-
const prefix = message.ruleId ? `${message.ruleId}: ` : "";
79-
if (message.fix) {
80-
// relative range from node
81-
const fixedRange = message.fix.range;
82-
const fixedText = message.fix.text;
83-
const sourceBlockDiffIndex = raw !== node.value ? raw.indexOf(code) : 0;
84-
const fixedWithPadding = [
85-
fixedRange[0] + sourceBlockDiffIndex,
86-
fixedRange[1] + sourceBlockDiffIndex
75+
const prefix = message.ruleId ? `${message.ruleId}: ` : "";
76+
if (message.fix) {
77+
// relative range from node
78+
const fixedRange = message.fix.range;
79+
const fixedText = message.fix.text;
80+
const sourceBlockDiffIndex = raw !== node.value ? raw.indexOf(code) : 0;
81+
const fixedWithPadding = [
82+
fixedRange[0] + sourceBlockDiffIndex,
83+
fixedRange[1] + sourceBlockDiffIndex
84+
] as const;
85+
const location = source.rangeToLocation(fixedWithPadding);
86+
const isSamePosition =
87+
location.start.line === location.end.line && location.start.column === location.end.column;
88+
report(
89+
node,
90+
new RuleError(`${prefix}${message.message}`, {
91+
padding: isSamePosition
92+
? locator.at(fixedWithPadding[0])
93+
: locator.range(fixedWithPadding),
94+
fix: isSamePosition
95+
? fixer.insertTextAfterRange(fixedWithPadding, fixedText)
96+
: fixer.replaceTextRange(fixedWithPadding, fixedText)
97+
})
98+
);
99+
} else {
100+
const sourceBlockDiffIndex = raw !== node.value ? raw.indexOf(code) : 0;
101+
if (message.endLine !== undefined && message.endColumn !== undefined) {
102+
const range = source.locationToRange({
103+
start: {
104+
line: message.line,
105+
column: message.column
106+
},
107+
end: {
108+
line: message.endLine,
109+
column: message.endColumn
110+
}
111+
});
112+
const adjustedRange = [
113+
range[0] + sourceBlockDiffIndex,
114+
range[1] + sourceBlockDiffIndex
87115
] as const;
88-
const location = source.rangeToLocation(fixedWithPadding);
89-
const isSamePosition =
90-
location.start.line === location.end.line &&
91-
location.start.column === location.end.column;
92116
report(
93117
node,
94118
new RuleError(`${prefix}${message.message}`, {
95-
padding: isSamePosition
96-
? locator.at(fixedWithPadding[0])
97-
: locator.range(fixedWithPadding),
98-
fix: isSamePosition
99-
? fixer.insertTextAfterRange(fixedWithPadding, fixedText)
100-
: fixer.replaceTextRange(fixedWithPadding, fixedText)
119+
padding: locator.range(adjustedRange)
101120
})
102121
);
103122
} else {
104-
const sourceBlockDiffIndex = raw !== node.value ? raw.indexOf(code) : 0;
105-
if (message.endLine !== undefined && message.endColumn !== undefined) {
106-
const range = source.locationToRange({
107-
start: {
108-
line: message.line,
109-
column: message.column
110-
},
111-
end: {
112-
line: message.endLine,
113-
column: message.endColumn
114-
}
115-
});
116-
const adjustedRange = [
117-
range[0] + sourceBlockDiffIndex,
118-
range[1] + sourceBlockDiffIndex
119-
] as const;
120-
report(
121-
node,
122-
new RuleError(`${prefix}${message.message}`, {
123-
padding: locator.range(adjustedRange)
124-
})
125-
);
126-
} else {
127-
const index = source.positionToIndex({
128-
line: message.line,
129-
column: message.column
130-
});
131-
const adjustedIndex = index + sourceBlockDiffIndex - 1;
132-
report(
133-
node,
134-
new RuleError(`${prefix}${message.message}`, {
135-
padding: locator.at(adjustedIndex)
136-
})
137-
);
138-
}
123+
const index = source.positionToIndex({
124+
line: message.line,
125+
column: message.column
126+
});
127+
const adjustedIndex = index + sourceBlockDiffIndex - 1;
128+
report(
129+
node,
130+
new RuleError(`${prefix}${message.message}`, {
131+
padding: locator.at(adjustedIndex)
132+
})
133+
);
139134
}
140-
});
135+
}
141136
});
142-
}
143-
};
137+
});
138+
}
144139
};
145140
};
146141

@@ -177,10 +172,6 @@ function getUntrimmedCode(node: any, raw: string) {
177172
}
178173

179174
export default {
180-
linter: createReporter({
181-
fix: false
182-
}),
183-
fixer: createReporter({
184-
fix: true
185-
})
175+
linter: reporter,
176+
fixer: reporter
186177
};

0 commit comments

Comments
 (0)