Skip to content

Commit

Permalink
add file filter based on the tsconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
tsne committed Oct 23, 2018
1 parent 7f539a4 commit d4a89a8
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
68 changes: 68 additions & 0 deletions src/filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import {resolve} from "path";



export function fileFilter(tsconfig) {
const files = fileMatcher(tsconfig.files || []);
const glob = globMatcher(tsconfig.exclude || [], tsconfig.include || []);

return (filename) => {
filename = resolve(filename);
return files(filename) || glob(filename);
};
}


function fileMatcher(files) {
files = files.map(resolve);
return (filename) => files.includes(filename);
}

function globMatcher(excludePatterns, includePatterns) {
const exclude = excludePatterns.map(buildRegexp);
const include = includePatterns.map(buildRegexp);

return (filename) => {
for(let i = 0; i < exclude.length; ++i) {
if(exclude[i].test(filename)) {
return false;
}
}

for(let i = 0; i < include.length; ++i) {
if(include[i].test(filename)) {
return true;
}
}

return false;
};
}

function buildRegexp(pattern) {
const parts = pattern.replace("\\", "/").split("/").map(part => {
if(part === "**") {
return "([^/]*(/|$))*";
}

let rx = "";
for(let i = 0; i < part.length; ++i) {
switch(part[i]) {
case "*":
rx += "[^/]*";
break;

case "?":
rx += "[^/]";
break;

default:
rx += part[i];
break;
}
}
return rx;
});

return new RegExp(resolve(parts.join("/")));
}
5 changes: 4 additions & 1 deletion src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
DiagnosticCategory,
} from "typescript";
import {createServiceHost} from "./servicehost";
import {fileFilter} from "./filter";



Expand Down Expand Up @@ -41,6 +42,8 @@ export function createService(tsconfig) {
const reg = createDocumentRegistry();
const svc = createLanguageService(host, reg);

const filter = fileFilter(tsconfig);

return {
emit(filename, code) {
host.addFile(filename, code);
Expand Down Expand Up @@ -71,7 +74,7 @@ export function createService(tsconfig) {
},

filter(filename) {
return host.containsFile(filename);
return filter(filename) || host.containsFile(filename);
},

resolveModuleName(importee, importer) {
Expand Down

0 comments on commit d4a89a8

Please sign in to comment.