Skip to content

Commit 4ba9de6

Browse files
sapeginazu
authored andcommitted
fix: Append a new line to the code before sending it to the linter (#4)
* Append a new line to the code before sending it to the linter (fix #1) * Correct way of extracting source code
1 parent ca2e3eb commit 4ba9de6

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

src/textlint-rule-eslint.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ const reporter = (context, options) => {
2626
return;
2727
}
2828
const raw = getSource(node);
29-
const code = node.value;
29+
const code = getUntrimmedCode(node, raw);
30+
3031
const resultLinting = engine.executeOnText(code, node.lang);
3132
if (resultLinting.errorCount === 0) {
3233
return;
@@ -65,6 +66,34 @@ const reporter = (context, options) => {
6566
}
6667
}
6768
};
69+
70+
/**
71+
* get actual code value from CodeBlock node
72+
* @param {Object} node
73+
* @param {string} raw raw value include CodeBlock syntax
74+
* @returns {string}
75+
*/
76+
function getUntrimmedCode(node, raw) {
77+
if (node.type !== "CodeBlock") {
78+
return node.value
79+
}
80+
81+
// Space indented CodeBlock that has not lang
82+
if (!node.lang) {
83+
return node.value;
84+
}
85+
86+
// https://github.com/wooorm/remark/issues/207#issuecomment-244620590
87+
const lines = raw.split("\n");
88+
89+
// code lines without the first line and the last line
90+
const codeLines = lines.slice(1, lines.length - 1);
91+
92+
// add last new line
93+
// \n```
94+
return codeLines.join("\n") + "\n";
95+
}
96+
6897
module.exports = {
6998
linter: reporter,
7099
fixer: reporter

test/fixtures/style.eslintconfig.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"use strict";
33
module.exports = {
44
rules: {
5+
"eol-last": ["error", "always"],
56
"indent": ["error", 4, {
67
"SwitchCase": 1
78
}],

test/textlint-rule-eslint-test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,23 @@ tester.run("textlint-rule-eslint", rule, {
1414
options: {
1515
configFile: configFilePath
1616
}
17-
}
17+
},
18+
{
19+
text: "```js\n" +
20+
"var a = 1;\n" +
21+
"```",
22+
options: {
23+
configFile: configFilePath
24+
}
25+
},
26+
{
27+
text: "```js\n\n" +
28+
"var a = 1;\n\n" +
29+
"```",
30+
options: {
31+
configFile: configFilePath
32+
}
33+
},
1834
],
1935
invalid: [
2036
{

0 commit comments

Comments
 (0)