Skip to content

Handle multiline spaced comments #298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
64 changes: 45 additions & 19 deletions src/Validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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);
});

Expand Down Expand Up @@ -525,24 +547,22 @@ 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;

let spacesExpected;
let indent;
let message;
let data;
let payload;

switch (this._settings.indentation) {
case 'tabs':
if (!tabsRegExpFinal.test(line)) {
Expand All @@ -560,22 +580,28 @@ class Validator {
} else {
// 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) {
const indent = line.match(REGEXP_LEADING_SPACES)[1].length;

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
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,
});
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/Validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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})],
},
});
});
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions src/__fixtures__/indentation.spaces.fixture
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
Expand Down
3 changes: 3 additions & 0 deletions src/__fixtures__/indentation.spaces.invalid.fixture
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
var foo = 'bar';
var index = 0;
while (index < foo.length) {
/**
* Not incorrect
*/
console.log(foo.charAt(index));
index++;
}
Expand Down
3 changes: 3 additions & 0 deletions src/__fixtures__/indentation.tabs.fixture
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
Expand Down