Skip to content

Commit fd7da14

Browse files
committed
Add limitation to the node analysis not only in tests, as originally, but in the actual product code.
1 parent 8ec3623 commit fd7da14

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

Nodejs/Product/Analysis/Analysis/AnalysisLimits.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public AnalysisLimits() {
3434
MaxObjectLiteralProperties = 50;
3535
MaxObjectKeysTypes = 5;
3636
MaxMergeTypes = 5;
37-
NestedModulesLimit = 4;
37+
NestedModulesLimit = AnalysisConstants.MaxAnalysisDepthQualty;
3838
}
3939

4040
/// <summary>
@@ -43,7 +43,7 @@ public AnalysisLimits() {
4343
/// <returns>An <see cref="AnalysisLimits"/> object representing medium level Initellisense settings.</returns>
4444
public static AnalysisLimits MakeMediumAnalysisLimits() {
4545
return new AnalysisLimits() {
46-
NestedModulesLimit = 2
46+
NestedModulesLimit = AnalysisConstants.MaxAnalysisDepthFast
4747
};
4848
}
4949

Nodejs/Product/Analysis/AnalysisConstants.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,40 @@
2222
namespace Microsoft.NodejsTools {
2323
internal sealed class AnalysisConstants {
2424
internal const string NodeModulesFolder = "node_modules";
25+
26+
/// <summary>
27+
/// Maximum practical limit for the dependencies analysis.
28+
/// </summary>
29+
/// <remarks>
30+
/// There no practical reasons to go deeper in dependencies analysis.
31+
/// Number 4 is very practical. Here the examples which hightlight idea.
32+
///
33+
/// Example 1
34+
///
35+
/// Level 0 - Large system. Some code should use properties of the object on level 4 here.
36+
/// Level 1 - Subsystem - pass data from level 4
37+
/// Level 2 - Internal dependency for company - Do some work and pass data to level 1
38+
/// Level 3 - Framework on top of which created Internal dependency.
39+
/// Level 4 - Dependency of the framework. Objects from here still should be available.
40+
/// Level 5 - Dependency of the framework provide some usefull primitive which would be used very often on the level of whole system. Hmmm.
41+
///
42+
/// Example 2 (reason why I could increase to 5)
43+
///
44+
/// Level 0 - Large system. Some code should use properties of the object on level 4 here.
45+
/// Level 1 - Subsystem - pass data from level 4
46+
/// Level 2 - Internal dependency for company - Wrap access to internal library and perform business logic. Do some work and pass data to level 1
47+
/// Level 3 - Internal library which wrap access to API.
48+
/// Level 4 - Http library.
49+
/// Level 5 - Promise Polyfill.
50+
///
51+
/// All these examples are highly speculative and I specifically try to create such deep level.
52+
/// If you develop on windows with such deep level you already close to your limit, your maximum is probably 10.
53+
/// </remarks>
54+
internal const int MaxAnalysisDepthQualty = 4;
55+
56+
/// <summary>
57+
/// Maximum practical limit for the dependencies analysis oriented on producing faster results.
58+
/// </summary>
59+
internal const int MaxAnalysisDepthFast = 2;
2560
}
2661
}

Nodejs/Product/Nodejs/Project/NodejsProjectNode.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,26 @@ internal bool IncludeNodejsFile(NodejsFileNode fileNode) {
490490
if (CommonUtils.IsSubpathOf(_intermediateOutputPath, fileNode.Url)) {
491491
return false;
492492
}
493+
494+
int maxModulesDepth = AnalysisConstants.MaxAnalysisDepthQualty;
495+
if (this._analyzer.AnalysisLevel == Options.AnalysisLevel.Medium) {
496+
maxModulesDepth = AnalysisConstants.MaxAnalysisDepthFast;
497+
}
498+
499+
var relativeFile = CommonUtils.GetRelativeFilePath(this.FullPathToChildren, fileNode.Url);
500+
int nestedModulesCount = 0;
501+
int startIndex = 0;
502+
int index = relativeFile.IndexOf(AnalysisConstants.NodeModulesFolder, startIndex, StringComparison.OrdinalIgnoreCase);
503+
while (index != -1) {
504+
nestedModulesCount++;
505+
if (nestedModulesCount > maxModulesDepth){
506+
return false;
507+
}
508+
509+
startIndex = index + 1;
510+
index = relativeFile.IndexOf(AnalysisConstants.NodeModulesFolder, startIndex, StringComparison.OrdinalIgnoreCase);
511+
}
512+
493513
foreach (var path in _analysisIgnoredDirs) {
494514
if (url.IndexOf(path, 0, StringComparison.OrdinalIgnoreCase) != -1) {
495515
return false;

0 commit comments

Comments
 (0)