Skip to content

Commit df96a45

Browse files
authored
Fix treatment of no-output ERB tags (#16)
* Add launch config for test debugging * Disable `reportUnusedDisableDirectives` * Add no-output-erb tag test case * Implement new comment replacement * Add to testset & fix adjust other test case * Setup editor vscode config * Move wrapping function to inline * Delete unnecessary linter option
1 parent 9c26b35 commit df96a45

File tree

9 files changed

+87
-17
lines changed

9 files changed

+87
-17
lines changed

.vscode/launch.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Debug Tests",
11+
"internalConsoleOptions": "openOnSessionStart",
12+
"skipFiles": [
13+
"<node_internals>/**"
14+
],
15+
"program": "${workspaceFolder}/node_modules/.bin/mocha",
16+
"args": [
17+
"--colors"
18+
]
19+
20+
}
21+
]
22+
}

.vscode/settings.json

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,35 @@
2121
//////////////////////////////////////
2222
// Git
2323
//////////////////////////////////////
24-
"git.inputValidation": "warn",
24+
"git.inputValidation": true,
2525
"git.inputValidationSubjectLength": 50,
2626
"git.inputValidationLength": 72,
2727
"cSpell.words": [
2828
"lintable"
29-
]
29+
],
30+
//////////////////////////////////////
31+
// Editor
32+
//////////////////////////////////////
33+
"editor.indentSize": 2,
34+
"editor.tabSize": 2,
35+
"[html,js]": {
36+
"editor.indentSize": 4,
37+
"editor.tabSize": 4
38+
},
39+
"editor.insertSpaces": true,
40+
"editor.detectIndentation": false,
41+
"editor.renderFinalNewline": "on",
42+
"editor.wordWrap": "wordWrapColumn",
43+
"editor.wordWrapColumn": 100, // toggle via Alt + Z shortcut
44+
"editor.mouseWheelZoom": true,
45+
"editor.rulers": [
46+
{
47+
"column": 80, // soft limit
48+
"color": "#e5e5e5"
49+
},
50+
{
51+
"column": 100, // hard limit
52+
"color": "#c9c9c9"
53+
}
54+
],
3055
}

lib/preprocess.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@ const HASH = "566513c5d83ac26e15414f2754"; // to avoid collisions with user code
1919
* location of messages in the postprocess step later.
2020
* @param {string} text text of the file
2121
* @param {string} filename filename of the file
22-
* @param {(id) => string} dummyString dummy string to replace ERB tags with
23-
* (this is language-specific). Should be
24-
* a function that takes an id and returns
25-
* a UNIQUE dummy string.
22+
* @param {(id) => string} toDummyString function that takes an id and the
23+
* matched text and returns a dummy string to replace the matched text with.
24+
* This dummy string must be unique.
2625
* @returns {Array<{ filename: string, text: string }>} source code blocks to lint
2726
*/
28-
function preprocess(text, filename, dummyString) {
27+
function preprocess(text, filename, toDummyString) {
2928
let lintableTextArr = text.split("");
3029

3130
let match;
@@ -47,7 +46,7 @@ function preprocess(text, filename, dummyString) {
4746
numAddLines += matchLines.length - 1;
4847

4948
// Columns
50-
const dummy = dummyString(matchedId);
49+
const dummy = toDummyString(matchedId, matchText);
5150
const coordStartIndex = indexToColumn(text, startIndex);
5251
const endColumnAfter = coordStartIndex.column + dummy.length;
5352
const coordEndIndex = indexToColumn(text, endIndex);
@@ -88,17 +87,24 @@ function replaceTextWithDummy(lintableTextArr, startIndex, length, dummy) {
8887
}
8988

9089
function preprocessJs(text, filename) {
91-
function dummyString(id) {
92-
return `/* eslint-disable */{}/* ${HASH} ${id} *//* eslint-enable */`;
90+
function wrapInEslintDisable(text) {
91+
return `/* eslint-disable */${text}/* eslint-enable */`;
9392
}
94-
return preprocess(text, filename, dummyString);
93+
94+
function toDummyString(id, matchText) {
95+
if (matchText.startsWith("<%=")) {
96+
return wrapInEslintDisable(`{}/* eslint-plugin-erb ${HASH} ${id} */`);
97+
}
98+
return wrapInEslintDisable(`/* eslint-plugin-erb ${HASH} ${id} */`);
99+
}
100+
return preprocess(text, filename, toDummyString);
95101
}
96102

97103
function preprocessHtml(text, filename) {
98-
function dummyString(id) {
104+
function toDummyString(id, _matchText) {
99105
return `<!-- ${HASH} ${id} -->`;
100106
}
101-
return preprocess(text, filename, dummyString);
107+
return preprocess(text, filename, toDummyString);
102108
}
103109

104110
module.exports = {

tests/eslint.test.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ module.exports = [
3737
},
3838
},
3939
ignores: ["**/*.html**"],
40+
linterOptions: {
41+
// see https://github.com/Splines/eslint-plugin-erb/releases/tag/v2.0.1
42+
reportUnusedDisableDirectives: "off",
43+
},
4044
},
4145
{
4246
// HTML linting (aside from erb_lint)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
console.log ("Hi "); <%# This is a comment %>console.log("whatever"); <% if true %> console.log("true"); <% end %>
1+
console.log ("Hi "); <%# This is a comment %>console.log("whatever"); <% if true %> console.log("true");<% end %>

tests/fixtures/multiple-erb-in-one-line.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
OSM = {
2+
<% if something %>
3+
MATOMO: <%= Settings.matomo.to_json %>,
4+
<% end %>
5+
MAX_REQUEST_AREA: <%= Custom.to_yml %>
6+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
OSM = {
2+
<% if something %>
3+
MATOMO : <%= Settings.matomo.to_json %>,
4+
<% end %>
5+
MAX_REQUEST_AREA: <%= Custom.to_yml %>
6+
}

tests/integration.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const fs = require("fs");
2-
const assert = require("chai").assert;
2+
const expect = require("chai").expect;
33
const { ESLint } = require("eslint");
44

55
const eslint = new ESLint({
@@ -11,7 +11,7 @@ async function runTest(filename, expectedFilename) {
1111
const testCode = fs.readFileSync(filename, "utf-8");
1212
const result = await eslint.lintText(testCode, { filePath: filename });
1313
const expectedCode = fs.readFileSync(expectedFilename, "utf-8");
14-
assert.strictEqual(result[0].output, expectedCode);
14+
expect(result[0].output).to.equal(expectedCode);
1515
}
1616

1717
describe("Integration tests (JS)", () => {
@@ -22,6 +22,7 @@ describe("Integration tests (JS)", () => {
2222
"multiple-erb-in-one-line",
2323
"one-liner",
2424
"multiple-properties",
25+
"no-output-erb-tag",
2526
];
2627
mapFiles.forEach((name) => {
2728
it(`performs linting as we expect it on ${name}.js`, async () => {

0 commit comments

Comments
 (0)