From 7789853017282020d6d290b5a8dfce53e539bcf2 Mon Sep 17 00:00:00 2001 From: Yassin Kammoun <52890329+yassin-kammoun-sonarsource@users.noreply.github.com> Date: Mon, 16 Oct 2023 10:57:51 +0200 Subject: [PATCH] Fix quality gate (#4272) --- packages/bridge/tests/tools/request.ts | 2 +- packages/css/tests/tools/tester/tester.ts | 2 +- packages/jsts/src/rules/helpers/ast.ts | 1 - .../rule/no-missing-sonar-runtime.ts | 30 ++++++++++--------- .../tools/testers/comment-based/checker.ts | 2 +- .../testers/comment-based/helpers/issues.ts | 2 +- .../comment-based/helpers/locations.ts | 6 +--- .../comment-based/helpers/quickfixes.ts | 4 +-- .../tests/tools/testers/typescript/tester.ts | 2 +- packages/shared/tests/helpers/debug.test.ts | 18 ++++++++++- pom.xml | 1 + 11 files changed, 42 insertions(+), 28 deletions(-) diff --git a/packages/bridge/tests/tools/request.ts b/packages/bridge/tests/tools/request.ts index 065e4823090..b75f43a451e 100644 --- a/packages/bridge/tests/tools/request.ts +++ b/packages/bridge/tests/tools/request.ts @@ -28,7 +28,7 @@ export function request(server: http.Server, path: string, method: string, data: host: '127.0.0.1', path, method, - port: (server.address()).port, + port: (server.address() as AddressInfo).port, headers: { 'Content-Type': 'application/json', }, diff --git a/packages/css/tests/tools/tester/tester.ts b/packages/css/tests/tools/tester/tester.ts index fc343e371dc..842d7648897 100644 --- a/packages/css/tests/tools/tester/tester.ts +++ b/packages/css/tests/tools/tester/tester.ts @@ -29,7 +29,7 @@ export type InvalidAssertion = { class StylelintRuleTester { private readonly config: stylelint.Config; - constructor(rule: { ruleName: string; rule: stylelint.Rule }) { + constructor(rule: { ruleName: string; rule: stylelint.Rule }) { stylelint.rules[rule.ruleName] = rule.rule; this.config = { rules: { [rule.ruleName]: true } }; } diff --git a/packages/jsts/src/rules/helpers/ast.ts b/packages/jsts/src/rules/helpers/ast.ts index e62e4645e5d..4a375a2acd6 100644 --- a/packages/jsts/src/rules/helpers/ast.ts +++ b/packages/jsts/src/rules/helpers/ast.ts @@ -412,7 +412,6 @@ function resolveIdentifiersAcc( } } -// TODO Drop this function and replace it with `getProperty` export function getObjectExpressionProperty( node: estree.Node | undefined | null, propertyKey: string, diff --git a/packages/jsts/tests/tools/sonar-runtime/rule/no-missing-sonar-runtime.ts b/packages/jsts/tests/tools/sonar-runtime/rule/no-missing-sonar-runtime.ts index e1607680a80..47237fc9b1f 100644 --- a/packages/jsts/tests/tools/sonar-runtime/rule/no-missing-sonar-runtime.ts +++ b/packages/jsts/tests/tools/sonar-runtime/rule/no-missing-sonar-runtime.ts @@ -61,20 +61,22 @@ export const rule: Rule.RuleModule = { return; } const maybeMeta = getObjectExpressionProperty(node, 'meta'); - if (maybeMeta) { - const maybeSchema = getObjectExpressionProperty(maybeMeta.value, 'schema'); - if (maybeSchema && maybeSchema.value.type === 'ArrayExpression') { - const schema = maybeSchema.value; - for (const element of schema.elements) { - const maybeEnum = getObjectExpressionProperty(element, 'enum'); - if (maybeEnum) { - isSecondaryLocationEnabled = - maybeEnum.value.type === 'ArrayExpression' && - maybeEnum.value.elements.length === 1 && - maybeEnum.value.elements[0].type === 'Identifier' && - maybeEnum.value.elements[0].name === 'SONAR_RUNTIME'; - } - } + if (!maybeMeta) { + return; + } + const maybeSchema = getObjectExpressionProperty(maybeMeta.value, 'schema'); + if (maybeSchema?.value.type !== 'ArrayExpression') { + return; + } + const schema = maybeSchema.value; + for (const element of schema.elements) { + const maybeEnum = getObjectExpressionProperty(element, 'enum'); + if (maybeEnum) { + isSecondaryLocationEnabled = + maybeEnum.value.type === 'ArrayExpression' && + maybeEnum.value.elements.length === 1 && + maybeEnum.value.elements[0].type === 'Identifier' && + maybeEnum.value.elements[0].name === 'SONAR_RUNTIME'; } } }, diff --git a/packages/jsts/tests/tools/testers/comment-based/checker.ts b/packages/jsts/tests/tools/testers/comment-based/checker.ts index 25e6efe6adb..68bfee967a6 100644 --- a/packages/jsts/tests/tools/testers/comment-based/checker.ts +++ b/packages/jsts/tests/tools/testers/comment-based/checker.ts @@ -39,7 +39,7 @@ const ruleTester = new RuleTester({ parser: __filename }); export function check(ruleId: string, ruleModule: Rule.RuleModule, ruleDir: string) { const fixtures = []; for (const file of fs.readdirSync(ruleDir)) { - if (file.match(/\.fixture\.(js|ts|jsx|tsx|vue)$/)) { + if (/\.fixture\.(js|ts|jsx|tsx|vue)$/.exec(file)) { const fixture = path.join(ruleDir, file); fixtures.push(fixture); } diff --git a/packages/jsts/tests/tools/testers/comment-based/helpers/issues.ts b/packages/jsts/tests/tools/testers/comment-based/helpers/issues.ts index 13eaa19f690..47ece602e26 100644 --- a/packages/jsts/tests/tools/testers/comment-based/helpers/issues.ts +++ b/packages/jsts/tests/tools/testers/comment-based/helpers/issues.ts @@ -84,7 +84,7 @@ export function isNonCompliantLine(comment: string) { } export function extractLineIssues(file: FileIssues, comment: Comment) { - const matcher = comment.value.match(NON_COMPLIANT_PATTERN); + const matcher = NON_COMPLIANT_PATTERN.exec(comment.value); if (matcher === null) { throw new Error(`Invalid comment format at line ${comment.line}: ${comment.value}`); } diff --git a/packages/jsts/tests/tools/testers/comment-based/helpers/locations.ts b/packages/jsts/tests/tools/testers/comment-based/helpers/locations.ts index 45c1d048378..c6b9ebb5270 100644 --- a/packages/jsts/tests/tools/testers/comment-based/helpers/locations.ts +++ b/packages/jsts/tests/tools/testers/comment-based/helpers/locations.ts @@ -51,10 +51,6 @@ export abstract class Location { export class PrimaryLocation extends Location { readonly secondaryLocations: SecondaryLocation[] = []; - - constructor(range: Range) { - super(range); - } } export class SecondaryLocation extends Location { @@ -79,7 +75,7 @@ export function extractLocations(file: FileIssues, comment: Comment) { let offset = 0; let matcher: RegExpMatchArray | null; LOCATION_PATTERN.lastIndex = 0; - while ((matcher = toBeMatched.match(LOCATION_PATTERN)) !== null) { + while ((matcher = LOCATION_PATTERN.exec(toBeMatched)) !== null) { locations.push( matcherToLocation(line, column, commentContent.indexOf(matcher[1], offset) + 1, matcher), ); diff --git a/packages/jsts/tests/tools/testers/comment-based/helpers/quickfixes.ts b/packages/jsts/tests/tools/testers/comment-based/helpers/quickfixes.ts index fac39b601d3..93f5df86abb 100644 --- a/packages/jsts/tests/tools/testers/comment-based/helpers/quickfixes.ts +++ b/packages/jsts/tests/tools/testers/comment-based/helpers/quickfixes.ts @@ -75,7 +75,7 @@ export function isQuickfixLine(comment: string) { export function extractQuickFixes(quickfixes: Map, comment: Comment) { if (QUICKFIX_DESCRIPTION_PATTERN.test(comment.value)) { - const matches = comment.value.match(QUICKFIX_DESCRIPTION_PATTERN); + const matches = QUICKFIX_DESCRIPTION_PATTERN.exec(comment.value); const { quickfixId, message } = matches.groups; const quickfix = quickfixes.get(quickfixId); if (!quickfix) { @@ -87,7 +87,7 @@ export function extractQuickFixes(quickfixes: Map, comment: Co } quickfix.description = message; } else if (QUICKFIX_CHANGE_PATTERN.test(comment.value)) { - const matches = comment.value.match(QUICKFIX_CHANGE_PATTERN); + const matches = QUICKFIX_CHANGE_PATTERN.exec(comment.value); const { quickfixId, type, diff --git a/packages/jsts/tests/tools/testers/typescript/tester.ts b/packages/jsts/tests/tools/testers/typescript/tester.ts index afb8214622f..ff2ee843874 100644 --- a/packages/jsts/tests/tools/testers/typescript/tester.ts +++ b/packages/jsts/tests/tools/testers/typescript/tester.ts @@ -45,7 +45,7 @@ class TypeScriptRuleTester extends RuleTester { super({ env, parser, - parserOptions: parserOptions, + parserOptions, }); } diff --git a/packages/shared/tests/helpers/debug.test.ts b/packages/shared/tests/helpers/debug.test.ts index 4329739fbce..5064d53f1e4 100644 --- a/packages/shared/tests/helpers/debug.test.ts +++ b/packages/shared/tests/helpers/debug.test.ts @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -import { debug } from '../../src/helpers'; +import { debug, info, warn } from '../../src/helpers'; describe('debug', () => { it('should log with a `DEBUG` prefix', () => { @@ -26,3 +26,19 @@ describe('debug', () => { expect(console.log).toHaveBeenCalledWith(`DEBUG hello, world!`); }); }); + +describe('warn', () => { + it('should log with a `WARN` prefix', () => { + console.log = jest.fn(); + warn('hello, world!'); + expect(console.log).toHaveBeenCalledWith(`WARN hello, world!`); + }); +}); + +describe('info', () => { + it('should log with no prefix', () => { + console.log = jest.fn(); + info('hello, world!'); + expect(console.log).toHaveBeenCalledWith(`hello, world!`); + }); +}); diff --git a/pom.xml b/pom.xml index 8a7081744b4..1367d0a56b5 100644 --- a/pom.xml +++ b/pom.xml @@ -86,6 +86,7 @@ coverage/lcov.info ${project.basedir}/packages/tsconfig.app.json,${project.basedir}/packages/tsconfig.test.json sonar-plugin/javascript-checks/src/main/resources/**/*.html + packages/bridge/src/worker.js