Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(globMatch): support implicit globs #200

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/neat-carpets-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'tsconfck': patch
---

fix(glob-matching): add implicit **/\* to path patterns that do not have an extension or wildcard in their last segment, eg `src` becomes `src/**/\*` for matching.
39 changes: 34 additions & 5 deletions packages/tsconfck/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,35 @@ export function isGlobMatch(filename, dir, patterns, allowJs) {
// filename must end with part of pattern that comes after last wildcard
let lastWildcardIndex = pattern.length;
let hasWildcard = false;
let hasExtension = false;
let hasSlash = false;
let lastSlashIndex = -1;
for (let i = pattern.length - 1; i > -1; i--) {
if (pattern[i] === '*' || pattern[i] === '?') {
lastWildcardIndex = i;
hasWildcard = true;
const c = pattern[i];
if (!hasWildcard) {
if (c === '*' || c === '?') {
lastWildcardIndex = i;
hasWildcard = true;
}
}
if (!hasSlash) {
if (c === '.') {
hasExtension = true;
} else if (c === '/') {
lastSlashIndex = i;
hasSlash = true;
}
}
if (hasWildcard && hasSlash) {
break;
}
}
if (!hasExtension && (!hasWildcard || lastWildcardIndex < lastSlashIndex)) {
// add implicit glob
pattern += `${pattern.endsWith('/') ? '' : '/'}${GLOB_ALL_PATTERN}`;
lastWildcardIndex = pattern.length - 1;
hasWildcard = true;
}

// if pattern does not end with wildcard, filename must end with pattern after last wildcard
if (
Expand Down Expand Up @@ -243,11 +265,18 @@ export function isGlobMatch(filename, dir, patterns, allowJs) {
return false;
}

// if no wildcard in pattern, filename must be equal to resolved pattern
if (!hasWildcard) {
// no wildcard in pattern, filename must be equal to resolved pattern
return filename === resolvedPattern;
} else if (
firstWildcardIndex + GLOB_ALL_PATTERN.length ===
resolvedPattern.length - (pattern.length - 1 - lastWildcardIndex) &&
resolvedPattern.slice(firstWildcardIndex, firstWildcardIndex + GLOB_ALL_PATTERN.length) ===
GLOB_ALL_PATTERN
) {
// singular glob-all pattern and we already validated prefix and suffix matches
return true;
}

// complex pattern, use regex to check it
if (PATTERN_REGEX_CACHE.has(resolvedPattern)) {
return PATTERN_REGEX_CACHE.get(resolvedPattern).test(filename);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function foo() {
return 'foo';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { foo } from '../src/foo';
import * as assert from 'assert';

function test() {
const actual = foo();
const expected = 'foo';
assert.strictEqual(actual, expected);
}
test();
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"files": [],
"references": [
{"path": "./tsconfig.src.json"},
{"path": "./tsconfig.test.json"}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"include": ["src"],
"compilerOptions": {
"composite": true,
"strict": true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"include": ["tests"],
"compilerOptions": {
"composite": true,
"strict": false
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"files": [],
"references": [
{
"path": "./tsconfig.src.json"
},
{
"path": "./tsconfig.test.json"
}
],
"include": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"include": [
"src"
],
"compilerOptions": {
"composite": true,
"strict": true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"include": [
"src"
],
"compilerOptions": {
"composite": true,
"strict": true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"files": [],
"references": [
{
"path": "./tsconfig.src.json"
},
{
"path": "./tsconfig.test.json"
}
],
"include": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"include": [
"tests"
],
"compilerOptions": {
"composite": true,
"strict": false
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"include": [
"tests"
],
"compilerOptions": {
"composite": true,
"strict": false
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"files": [],
"references": [
{
"path": "./tsconfig.src.json"
},
{
"path": "./tsconfig.test.json"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"files": [],
"references": [
{
"path": "./tsconfig.src.json"
},
{
"path": "./tsconfig.test.json"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"include": [
"src"
],
"compilerOptions": {
"composite": true,
"strict": true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"include": [
"tests"
],
"compilerOptions": {
"composite": true,
"strict": false
}
}