eslint-plugin-sonarjs
Version:
64 lines (63 loc) • 2.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DEFAULT_TEST_FILE_EXTENSIONS = void 0;
exports.isTestFile = isTestFile;
exports.isTestRelatedFile = isTestRelatedFile;
/*
* SonarQube JavaScript Plugin
* Copyright (C) SonarSource Sàrl
* mailto:info AT sonarsource DOT com
*
* You can redistribute and/or modify this program under the terms of
* the Sonar Source-Available License Version 1, as published by SonarSource Sàrl.
*
* 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 Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
exports.DEFAULT_TEST_FILE_EXTENSIONS = [
'.js',
'.mjs',
'.cjs',
'.jsx',
'.vue',
'.ts',
'.mts',
'.cts',
'.tsx',
];
function suffixAlternation(extensions) {
const effective = extensions?.length ? extensions : exports.DEFAULT_TEST_FILE_EXTENSIONS;
return effective.map(ext => (ext.startsWith('.') ? ext.slice(1) : ext)).join('|');
}
function testFilePattern(extensions) {
return new RegExp(String.raw `\.(?:test|spec|cy)\.(?:${suffixAlternation(extensions)})$`);
}
function testRelatedFilePattern(extensions) {
const alternation = suffixAlternation(extensions);
return new RegExp(String.raw `\.(?:test|spec|cy)\.(?:${alternation})$|\.(?:e2e|mock)\.(?:${alternation})$|(?:^|[\\/])(?:__tests__|__mocks__)[\\/]`);
}
/**
* Checks whether a file path matches a test file pattern.
*
* @param filePath the file path to test.
* @param extensions the allowed test file extensions.
* @returns true when the path looks like a test file.
*/
function isTestFile(filePath, extensions) {
return testFilePattern(extensions).test(filePath);
}
/**
* Checks whether a file path looks test-related.
*
* @param filePath the file path to test.
* @param extensions the allowed test file extensions.
* @returns true when the path looks test-related.
*/
function isTestRelatedFile(filePath, extensions) {
return testRelatedFilePattern(extensions).test(filePath);
}