For each target, allow importing itself

This commit is contained in:
Alex Dima
2022-01-17 22:42:26 +01:00
parent 066671a902
commit aa86df2561
3 changed files with 70 additions and 35 deletions

View File

@@ -25,28 +25,52 @@ module.exports = new class {
url: 'https://github.com/microsoft/vscode/wiki/Source-Code-Organization'
}
};
this._optionsCache = new WeakMap();
}
create(context) {
const configs = this._processOptions(context.options);
const options = context.options;
const { configs, warnings } = this._processOptions(options);
const relativeFilename = getRelativeFilename(context);
if (warnings.length > 0) {
// configuration warnings
context.report({
loc: { line: 1, column: 0 },
message: warnings.join('\n')
});
return {};
}
for (const config of configs) {
if (minimatch(relativeFilename, config.target)) {
return (0, utils_1.createImportRuleListener)((node, value) => this._checkImport(context, config, node, value));
}
}
context.report({
loc: { line: 1, column: 1 },
loc: { line: 1, column: 0 },
messageId: 'badFilename'
});
return {};
}
_processOptions(options) {
const result = [];
if (this._optionsCache.has(options)) {
return this._optionsCache.get(options);
}
const configs = [];
const warnings = [];
for (const option of options) {
const target = option.target;
const restrictions = (typeof option.restrictions === 'string' ? [option.restrictions] : option.restrictions);
result.push({ target, restrictions });
const restrictions = (typeof option.restrictions === 'string' ? [option.restrictions] : option.restrictions.slice(0));
if (/^src\/vs\/.*\/\*\*/.test(target)) {
const amdTarget = target.substring('src/'.length);
// Allow importing itself
if (restrictions.includes(amdTarget)) {
warnings.push(`target ${target}: '${amdTarget}' is automatically included in restrictions.`);
}
restrictions.push(amdTarget);
}
configs.push({ target, restrictions });
}
const result = { configs, warnings };
this._optionsCache.set(options, result);
return result;
}
_checkImport(context, config, node, importPath) {