diff --git a/src/linter/linter.ts b/src/linter/linter.ts index 5947518c..3d569ba6 100644 --- a/src/linter/linter.ts +++ b/src/linter/linter.ts @@ -169,7 +169,7 @@ async function lint( }); const res = await lintWorkspace(workspace, filePathsWorkspace, options, config, matchedPatterns); - checkUnmatchedPatterns(filePatterns, matchedPatterns); + await checkUnmatchedPatterns(filePatterns, matchedPatterns, options); lintEnd(); return res; @@ -360,7 +360,7 @@ export function resolveReader({ * @param patterns Available patterns * @throws Error if an unmatched pattern is found */ -function checkUnmatchedPatterns(patterns: FilePattern[], patternsMatch: Set) { +async function checkUnmatchedPatterns(patterns: FilePattern[], patternsMatch: Set, options: LinterOptions) { const unmatchedPatterns = patterns.reduce((acc, pattern) => { if (pattern.endsWith("/")) { // Match all files in a directory pattern += "**/*"; @@ -372,6 +372,41 @@ function checkUnmatchedPatterns(patterns: FilePattern[], patternsMatch: Set(); + const rootFileReader = createReader({ + fsBasePath: options.rootDir, + virBasePath: "/", + excludes: [ + "/node_modules/**/*", + "/.*", + "/.*/**/*", + ], + }); + + const allFiles = await rootFileReader.byGlob("/**/*"); + const filePaths = allFiles.map((file) => file.getPath().substring(1)); + const patterns = buildPatterns(unmatchedPatterns); + + for (const pattern of patterns) { + const matchedFilePaths = filePaths.filter((filePath) => pattern.match(filePath)); + if (matchedFilePaths.length) { + matchedFilePaths.forEach((filePath) => notProcessedFiles.add(filePath)); + unmatchedPatterns.splice(unmatchedPatterns.indexOf(pattern.pattern), 1); + } + } + + if (notProcessedFiles.size) { + // TODO: For files we need to just print a warning, but the reporter is not available here + // eslint-disable-next-line no-console + console.log( + `Specified ${notProcessedFiles.size === 1 ? "file" : "files"} ` + + `'${[...notProcessedFiles].join("', '")}' ${notProcessedFiles.size === 1 ? "is" : "are"}` + + ` not supporeted by the linter`); + } + } + if (unmatchedPatterns.length) { throw new Error(`Specified file ${unmatchedPatterns.length === 1 ? "pattern" : "patterns"}` + ` '${unmatchedPatterns.join("', '")}' did not match any resource`); diff --git a/test/lib/linter/linter.ts b/test/lib/linter/linter.ts index 5deea096..9e5852d6 100644 --- a/test/lib/linter/linter.ts +++ b/test/lib/linter/linter.ts @@ -123,6 +123,20 @@ test.serial("lint: com.ui5.troublesome.app with unmatched patterns", async (t) = }); }); +test.serial("lint: com.ui5.troublesome.app with unmatched files", async (t) => { + const projectPath = path.join(fixturesProjectsPath, "com.ui5.troublesome.app"); + + const {lintProject} = t.context; + + const res = await lintProject({ + filePatterns: ["webapp/i18n/*"], + rootDir: projectPath, + }); + + // TODO: when logging is implemented, check for log messages. + t.snapshot(preprocessLintResultsForSnapshot(res)); +}); + test.serial("lint: com.ui5.troublesome.app with files property in ui5lint.config", async (t) => { const projectPath = path.join(fixturesProjectsPath, "com.ui5.troublesome.app"); diff --git a/test/lib/linter/snapshots/linter.ts.md b/test/lib/linter/snapshots/linter.ts.md index 3b5bb89f..1befc789 100644 --- a/test/lib/linter/snapshots/linter.ts.md +++ b/test/lib/linter/snapshots/linter.ts.md @@ -1388,6 +1388,12 @@ Generated by [AVA](https://avajs.dev). }, ] +## lint: com.ui5.troublesome.app with unmatched files + +> Snapshot 1 + + [] + ## lint: com.ui5.troublesome.app with files property in ui5lint.config > Snapshot 1 diff --git a/test/lib/linter/snapshots/linter.ts.snap b/test/lib/linter/snapshots/linter.ts.snap index e66c3350..f3cbe8bf 100644 Binary files a/test/lib/linter/snapshots/linter.ts.snap and b/test/lib/linter/snapshots/linter.ts.snap differ