Skip to content

Commit

Permalink
Merge pull request #2940 from judithhinlung/jlung/simple-unless-locat…
Browse files Browse the repository at this point in the history
…ion-reporting-fix

Fixes location reporting in `simple-unless` rule
  • Loading branch information
lin-ll committed Jul 18, 2023
2 parents 4c966df + c63e839 commit 4945c83
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 29 deletions.
22 changes: 7 additions & 15 deletions lib/rules/simple-unless.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,29 +105,26 @@ export default class SimpleUnless extends Rule {
return;
}
let node = block.program;
let loc = node.loc.end;
let actual = '{{else}}';

this._logMessage(node, messages.followingElseBlock, loc.line, loc.column, actual, true);
this._logMessage(node, messages.followingElseBlock, actual, true);
}

_followingElseIfBlock(block) {
let inverse = block.inverse;
let node = block.program;
let loc = node.loc.end;
let parameter = inverse.body[0].params[0].original;
let actual = `{{else if ${parameter}}}`;

this._logMessage(node, messages.followingElseBlock, loc.line, loc.column, actual);
this._logMessage(node, messages.followingElseBlock, actual);
}

_asElseUnlessBlock(block) {
let inverse = block.inverse;
let node = inverse.body[0];
let loc = node.loc.start;
let actual = '{{else unless ...';

this._logMessage(node, messages.asElseUnlessBlock, loc.line, loc.column, actual);
this._logMessage(node, messages.asElseUnlessBlock, actual);
}

_withHelper(node, fixMode) {
Expand All @@ -145,33 +142,30 @@ export default class SimpleUnless extends Rule {
for (const param of params) {
if (param.type === 'SubExpression') {
if (++helperCount > maxHelpers && maxHelpers > -1) {
let loc = param.loc.start;
let actual = `{{unless ${helperCount > 1 ? '(... ' : ''}(${param.path.original} ...`;
let message = `${messages.withHelper} MaxHelpers: ${maxHelpers}`;

this._logMessage(param, message, loc.line, loc.column, actual, true);
this._logMessage(param, message, actual, true);
shouldFix = true;
}

if (allowlist.length > 0 && !allowlist.includes(param.path.original)) {
let loc = param.loc.start;
let actual = `{{unless ${helperCount > 1 ? '(... ' : ''}(${param.path.original} ...`;
let message = `${messages.withHelper} Allowed helper${
allowlist.length > 1 ? 's' : ''
}: ${allowlist.toString()}`;

this._logMessage(param, message, loc.line, loc.column, actual, true);
this._logMessage(param, message, actual, true);
shouldFix = true;
}

if (denylist.length > 0 && denylist.includes(param.path.original)) {
let loc = param.loc.start;
let actual = `{{unless ${helperCount > 1 ? '(... ' : ''}(${param.path.original} ...`;
let message = `${messages.withHelper} Restricted helper${
denylist.length > 1 ? 's' : ''
}: ${denylist.toString()}`;

this._logMessage(param, message, loc.line, loc.column, actual, true);
this._logMessage(param, message, actual, true);
shouldFix = true;
}

Expand Down Expand Up @@ -209,12 +203,10 @@ export default class SimpleUnless extends Rule {
);
}

_logMessage(node, message, line, column, source, isFixable = false) {
_logMessage(node, message, source, isFixable = false) {
return this.log({
message,
node,
line,
column,
source,
isFixable,
});
Expand Down
28 changes: 14 additions & 14 deletions test/unit/rules/simple-unless-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,12 @@ generateRuleTests({
expect(results).toMatchInlineSnapshot(`
[
{
"column": 0,
"column": 23,
"endColumn": 0,
"endLine": 3,
"filePath": "layout.hbs",
"isFixable": true,
"line": 3,
"line": 1,
"message": "Using an {{else}} block with {{unless}} should be avoided.",
"rule": "simple-unless",
"severity": 2,
Expand All @@ -181,12 +181,12 @@ generateRuleTests({
expect(results).toMatchInlineSnapshot(`
[
{
"column": 0,
"column": 23,
"endColumn": 0,
"endLine": 3,
"filePath": "layout.hbs",
"isFixable": true,
"line": 3,
"line": 1,
"message": "Using an {{else}} block with {{unless}} should be avoided.",
"rule": "simple-unless",
"severity": 2,
Expand Down Expand Up @@ -216,12 +216,12 @@ generateRuleTests({
expect(results).toMatchInlineSnapshot(`
[
{
"column": 0,
"column": 23,
"endColumn": 0,
"endLine": 2,
"filePath": "layout.hbs",
"isFixable": true,
"line": 2,
"line": 1,
"message": "Using an {{else}} block with {{unless}} should be avoided.",
"rule": "simple-unless",
"severity": 2,
Expand All @@ -244,12 +244,12 @@ generateRuleTests({
expect(results).toMatchInlineSnapshot(`
[
{
"column": 0,
"column": 23,
"endColumn": 0,
"endLine": 3,
"filePath": "layout.hbs",
"isFixable": false,
"line": 3,
"line": 1,
"message": "Using an {{else}} block with {{unless}} should be avoided.",
"rule": "simple-unless",
"severity": 2,
Expand All @@ -274,12 +274,12 @@ generateRuleTests({
expect(results).toMatchInlineSnapshot(`
[
{
"column": 0,
"column": 23,
"endColumn": 0,
"endLine": 3,
"filePath": "layout.hbs",
"isFixable": false,
"line": 3,
"line": 1,
"message": "Using an {{else}} block with {{unless}} should be avoided.",
"rule": "simple-unless",
"severity": 2,
Expand All @@ -304,12 +304,12 @@ generateRuleTests({
expect(results).toMatchInlineSnapshot(`
[
{
"column": 0,
"column": 23,
"endColumn": 0,
"endLine": 3,
"filePath": "layout.hbs",
"isFixable": false,
"line": 3,
"line": 1,
"message": "Using an {{else}} block with {{unless}} should be avoided.",
"rule": "simple-unless",
"severity": 2,
Expand Down Expand Up @@ -494,12 +494,12 @@ generateRuleTests({
expect(results).toMatchInlineSnapshot(`
[
{
"column": 0,
"column": 20,
"endColumn": 0,
"endLine": 3,
"filePath": "layout.hbs",
"isFixable": true,
"line": 3,
"line": 1,
"message": "Using an {{else}} block with {{unless}} should be avoided.",
"rule": "simple-unless",
"severity": 2,
Expand Down

0 comments on commit 4945c83

Please sign in to comment.