Skip to content

Commit bb48ae2

Browse files
authored
fix: fenced-code-language highlights only relevant parts (#487)
* fix: fenced-code-language rule to highlight only relevant parts for violations * prevent meta text highlight
1 parent 6c35261 commit bb48ae2

File tree

2 files changed

+214
-6
lines changed

2 files changed

+214
-6
lines changed

src/rules/fenced-code-language.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,38 @@ export default {
8181
}
8282

8383
context.report({
84-
loc: node.position,
84+
loc: {
85+
start: node.position.start,
86+
end: {
87+
line: node.position.start.line,
88+
column:
89+
sourceCode.lines[
90+
node.position.start.line - 1
91+
].length + 1,
92+
},
93+
},
8594
messageId: "missingLanguage",
8695
});
8796

8897
return;
8998
}
9099

91100
if (required.size && !required.has(node.lang)) {
101+
const lineText =
102+
sourceCode.lines[node.position.start.line - 1];
103+
const langIndex = lineText.indexOf(node.lang);
104+
92105
context.report({
93-
loc: node.position,
106+
loc: {
107+
start: node.position.start,
108+
end: {
109+
line: node.position.start.line,
110+
column:
111+
node.position.start.column +
112+
langIndex +
113+
node.lang.length,
114+
},
115+
},
94116
messageId: "disallowedLanguage",
95117
data: {
96118
lang: node.lang,

tests/rules/fenced-code-language.test.js

Lines changed: 190 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,48 @@ console.log("Hello, world!");
5050
\`\`\``,
5151
options: [{ required: ["js"] }],
5252
},
53+
{
54+
code: `\`\`\`js foo
55+
console.log("Hello, world!");
56+
\`\`\``,
57+
options: [{ required: ["js"] }],
58+
},
59+
{
60+
code: `\`\`\` js
61+
console.log("Hello, world!");
62+
\`\`\``,
63+
options: [{ required: ["js"] }],
64+
},
65+
{
66+
code: `\`\`\`JS
67+
console.log("Hello, world!");
68+
\`\`\``,
69+
options: [{ required: ["JS"] }],
70+
},
71+
{
72+
code: `~~~js
73+
console.log("Hello, world!");
74+
~~~`,
75+
options: [{ required: ["js"] }],
76+
},
77+
{
78+
code: `~~~js foo
79+
console.log("Hello, world!");
80+
~~~`,
81+
options: [{ required: ["js"] }],
82+
},
83+
{
84+
code: `~~~ js
85+
console.log("Hello, world!");
86+
~~~`,
87+
options: [{ required: ["js"] }],
88+
},
89+
{
90+
code: `~~~JS
91+
console.log("Hello, world!");
92+
~~~`,
93+
options: [{ required: ["JS"] }],
94+
},
5395
],
5496
invalid: [
5597
{
@@ -61,7 +103,7 @@ console.log("Hello, world!");
61103
messageId: "missingLanguage",
62104
line: 1,
63105
column: 1,
64-
endLine: 3,
106+
endLine: 1,
65107
endColumn: 4,
66108
},
67109
],
@@ -75,7 +117,7 @@ console.log("Hello, world!");
75117
messageId: "missingLanguage",
76118
line: 1,
77119
column: 1,
78-
endLine: 3,
120+
endLine: 1,
79121
endColumn: 4,
80122
},
81123
],
@@ -91,8 +133,152 @@ console.log("Hello, world!");
91133
data: { lang: "javascript" },
92134
line: 1,
93135
column: 1,
94-
endLine: 3,
95-
endColumn: 4,
136+
endLine: 1,
137+
endColumn: 14,
138+
},
139+
],
140+
},
141+
{
142+
code: `\`\`\`\`javascript
143+
console.log("Hello, world!");
144+
\`\`\`\``,
145+
options: [{ required: ["js"] }],
146+
errors: [
147+
{
148+
messageId: "disallowedLanguage",
149+
data: { lang: "javascript" },
150+
line: 1,
151+
column: 1,
152+
endLine: 1,
153+
endColumn: 15,
154+
},
155+
],
156+
},
157+
{
158+
code: `\`\`\`js
159+
console.log("Hello, world!");
160+
\`\`\``,
161+
options: [{ required: ["JS"] }],
162+
errors: [
163+
{
164+
messageId: "disallowedLanguage",
165+
data: { lang: "js" },
166+
line: 1,
167+
column: 1,
168+
endLine: 1,
169+
endColumn: 6,
170+
},
171+
],
172+
},
173+
{
174+
code: `\`\`\` js
175+
console.log("Hello, world!");
176+
\`\`\``,
177+
options: [{ required: ["JS"] }],
178+
errors: [
179+
{
180+
messageId: "disallowedLanguage",
181+
data: { lang: "js" },
182+
line: 1,
183+
column: 1,
184+
endLine: 1,
185+
endColumn: 7,
186+
},
187+
],
188+
},
189+
{
190+
code: `\`\`\`js foo
191+
console.log("Hello, world!");
192+
\`\`\``,
193+
options: [{ required: ["foo"] }],
194+
errors: [
195+
{
196+
messageId: "disallowedLanguage",
197+
data: { lang: "js" },
198+
line: 1,
199+
column: 1,
200+
endLine: 1,
201+
endColumn: 6,
202+
},
203+
],
204+
},
205+
{
206+
code: `~~~javascript
207+
console.log("Hello, world!");
208+
~~~`,
209+
options: [{ required: ["js"] }],
210+
errors: [
211+
{
212+
messageId: "disallowedLanguage",
213+
data: { lang: "javascript" },
214+
line: 1,
215+
column: 1,
216+
endLine: 1,
217+
endColumn: 14,
218+
},
219+
],
220+
},
221+
{
222+
code: `~~~~javascript
223+
console.log("Hello, world!");
224+
~~~~`,
225+
options: [{ required: ["js"] }],
226+
errors: [
227+
{
228+
messageId: "disallowedLanguage",
229+
data: { lang: "javascript" },
230+
line: 1,
231+
column: 1,
232+
endLine: 1,
233+
endColumn: 15,
234+
},
235+
],
236+
},
237+
{
238+
code: `~~~js
239+
console.log("Hello, world!");
240+
~~~`,
241+
options: [{ required: ["JS"] }],
242+
errors: [
243+
{
244+
messageId: "disallowedLanguage",
245+
data: { lang: "js" },
246+
line: 1,
247+
column: 1,
248+
endLine: 1,
249+
endColumn: 6,
250+
},
251+
],
252+
},
253+
{
254+
code: `~~~ js
255+
console.log("Hello, world!");
256+
~~~`,
257+
options: [{ required: ["JS"] }],
258+
errors: [
259+
{
260+
messageId: "disallowedLanguage",
261+
data: { lang: "js" },
262+
line: 1,
263+
column: 1,
264+
endLine: 1,
265+
endColumn: 7,
266+
},
267+
],
268+
},
269+
{
270+
code: `~~~js foo
271+
console.log("Hello, world!");
272+
~~~`,
273+
options: [{ required: ["foo"] }],
274+
errors: [
275+
{
276+
messageId: "disallowedLanguage",
277+
data: { lang: "js" },
278+
line: 1,
279+
column: 1,
280+
endLine: 1,
281+
endColumn: 6,
96282
},
97283
],
98284
},

0 commit comments

Comments
 (0)