From 3f214153d9d7f098bd34ead676a9bf5bb162aa0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barth=C3=A9lemy=20Laurans?= Date: Sun, 27 Oct 2024 16:24:14 +0100 Subject: [PATCH 1/2] Handle leading space in comment section --- src/Validator.js | 40 +++++++++++++++++-- src/Validator.test.js | 7 +++- src/__fixtures__/indentation.spaces.fixture | 3 ++ .../indentation.spaces.invalid.fixture | 3 ++ src/__fixtures__/indentation.tabs.fixture | 3 ++ 5 files changed, 50 insertions(+), 6 deletions(-) diff --git a/src/Validator.js b/src/Validator.js index b885c28..ef36eca 100644 --- a/src/Validator.js +++ b/src/Validator.js @@ -19,6 +19,7 @@ const REGEXP_EOL = new RegExp(EOF); const REGEXP_INDENTATION_TABS = /^\t*(?!\s).*$/; // leading tabs without leading spaces const REGEXP_INDENTATION_TABS_WITH_BOM = /^\t*(?! |\t).*$/; // leading tabs without leading spaces (allows BOM) +const REGEXP_INDENTATION_TABS_INSIDE_COMMENT = /^\t* ?\*/; // leading tabs inside comment might have an extra space const REGEXP_INDENTATION_SPACES = /^ *(?!\s).*$/; // leading spaces without leading tabs const REGEXP_INDENTATION_SPACES_WITH_BOM = /^ *(?!\t).*$/; // leading spaces without leading tabs (allows BOM) @@ -90,8 +91,29 @@ class Validator { this._validateEndOfLine(); // Validate single lines: + let insideComment = false; + const enteringCommentRegex = /^\s*\/\*\*?/; + const exitingCommentRegex = /^\s*\*\/$/; + const exitingCommentSameLineRegex = /\*\/\s*$/; + this._lines.forEach((line, index) => { - this._validateIndentation(line, index); + if (index > 0) { + const previousLine = this._lines[index - 1]; + + if ( + enteringCommentRegex.test(previousLine) + && !exitingCommentSameLineRegex.test(previousLine) + ) { + insideComment = true; + } else if ( + insideComment + && exitingCommentRegex.test(previousLine) + ) { + insideComment = false; + } + } + + this._validateIndentation(line, index, insideComment); this._validateTrailingspaces(line, index); }); @@ -525,14 +547,18 @@ class Validator { * Check indentations * @private */ - _validateIndentation(line, index) { + _validateIndentation(line, index, insideComment) { if (!this._ignoredLines[index] && typeof this._settings.indentation === 'string' && typeof line === 'string') { const tabsRegExpFinal = this._settings.allowsBOM ? REGEXP_INDENTATION_TABS_WITH_BOM - : REGEXP_INDENTATION_TABS; + : ( + insideComment + ? REGEXP_INDENTATION_TABS_INSIDE_COMMENT + : REGEXP_INDENTATION_TABS + ); const spacesRegExpFinal = this._settings.allowsBOM ? REGEXP_INDENTATION_SPACES_WITH_BOM : REGEXP_INDENTATION_SPACES; @@ -561,7 +587,13 @@ class Validator { // Indentation correct, is amount of spaces correct? if (typeof this._settings.spaces === 'number') { indent = line.match(REGEXP_LEADING_SPACES)[1].length; - if (indent % this._settings.spaces !== 0) { + if ( + indent % this._settings.spaces !== 0 + && ( + !insideComment + || (indent - 1) % this._settings.spaces !== 0 + ) + ) { // Indentation incorrect, create message and report: spacesExpected = Math.round(indent / this._settings.spaces) * this._settings.spaces; message = MESSAGES.INDENTATION_SPACES_AMOUNT.message diff --git a/src/Validator.test.js b/src/Validator.test.js index 631a54b..2e0e547 100644 --- a/src/Validator.test.js +++ b/src/Validator.test.js @@ -692,6 +692,9 @@ describe('The validator', () => { '5': [extend({}, Messages.INDENTATION_SPACES, {line: 5})], '6': [extend({}, Messages.INDENTATION_SPACES, {line: 6})], '7': [extend({}, Messages.INDENTATION_SPACES, {line: 7})], + '8': [extend({}, Messages.INDENTATION_SPACES, {line: 8})], + '9': [extend({}, Messages.INDENTATION_SPACES, {line: 9})], + '10': [extend({}, Messages.INDENTATION_SPACES, {line: 10})], }, }); }); @@ -736,13 +739,13 @@ describe('The validator', () => { indent: 5, }, })], - '5': [extend({}, Messages.INDENTATION_SPACES_AMOUNT, { + '8': [extend({}, Messages.INDENTATION_SPACES_AMOUNT, { message: Messages .INDENTATION_SPACES_AMOUNT .message .replace('{a}', 12) .replace('{b}', 10), - line: 5, + line: 8, payload: { expected: 12, indent: 10, diff --git a/src/__fixtures__/indentation.spaces.fixture b/src/__fixtures__/indentation.spaces.fixture index 865ab25..57d1b25 100644 --- a/src/__fixtures__/indentation.spaces.fixture +++ b/src/__fixtures__/indentation.spaces.fixture @@ -2,6 +2,9 @@ var foo = 'bar'; var index = 0; while (index < foo.length) { + /** + * This line and the next should not fail + */ console.log(foo.charAt(index)); index++; } diff --git a/src/__fixtures__/indentation.spaces.invalid.fixture b/src/__fixtures__/indentation.spaces.invalid.fixture index a91d99c..ecac182 100644 --- a/src/__fixtures__/indentation.spaces.invalid.fixture +++ b/src/__fixtures__/indentation.spaces.invalid.fixture @@ -2,6 +2,9 @@ var foo = 'bar'; var index = 0; while (index < foo.length) { + /** + * Not incorrect + */ console.log(foo.charAt(index)); index++; } diff --git a/src/__fixtures__/indentation.tabs.fixture b/src/__fixtures__/indentation.tabs.fixture index 41bfa00..7d8b5ac 100644 --- a/src/__fixtures__/indentation.tabs.fixture +++ b/src/__fixtures__/indentation.tabs.fixture @@ -2,6 +2,9 @@ var foo = 'bar'; var index = 0; while (index < foo.length) { + /** + * This line and the next should not fail + */ console.log(foo.charAt(index)); index++; } From 5bef38749953ca1077440b41125e7892bd5889a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barth=C3=A9lemy=20Laurans?= Date: Sun, 27 Oct 2024 16:25:17 +0100 Subject: [PATCH 2/2] Take advantage of scoped variable and add some spacing for readability --- src/Validator.js | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/src/Validator.js b/src/Validator.js index ef36eca..da53a36 100644 --- a/src/Validator.js +++ b/src/Validator.js @@ -563,12 +563,6 @@ class Validator { ? REGEXP_INDENTATION_SPACES_WITH_BOM : REGEXP_INDENTATION_SPACES; - let spacesExpected; - let indent; - let message; - let data; - let payload; - switch (this._settings.indentation) { case 'tabs': if (!tabsRegExpFinal.test(line)) { @@ -586,7 +580,8 @@ class Validator { } else { // Indentation correct, is amount of spaces correct? if (typeof this._settings.spaces === 'number') { - indent = line.match(REGEXP_LEADING_SPACES)[1].length; + const indent = line.match(REGEXP_LEADING_SPACES)[1].length; + if ( indent % this._settings.spaces !== 0 && ( @@ -595,19 +590,18 @@ class Validator { ) ) { // Indentation incorrect, create message and report: - spacesExpected = Math.round(indent / this._settings.spaces) * this._settings.spaces; - message = MESSAGES.INDENTATION_SPACES_AMOUNT.message + const spacesExpected = Math.round(indent / this._settings.spaces) * this._settings.spaces; + const message = MESSAGES.INDENTATION_SPACES_AMOUNT.message .replace('{a}', spacesExpected) .replace('{b}', indent); - data = {message: message}; + let data = {message}; data = extend({}, MESSAGES.INDENTATION_SPACES_AMOUNT, data); - payload = { - expected: spacesExpected, - indent: indent, - }; - this._report(data, index + 1, payload); + this._report(data, index + 1, { + expected: spacesExpected, + indent, + }); } } }