Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,13 @@
"homepage": "https://github.com/TypeStrong/ts-loader",
"dependencies": {
"chalk": "^4.1.0",
"enhanced-resolve": "^5.0.0",
"micromatch": "^4.0.0",
"semver": "^7.3.4",
"picomatch": "^4.0.0",
"source-map": "^0.7.4"
},
"devDependencies": {
"@eslint/js": "^10.0.1",
"@types/micromatch": "^4.0.0",
"@types/node": "*",
"@types/semver": "^7.3.4",
"@types/picomatch": "^4.0.0",
"babel": "^6.0.0",
"babel-core": "^6.0.0",
"babel-loader": "^7.0.0",
Expand Down
20 changes: 18 additions & 2 deletions src/compilerSetup.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
import * as semver from 'semver';
import type typescript from 'typescript';

import type { LoaderOptions } from './interfaces';
import type * as logger from './logger';

/**
* Compare two semver-formatted version strings.
* Returns true if v1 >= v2.
*/
function versionGte(v1: string, v2: string): boolean {
const parts1 = v1.split('.').map(Number);
const parts2 = v2.split('.').map(Number);
const len = Math.max(parts1.length, parts2.length);
for (let i = 0; i < len; i++) {
const a = parts1[i] ?? 0;
const b = parts2[i] ?? 0;
if (a > b) return true;
if (a < b) return false;
}
return true;
}

export function getCompiler(loaderOptions: LoaderOptions, log: logger.Logger) {
let compiler: typeof typescript | undefined;
let errorMessage: string | undefined;
Expand All @@ -27,7 +43,7 @@ export function getCompiler(loaderOptions: LoaderOptions, log: logger.Logger) {
if (loaderOptions.compiler === 'typescript') {
if (
compiler!.version !== undefined &&
semver.gte(compiler!.version, '3.6.3')
versionGte(compiler!.version, '3.6.3')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot please remove versionGte(compiler!.version, '3.6.3') test from the if statement

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Removed versionGte(compiler!.version, '3.6.3') from the condition — the if now simply checks compiler!.version !== undefined. The versionGte helper function was also removed since it had no remaining usages.

) {
// don't log yet in this case, if a tsconfig.json exists we want to combine the message
compilerCompatible = true;
Expand Down
2 changes: 0 additions & 2 deletions src/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type * as webpack from 'webpack';

import { create as _create } from 'enhanced-resolve';

export function makeResolver(
_options: webpack.WebpackOptionsNormalized
): ResolveSync {
Expand Down
19 changes: 14 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Chalk } from 'chalk';
import * as fs from 'fs';
import micromatch from 'micromatch';
import picomatch from 'picomatch';
import * as path from 'path';
import * as webpack from 'webpack';
import type typescript from 'typescript';
Expand Down Expand Up @@ -65,11 +65,20 @@ export function formatErrors(
context,
diagnostic.file.fileName
);
const matchResult = micromatch(
[relativeFileName],
loaderOptions.reportFiles
// Split positive and negative patterns to replicate micromatch semantics:
// a file must match a positive pattern AND not match any negative pattern.
const positivePatterns = loaderOptions.reportFiles.filter(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot please replace positivePatterns / negativePatterns creation with one reduce statement instead of 2 filters

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — replaced the two filter calls with a single reduce that partitions patterns into positivePatterns and negativePatterns in one pass.

p => !p.startsWith('!')
);
if (matchResult.length === 0) {
const negativePatterns = loaderOptions.reportFiles
.filter(p => p.startsWith('!'))
.map(p => p.slice(1));
const matchPos = picomatch(
positivePatterns.length > 0 ? positivePatterns : ['**']
);
const matchNeg =
negativePatterns.length > 0 ? picomatch(negativePatterns) : null;
if (!matchPos(relativeFileName) || (matchNeg && matchNeg(relativeFileName))) {
return false;
}
}
Expand Down
Loading