forked from SonarSource/SonarJS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce1d001
commit 14d73a7
Showing
3 changed files
with
86 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import supertest from 'supertest'; | ||
import foo from 'supertest'; | ||
|
||
describe('supertest', function () { // Compliant | ||
it('should work when assigned to a variable named "supertest" and the "get" HTTP verb', function () { | ||
return supertest(app).get(`/foo/bar`).expect('Content-Type', /json/u).expect(200); | ||
}); | ||
|
||
it('should work regardless of the HTTP verb', function () { // Compliant | ||
return supertest(app).foo(`/foo/bar`).expect('Content-Type', /json/u).expect(200); | ||
}); | ||
|
||
it('should work regardless of the name of the assigned variable', function () { // Compliant | ||
return foo(app).get(`/foo/bar`).expect('Content-Type', /json/u).expect(200); | ||
}); | ||
|
||
it('should fail when no assertion', function () { // Noncompliant | ||
return supertest(app).get(`/foo/bar`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* SonarQube JavaScript Plugin | ||
* Copyright (C) 2011-2024 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
import { Rule } from 'eslint'; | ||
import { getFullyQualifiedName, getImportDeclarations, getRequireCalls } from '.'; | ||
import * as estree from 'estree'; | ||
|
||
export namespace Supertest { | ||
export function isImported(context: Rule.RuleContext): boolean { | ||
return ( | ||
getRequireCalls(context).some( | ||
r => r.arguments[0].type === 'Literal' && r.arguments[0].value === 'supertest', | ||
) || getImportDeclarations(context).some(i => i.source.value === 'supertest') | ||
); | ||
} | ||
|
||
export function isAssertion(context: Rule.RuleContext, node: estree.Node) { | ||
const fqn = extractFQNForCallExpression(context, node); | ||
|
||
if (!fqn) { | ||
return false; | ||
} | ||
|
||
const names = fqn.split('.'); | ||
|
||
/** | ||
* supertest assertions look like `[alias resolved to supertest](...).[HTTP verb](...).expect(...)`, typically: | ||
* `supertest(application).get('/foo').expect(200)` | ||
* hence only the first and third values matter, the second one being an HTTP verb irrelevant for assertion detection | ||
*/ | ||
return names.length >= 3 && names[0] === 'supertest' && names[2] === 'expect'; | ||
} | ||
|
||
function extractFQNForCallExpression(context: Rule.RuleContext, node: estree.Node) { | ||
if (node.type !== 'CallExpression') { | ||
return undefined; | ||
} | ||
|
||
return getFullyQualifiedName(context, node); | ||
} | ||
} |