@@ -38,51 +38,52 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
38
38
39
39
foreach ( FunctionDefinitionAst funcAst in functionAsts )
40
40
{
41
- if ( funcAst . Body == null || funcAst . Body . ParamBlock == null
42
- || funcAst . Body . ParamBlock . Attributes == null || funcAst . Body . ParamBlock . Parameters == null )
41
+ if ( funcAst . Body == null || funcAst . Body . ParamBlock == null || funcAst . Body . ParamBlock . Parameters == null )
43
42
{
44
43
continue ;
45
44
}
46
45
47
- foreach ( var paramAst in funcAst . Body . ParamBlock . Parameters )
48
- {
49
- foreach ( var paramAstAttribute in paramAst . Attributes )
46
+ foreach ( ParameterAst paramAst in funcAst . Body . ParamBlock . Parameters )
47
+ {
48
+ if ( paramAst == null )
50
49
{
51
- if ( ! ( paramAstAttribute is AttributeAst ) )
50
+ continue ;
51
+ }
52
+
53
+ foreach ( AttributeBaseAst attributeAst in paramAst . Attributes )
54
+ {
55
+ var paramAttributeAst = attributeAst as AttributeAst ;
56
+ if ( paramAttributeAst == null )
52
57
{
53
58
continue ;
54
59
}
55
60
56
- var namedArguments = ( paramAstAttribute as AttributeAst ) . NamedArguments ;
57
-
61
+ var namedArguments = paramAttributeAst . NamedArguments ;
58
62
if ( namedArguments == null )
59
63
{
60
64
continue ;
61
65
}
62
-
66
+
63
67
foreach ( NamedAttributeArgumentAst namedArgument in namedArguments )
64
68
{
65
- if ( ! ( namedArgument . ArgumentName . Equals ( "HelpMessage" , StringComparison . OrdinalIgnoreCase ) ) )
69
+ if ( namedArgument == null || ! ( namedArgument . ArgumentName . Equals ( "HelpMessage" , StringComparison . OrdinalIgnoreCase ) ) )
66
70
{
67
71
continue ;
68
72
}
69
73
70
74
string errCondition ;
71
- if ( namedArgument . ExpressionOmitted
72
- || namedArgument . Argument . Extent . Text . Equals ( "\" \" " )
73
- || namedArgument . Argument . Extent . Text . Equals ( "\' \' " ) )
75
+ if ( namedArgument . ExpressionOmitted || HasEmptyStringInExpression ( namedArgument . Argument ) )
74
76
{
75
77
errCondition = "empty" ;
76
78
}
77
- else if ( namedArgument . Argument . Extent . Text . Equals ( "$null" , StringComparison . OrdinalIgnoreCase ) )
79
+ else if ( HasNullInExpression ( namedArgument . Argument ) )
78
80
{
79
81
errCondition = "null" ;
80
82
}
81
83
else
82
84
{
83
85
errCondition = null ;
84
86
}
85
-
86
87
if ( ! String . IsNullOrEmpty ( errCondition ) )
87
88
{
88
89
string message = string . Format ( CultureInfo . CurrentCulture ,
@@ -102,6 +103,30 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
102
103
}
103
104
}
104
105
106
+ /// <summary>
107
+ /// Checks if the given ast is an empty string.
108
+ /// </summary>
109
+ /// <param name="ast"></param>
110
+ /// <returns></returns>
111
+ private bool HasEmptyStringInExpression ( ExpressionAst ast )
112
+ {
113
+ var constStrAst = ast as StringConstantExpressionAst ;
114
+ return constStrAst != null && constStrAst . Value . Equals ( String . Empty ) ;
115
+ }
116
+
117
+ /// <summary>
118
+ /// Checks if the ast contains null expression.
119
+ /// </summary>
120
+ /// <param name="ast"></param>
121
+ /// <returns></returns>
122
+ private bool HasNullInExpression ( Ast ast )
123
+ {
124
+ var varExprAst = ast as VariableExpressionAst ;
125
+ return varExprAst != null
126
+ && varExprAst . VariablePath . IsUnqualified
127
+ && varExprAst . VariablePath . UserPath . Equals ( "null" , StringComparison . OrdinalIgnoreCase ) ;
128
+ }
129
+
105
130
/// <summary>
106
131
/// GetName: Retrieves the name of this rule.
107
132
/// </summary>
0 commit comments