Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug Tests",
"internalConsoleOptions": "openOnSessionStart",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/node_modules/.bin/mocha",
"args": [
"--colors"
]

}
]
}
29 changes: 27 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,35 @@
//////////////////////////////////////
// Git
//////////////////////////////////////
"git.inputValidation": "warn",
"git.inputValidation": true,
"git.inputValidationSubjectLength": 50,
"git.inputValidationLength": 72,
"cSpell.words": [
"lintable"
]
],
//////////////////////////////////////
// Editor
//////////////////////////////////////
"editor.indentSize": 2,
"editor.tabSize": 2,
"[html,js]": {
"editor.indentSize": 4,
"editor.tabSize": 4
},
"editor.insertSpaces": true,
"editor.detectIndentation": false,
"editor.renderFinalNewline": "on",
"editor.wordWrap": "wordWrapColumn",
"editor.wordWrapColumn": 100, // toggle via Alt + Z shortcut
"editor.mouseWheelZoom": true,
"editor.rulers": [
{
"column": 80, // soft limit
"color": "#e5e5e5"
},
{
"column": 100, // hard limit
"color": "#c9c9c9"
}
],
}
28 changes: 17 additions & 11 deletions lib/preprocess.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ const HASH = "566513c5d83ac26e15414f2754"; // to avoid collisions with user code
* location of messages in the postprocess step later.
* @param {string} text text of the file
* @param {string} filename filename of the file
* @param {(id) => string} dummyString dummy string to replace ERB tags with
* (this is language-specific). Should be
* a function that takes an id and returns
* a UNIQUE dummy string.
* @param {(id) => string} toDummyString function that takes an id and the
* matched text and returns a dummy string to replace the matched text with.
* This dummy string must be unique.
* @returns {Array<{ filename: string, text: string }>} source code blocks to lint
*/
function preprocess(text, filename, dummyString) {
function preprocess(text, filename, toDummyString) {
let lintableTextArr = text.split("");

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

// Columns
const dummy = dummyString(matchedId);
const dummy = toDummyString(matchedId, matchText);
const coordStartIndex = indexToColumn(text, startIndex);
const endColumnAfter = coordStartIndex.column + dummy.length;
const coordEndIndex = indexToColumn(text, endIndex);
Expand Down Expand Up @@ -88,17 +87,24 @@ function replaceTextWithDummy(lintableTextArr, startIndex, length, dummy) {
}

function preprocessJs(text, filename) {
function dummyString(id) {
return `/* eslint-disable */{}/* ${HASH} ${id} *//* eslint-enable */`;
function wrapInEslintDisable(text) {
return `/* eslint-disable */${text}/* eslint-enable */`;
}
return preprocess(text, filename, dummyString);

function toDummyString(id, matchText) {
if (matchText.startsWith("<%=")) {
return wrapInEslintDisable(`{}/* eslint-plugin-erb ${HASH} ${id} */`);
}
return wrapInEslintDisable(`/* eslint-plugin-erb ${HASH} ${id} */`);
}
Comment on lines +90 to +99
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just gonna leave this idea here, I don't feel like forking this repo today:

Suggested change
function wrapInEslintDisable(text) {
return `/* eslint-disable */${text}/* eslint-enable */`;
}
return preprocess(text, filename, dummyString);
function toDummyString(id, matchText) {
if (matchText.startsWith("<%=")) {
return wrapInEslintDisable(`{}/* eslint-plugin-erb ${HASH} ${id} */`);
}
return wrapInEslintDisable(`/* eslint-plugin-erb ${HASH} ${id} */`);
}
function toDummyString(id, matchText) {
let replacement = "";
if (matchText.startsWith("<%=")) replacement = "{}";
const tagged = `${replacement}/* eslint-plugin-erb ${HASH} ${id} */`;
const wrapped = `/* eslint-disable */${tagged}/* eslint-enable */`;
return wrapped;
}

return preprocess(text, filename, toDummyString);
}

function preprocessHtml(text, filename) {
function dummyString(id) {
function toDummyString(id, _matchText) {
return `<!-- ${HASH} ${id} -->`;
}
return preprocess(text, filename, dummyString);
return preprocess(text, filename, toDummyString);
}

module.exports = {
Expand Down
4 changes: 4 additions & 0 deletions tests/eslint.test.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ module.exports = [
},
},
ignores: ["**/*.html**"],
linterOptions: {
// see https://github.com/Splines/eslint-plugin-erb/releases/tag/v2.0.1
reportUnusedDisableDirectives: "off",
},
},
{
// HTML linting (aside from erb_lint)
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/multiple-erb-in-one-line.expected.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
console.log ("Hi "); <%# This is a comment %>console.log("whatever"); <% if true %> console.log("true"); <% end %>
console.log ("Hi "); <%# This is a comment %>console.log("whatever"); <% if true %> console.log("true");<% end %>
2 changes: 1 addition & 1 deletion tests/fixtures/multiple-erb-in-one-line.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions tests/fixtures/no-output-erb-tag.expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
OSM = {
<% if something %>
MATOMO: <%= Settings.matomo.to_json %>,
<% end %>
MAX_REQUEST_AREA: <%= Custom.to_yml %>
};
6 changes: 6 additions & 0 deletions tests/fixtures/no-output-erb-tag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
OSM = {
<% if something %>
MATOMO : <%= Settings.matomo.to_json %>,
<% end %>
MAX_REQUEST_AREA: <%= Custom.to_yml %>
}
5 changes: 3 additions & 2 deletions tests/integration.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const fs = require("fs");
const assert = require("chai").assert;
const expect = require("chai").expect;
const { ESLint } = require("eslint");

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

describe("Integration tests (JS)", () => {
Expand All @@ -22,6 +22,7 @@ describe("Integration tests (JS)", () => {
"multiple-erb-in-one-line",
"one-liner",
"multiple-properties",
"no-output-erb-tag",
];
mapFiles.forEach((name) => {
it(`performs linting as we expect it on ${name}.js`, async () => {
Expand Down
Loading