1
0
Fork 0
management/front/dkha-web-sz-main/node_modules/eslint-module-utils/ignore.js

62 lines
1.7 KiB
JavaScript
Raw Normal View History

2023-12-28 23:41:32 +08:00
'use strict';
2023-12-18 13:12:25 +08:00
2023-12-28 23:41:32 +08:00
exports.__esModule = true;
2023-12-18 13:12:25 +08:00
2023-12-28 23:41:32 +08:00
const extname = require('path').extname;
const log = require('debug')('eslint-plugin-import:utils:ignore');
2023-12-18 13:12:25 +08:00
// one-shot memoized
2023-12-28 23:41:32 +08:00
let cachedSet; let lastSettings;
2023-12-18 13:12:25 +08:00
function validExtensions(context) {
if (cachedSet && context.settings === lastSettings) {
2023-12-28 23:41:32 +08:00
return cachedSet;
2023-12-18 13:12:25 +08:00
}
2023-12-28 23:41:32 +08:00
lastSettings = context.settings;
cachedSet = makeValidExtensionSet(context.settings);
return cachedSet;
2023-12-18 13:12:25 +08:00
}
function makeValidExtensionSet(settings) {
// start with explicit JS-parsed extensions
2023-12-28 23:41:32 +08:00
const exts = new Set(settings['import/extensions'] || [ '.js' ]);
2023-12-18 13:12:25 +08:00
// all alternate parser extensions are also valid
if ('import/parsers' in settings) {
2023-12-28 23:41:32 +08:00
for (const parser in settings['import/parsers']) {
const parserSettings = settings['import/parsers'][parser];
2023-12-18 13:12:25 +08:00
if (!Array.isArray(parserSettings)) {
2023-12-28 23:41:32 +08:00
throw new TypeError('"settings" for ' + parser + ' must be an array');
2023-12-18 13:12:25 +08:00
}
2023-12-28 23:41:32 +08:00
parserSettings.forEach((ext) => exts.add(ext));
2023-12-18 13:12:25 +08:00
}
}
2023-12-28 23:41:32 +08:00
return exts;
2023-12-18 13:12:25 +08:00
}
2023-12-28 23:41:32 +08:00
exports.getFileExtensions = makeValidExtensionSet;
2023-12-18 13:12:25 +08:00
exports.default = function ignore(path, context) {
// check extension whitelist first (cheap)
2023-12-28 23:41:32 +08:00
if (!hasValidExtension(path, context)) { return true; }
2023-12-18 13:12:25 +08:00
2023-12-28 23:41:32 +08:00
if (!('import/ignore' in context.settings)) { return false; }
const ignoreStrings = context.settings['import/ignore'];
2023-12-18 13:12:25 +08:00
for (let i = 0; i < ignoreStrings.length; i++) {
2023-12-28 23:41:32 +08:00
const regex = new RegExp(ignoreStrings[i]);
2023-12-18 13:12:25 +08:00
if (regex.test(path)) {
2023-12-28 23:41:32 +08:00
log(`ignoring ${path}, matched pattern /${ignoreStrings[i]}/`);
return true;
2023-12-18 13:12:25 +08:00
}
}
2023-12-28 23:41:32 +08:00
return false;
};
2023-12-18 13:12:25 +08:00
function hasValidExtension(path, context) {
2023-12-28 23:41:32 +08:00
return validExtensions(context).has(extname(path));
2023-12-18 13:12:25 +08:00
}
2023-12-28 23:41:32 +08:00
exports.hasValidExtension = hasValidExtension;