Skip to content

Commit

Permalink
Improve S4156 (prefer-namespace-keyword): Report on the "module" ke…
Browse files Browse the repository at this point in the history
…yword (#4329)
  • Loading branch information
yassin-kammoun-sonarsource authored Oct 30, 2023
1 parent ffa4380 commit d2b17c1
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/jsts/src/rules/S4156/cb.fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module MyModule { // Noncompliant [[qf!]]
// ^^^^^^
// edit@qf [[sc=4;ec=10]] {{namespace}}
const x = 42;
}
28 changes: 28 additions & 0 deletions packages/jsts/src/rules/S4156/cb.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* SonarQube JavaScript Plugin
* Copyright (C) 2011-2023 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 { check } from '../tools';
import { rule } from './';
import path from 'path';

const sonarId = path.basename(__dirname);

describe(`Rule ${sonarId}`, () => {
check(sonarId, rule, __dirname);
});
34 changes: 34 additions & 0 deletions packages/jsts/src/rules/S4156/decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* SonarQube JavaScript Plugin
* Copyright (C) 2011-2023 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 * as estree from 'estree';
import { interceptReport } from '../helpers';

export function decorate(rule: Rule.RuleModule): Rule.RuleModule {
return interceptReport(rule, (context: Rule.RuleContext, descriptor: Rule.ReportDescriptor) => {
const { node } = descriptor as { node: estree.Node };
const moduleKeyword = context.sourceCode.getFirstToken(node, token => token.value === 'module');
if (moduleKeyword?.loc) {
context.report({ ...descriptor, loc: moduleKeyword.loc });
} else {
context.report(descriptor);
}
});
}
25 changes: 25 additions & 0 deletions packages/jsts/src/rules/S4156/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* SonarQube JavaScript Plugin
* Copyright (C) 2011-2023 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.
*/
// https://sonarsource.github.io/rspec/#/rspec/S4156/javascript

import { tsEslintRules as rules } from '../typescript-eslint';
import { decorate } from './decorator';

export const rule = decorate(rules['prefer-namespace-keyword']);

0 comments on commit d2b17c1

Please sign in to comment.