Skip to content

Commit ae17c35

Browse files
committed
Added error checking for references that reference the same file along
with 3 tests,
1 parent db0a223 commit ae17c35

9 files changed

+65
-5
lines changed

src/compiler/diagnosticInformationMap.generated.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ module ts {
340340
Parameter_0_of_exported_function_has_or_is_using_name_1_from_private_module_2: { code: 4077, category: DiagnosticCategory.Error, key: "Parameter '{0}' of exported function has or is using name '{1}' from private module '{2}'." },
341341
Parameter_0_of_exported_function_has_or_is_using_private_name_1: { code: 4078, category: DiagnosticCategory.Error, key: "Parameter '{0}' of exported function has or is using private name '{1}'." },
342342
The_current_host_does_not_support_the_0_option: { code: 5001, category: DiagnosticCategory.Error, key: "The current host does not support the '{0}' option." },
343+
A_file_cannot_have_a_reference_to_itself: { code: 5006, category: DiagnosticCategory.Error, key: "A file cannot have a reference to itself." },
343344
Cannot_find_the_common_subdirectory_path_for_the_input_files: { code: 5009, category: DiagnosticCategory.Error, key: "Cannot find the common subdirectory path for the input files." },
344345
Cannot_read_file_0_Colon_1: { code: 5012, category: DiagnosticCategory.Error, key: "Cannot read file '{0}': {1}" },
345346
Unsupported_file_encoding: { code: 5013, category: DiagnosticCategory.Error, key: "Unsupported file encoding." },

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,6 +1355,10 @@
13551355
"category": "Error",
13561356
"code": 5001
13571357
},
1358+
"A file cannot have a reference to itself.": {
1359+
"category": "Error",
1360+
"code": 5006
1361+
},
13581362
"Cannot find the common subdirectory path for the input files.": {
13591363
"category": "Error",
13601364
"code": 5009

src/compiler/parser.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3828,11 +3828,18 @@ module ts {
38283828
errorAtPos(start, length, Diagnostics.Invalid_reference_directive_syntax);
38293829
}
38303830
else {
3831-
referencedFiles.push({
3832-
pos: range.pos,
3833-
end: range.end,
3834-
filename: matchResult[3]
3835-
});
3831+
var basePath = getDirectoryPath(file.filename);
3832+
var referenceFilename = normalizePath(combinePaths(basePath, matchResult[3]));
3833+
if (file.filename === referenceFilename) {
3834+
errorAtPos(range.pos, range.end - range.pos, Diagnostics.A_file_cannot_have_a_reference_to_itself);
3835+
}
3836+
else {
3837+
referencedFiles.push({
3838+
pos: range.pos,
3839+
end: range.end,
3840+
filename: matchResult[3]
3841+
});
3842+
}
38363843
}
38373844
}
38383845
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
tests/cases/compiler/selfReferencingFile.ts(1,1): error TS5006: A file cannot have a reference to itself.
2+
3+
4+
==== tests/cases/compiler/selfReferencingFile.ts (1 errors) ====
5+
///<reference path='selfReferencingFile.ts'/>
6+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7+
!!! error TS5006: A file cannot have a reference to itself.
8+
9+
class selfReferencingFile {
10+
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
tests/cases/compiler/selfReferencingFile2.ts(1,1): error TS6053: File 'tests/cases/selfReferencingFile2.ts' not found.
2+
3+
4+
==== tests/cases/compiler/selfReferencingFile2.ts (1 errors) ====
5+
///<reference path='../selfReferencingFile2.ts'/>
6+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7+
!!! error TS6053: File 'selfReferencingFile2.ts' not found.
8+
9+
class selfReferencingFile2 {
10+
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
tests/cases/compiler/selfReferencingFile3.ts(1,1): error TS5006: A file cannot have a reference to itself.
2+
3+
4+
==== tests/cases/compiler/selfReferencingFile3.ts (1 errors) ====
5+
///<reference path='./selfReferencingFile3.ts'/>
6+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7+
!!! error TS5006: A file cannot have a reference to itself.
8+
9+
class selfReferencingFile3 {
10+
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
///<reference path='selfReferencingFile.ts'/>
2+
3+
class selfReferencingFile {
4+
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
///<reference path='../selfReferencingFile2.ts'/>
2+
3+
class selfReferencingFile2 {
4+
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
///<reference path='./selfReferencingFile3.ts'/>
2+
3+
class selfReferencingFile3 {
4+
5+
}

0 commit comments

Comments
 (0)