@fimbul/wotan
Version:
Pluggable TypeScript and JavaScript linter
60 lines • 2.74 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DefaultFileFilterFactory = void 0;
const tslib_1 = require("tslib");
const inversify_1 = require("inversify");
const ts = require("typescript");
const utils_1 = require("../../utils");
const path = require("path");
let DefaultFileFilterFactory = class DefaultFileFilterFactory {
create(context) {
return new DefaultFileFilter(context.program, context.host);
}
};
DefaultFileFilterFactory = tslib_1.__decorate([
inversify_1.injectable()
], DefaultFileFilterFactory);
exports.DefaultFileFilterFactory = DefaultFileFilterFactory;
class DefaultFileFilter {
constructor(program, host) {
this.program = program;
this.host = host;
this.rootNames = this.program.getRootFileNames();
this.options = this.program.getCompilerOptions();
this.libDirectory = utils_1.unixifyPath(path.dirname(ts.getDefaultLibFilePath(this.options))) + '/';
this.typeRoots = undefined;
this.outputsOfReferencedProjects = undefined;
}
filter(file) {
const { fileName } = file;
if (fileName.endsWith('.json'))
return false;
if (this.options.composite)
return this.rootNames.includes(fileName);
if (this.program.isSourceFileFromExternalLibrary(file))
return false;
if (!fileName.endsWith('.d.ts'))
return true;
if (
// lib.xxx.d.ts
fileName.startsWith(this.libDirectory) ||
// tslib implicitly gets added while linting a project where a dependency in node_modules contains typescript files
fileName.endsWith('/node_modules/tslib/tslib.d.ts'))
return false; // lib.xxx.d.ts
return !this.isInTypeRoot(fileName) && !this.isOutputOfReferencedProject(fileName);
}
isInTypeRoot(fileName) {
if (this.typeRoots === undefined)
this.typeRoots = ts.getEffectiveTypeRoots(this.options, {
directoryExists: (dir) => this.host.directoryExists(dir),
getCurrentDirectory: () => this.program.getCurrentDirectory(),
}) || utils_1.emptyArray;
return !this.typeRoots.every((typeRoot) => path.relative(typeRoot, fileName).startsWith('..' + path.sep));
}
isOutputOfReferencedProject(fileName) {
var _a;
(_a = this.outputsOfReferencedProjects) !== null && _a !== void 0 ? _a : (this.outputsOfReferencedProjects = utils_1.flatMap(utils_1.iterateProjectReferences(this.program.getResolvedProjectReferences()), utils_1.getOutputFileNamesOfProjectReference));
return this.outputsOfReferencedProjects.includes(fileName);
}
}
//# sourceMappingURL=file-filter.js.map