From 0e736a743894313d0b7e66b84a9d96c724f49b3a Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Wed, 20 Nov 2019 10:55:40 -0800 Subject: [PATCH] Fix the usage of pattern matching in module resolution for check of hasZeroOrOneAsteriskCharacter (#35209) * Fix the usage of pattern matching for check of hasZeroOrOneAsteriskCharacter Fixes #35171 * Fix error message --- src/compiler/diagnosticMessages.json | 2 +- src/compiler/program.ts | 2 +- src/compiler/utilities.ts | 1 + ...gBasedModuleResolution2_classic.errors.txt | 4 +-- ...pingBasedModuleResolution2_node.errors.txt | 4 +-- .../reference/pathsValidation4.errors.txt | 26 ++++++++++++++ tests/baselines/reference/pathsValidation4.js | 7 ++++ .../reference/pathsValidation4.trace.json | 36 +++++++++++++++++++ tests/cases/compiler/pathsValidation4.ts | 16 +++++++++ 9 files changed, 92 insertions(+), 6 deletions(-) create mode 100644 tests/baselines/reference/pathsValidation4.errors.txt create mode 100644 tests/baselines/reference/pathsValidation4.js create mode 100644 tests/baselines/reference/pathsValidation4.trace.json create mode 100644 tests/cases/compiler/pathsValidation4.ts diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 02e852869c..fedf796769 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -3241,7 +3241,7 @@ "category": "Error", "code": 5061 }, - "Substitution '{0}' in pattern '{1}' in can have at most one '*' character.": { + "Substitution '{0}' in pattern '{1}' can have at most one '*' character.": { "category": "Error", "code": 5062 }, diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 429b7389f5..cba0cd748b 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -2980,7 +2980,7 @@ namespace ts { const typeOfSubst = typeof subst; if (typeOfSubst === "string") { if (!hasZeroOrOneAsteriskCharacter(subst)) { - createDiagnosticForOptionPathKeyValue(key, i, Diagnostics.Substitution_0_in_pattern_1_in_can_have_at_most_one_Asterisk_character, subst, key); + createDiagnosticForOptionPathKeyValue(key, i, Diagnostics.Substitution_0_in_pattern_1_can_have_at_most_one_Asterisk_character, subst, key); } } else { diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 5e40405817..e7c5a7ae20 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -8285,6 +8285,7 @@ namespace ts { export function matchPatternOrExact(patternStrings: readonly string[], candidate: string): string | Pattern | undefined { const patterns: Pattern[] = []; for (const patternString of patternStrings) { + if (!hasZeroOrOneAsteriskCharacter(patternString)) continue; const pattern = tryParsePattern(patternString); if (pattern) { patterns.push(pattern); diff --git a/tests/baselines/reference/pathMappingBasedModuleResolution2_classic.errors.txt b/tests/baselines/reference/pathMappingBasedModuleResolution2_classic.errors.txt index 3b0261dd39..61282d7353 100644 --- a/tests/baselines/reference/pathMappingBasedModuleResolution2_classic.errors.txt +++ b/tests/baselines/reference/pathMappingBasedModuleResolution2_classic.errors.txt @@ -1,5 +1,5 @@ tests/cases/compiler/root/tsconfig.json(8,13): error TS5061: Pattern '*1*' can have at most one '*' character. -tests/cases/compiler/root/tsconfig.json(8,22): error TS5062: Substitution '*2*' in pattern '*1*' in can have at most one '*' character. +tests/cases/compiler/root/tsconfig.json(8,22): error TS5062: Substitution '*2*' in pattern '*1*' can have at most one '*' character. ==== tests/cases/compiler/root/tsconfig.json (2 errors) ==== @@ -14,7 +14,7 @@ tests/cases/compiler/root/tsconfig.json(8,22): error TS5062: Substitution '*2*' ~~~~~ !!! error TS5061: Pattern '*1*' can have at most one '*' character. ~~~~~ -!!! error TS5062: Substitution '*2*' in pattern '*1*' in can have at most one '*' character. +!!! error TS5062: Substitution '*2*' in pattern '*1*' can have at most one '*' character. } } } diff --git a/tests/baselines/reference/pathMappingBasedModuleResolution2_node.errors.txt b/tests/baselines/reference/pathMappingBasedModuleResolution2_node.errors.txt index 3b0261dd39..61282d7353 100644 --- a/tests/baselines/reference/pathMappingBasedModuleResolution2_node.errors.txt +++ b/tests/baselines/reference/pathMappingBasedModuleResolution2_node.errors.txt @@ -1,5 +1,5 @@ tests/cases/compiler/root/tsconfig.json(8,13): error TS5061: Pattern '*1*' can have at most one '*' character. -tests/cases/compiler/root/tsconfig.json(8,22): error TS5062: Substitution '*2*' in pattern '*1*' in can have at most one '*' character. +tests/cases/compiler/root/tsconfig.json(8,22): error TS5062: Substitution '*2*' in pattern '*1*' can have at most one '*' character. ==== tests/cases/compiler/root/tsconfig.json (2 errors) ==== @@ -14,7 +14,7 @@ tests/cases/compiler/root/tsconfig.json(8,22): error TS5062: Substitution '*2*' ~~~~~ !!! error TS5061: Pattern '*1*' can have at most one '*' character. ~~~~~ -!!! error TS5062: Substitution '*2*' in pattern '*1*' in can have at most one '*' character. +!!! error TS5062: Substitution '*2*' in pattern '*1*' can have at most one '*' character. } } } diff --git a/tests/baselines/reference/pathsValidation4.errors.txt b/tests/baselines/reference/pathsValidation4.errors.txt new file mode 100644 index 0000000000..8056cc7baa --- /dev/null +++ b/tests/baselines/reference/pathsValidation4.errors.txt @@ -0,0 +1,26 @@ +tests/cases/compiler/tsconfig.json(6,11): error TS5061: Pattern '@interface/**/*' can have at most one '*' character. +tests/cases/compiler/tsconfig.json(7,11): error TS5061: Pattern '@service/**/*' can have at most one '*' character. +tests/cases/compiler/tsconfig.json(7,29): error TS5062: Substitution './src/service/**/*' in pattern '@service/**/*' can have at most one '*' character. + + +==== tests/cases/compiler/tsconfig.json (3 errors) ==== + { + "compilerOptions": { + "traceResolution": true, + "baseUrl": "./src", + "paths": { + "@interface/**/*" : ["./src/interface/*"], + ~~~~~~~~~~~~~~~~~ +!!! error TS5061: Pattern '@interface/**/*' can have at most one '*' character. + "@service/**/*": ["./src/service/**/*"], + ~~~~~~~~~~~~~~~ +!!! error TS5061: Pattern '@service/**/*' can have at most one '*' character. + ~~~~~~~~~~~~~~~~~~~~ +!!! error TS5062: Substitution './src/service/**/*' in pattern '@service/**/*' can have at most one '*' character. + "@controller/*": ["controller/*"], + } + } + } + +==== tests/cases/compiler/src/main.ts (0 errors) ==== + import 'someModule'; \ No newline at end of file diff --git a/tests/baselines/reference/pathsValidation4.js b/tests/baselines/reference/pathsValidation4.js new file mode 100644 index 0000000000..eef527fbf7 --- /dev/null +++ b/tests/baselines/reference/pathsValidation4.js @@ -0,0 +1,7 @@ +//// [main.ts] +import 'someModule'; + +//// [main.js] +"use strict"; +exports.__esModule = true; +require("someModule"); diff --git a/tests/baselines/reference/pathsValidation4.trace.json b/tests/baselines/reference/pathsValidation4.trace.json new file mode 100644 index 0000000000..cd4f358050 --- /dev/null +++ b/tests/baselines/reference/pathsValidation4.trace.json @@ -0,0 +1,36 @@ +[ + "======== Resolving module 'someModule' from 'tests/cases/compiler/src/main.ts'. ========", + "Module resolution kind is not specified, using 'NodeJs'.", + "'baseUrl' option is set to 'tests/cases/compiler/src', using this value to resolve non-relative module name 'someModule'.", + "'paths' option is specified, looking for a pattern to match module name 'someModule'.", + "'baseUrl' option is set to 'tests/cases/compiler/src', using this value to resolve non-relative module name 'someModule'.", + "Resolving module name 'someModule' relative to base url 'tests/cases/compiler/src' - 'tests/cases/compiler/src/someModule'.", + "Loading module as file / folder, candidate module location 'tests/cases/compiler/src/someModule', target file type 'TypeScript'.", + "File 'tests/cases/compiler/src/someModule.ts' does not exist.", + "File 'tests/cases/compiler/src/someModule.tsx' does not exist.", + "File 'tests/cases/compiler/src/someModule.d.ts' does not exist.", + "Directory 'tests/cases/compiler/src/someModule' does not exist, skipping all lookups in it.", + "Loading module 'someModule' from 'node_modules' folder, target file type 'TypeScript'.", + "Directory 'tests/cases/compiler/src/node_modules' does not exist, skipping all lookups in it.", + "Directory 'tests/cases/compiler/node_modules' does not exist, skipping all lookups in it.", + "Directory 'tests/cases/node_modules' does not exist, skipping all lookups in it.", + "Directory 'tests/node_modules' does not exist, skipping all lookups in it.", + "Directory 'node_modules' does not exist, skipping all lookups in it.", + "Directory '/node_modules' does not exist, skipping all lookups in it.", + "'baseUrl' option is set to 'tests/cases/compiler/src', using this value to resolve non-relative module name 'someModule'.", + "'paths' option is specified, looking for a pattern to match module name 'someModule'.", + "'baseUrl' option is set to 'tests/cases/compiler/src', using this value to resolve non-relative module name 'someModule'.", + "Resolving module name 'someModule' relative to base url 'tests/cases/compiler/src' - 'tests/cases/compiler/src/someModule'.", + "Loading module as file / folder, candidate module location 'tests/cases/compiler/src/someModule', target file type 'JavaScript'.", + "File 'tests/cases/compiler/src/someModule.js' does not exist.", + "File 'tests/cases/compiler/src/someModule.jsx' does not exist.", + "Directory 'tests/cases/compiler/src/someModule' does not exist, skipping all lookups in it.", + "Loading module 'someModule' from 'node_modules' folder, target file type 'JavaScript'.", + "Directory 'tests/cases/compiler/src/node_modules' does not exist, skipping all lookups in it.", + "Directory 'tests/cases/compiler/node_modules' does not exist, skipping all lookups in it.", + "Directory 'tests/cases/node_modules' does not exist, skipping all lookups in it.", + "Directory 'tests/node_modules' does not exist, skipping all lookups in it.", + "Directory 'node_modules' does not exist, skipping all lookups in it.", + "Directory '/node_modules' does not exist, skipping all lookups in it.", + "======== Module name 'someModule' was not resolved. ========" +] \ No newline at end of file diff --git a/tests/cases/compiler/pathsValidation4.ts b/tests/cases/compiler/pathsValidation4.ts new file mode 100644 index 0000000000..fc9568c09e --- /dev/null +++ b/tests/cases/compiler/pathsValidation4.ts @@ -0,0 +1,16 @@ +// @noTypesAndSymbols: true +// @filename: tsconfig.json +{ + "compilerOptions": { + "traceResolution": true, + "baseUrl": "./src", + "paths": { + "@interface/**/*" : ["./src/interface/*"], + "@service/**/*": ["./src/service/**/*"], + "@controller/*": ["controller/*"], + } + } +} + +// @filename: src/main.ts +import 'someModule'; \ No newline at end of file