Skip to content

Commit

Permalink
Validate Script.Literal strings are valid String.Format strings (fix #…
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilk committed Apr 18, 2013
1 parent 18f37b7 commit 40dc7ea
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/Core/Compiler/Compiler/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using ScriptSharp;
using ScriptSharp.CodeModel;
Expand Down Expand Up @@ -1100,6 +1101,19 @@ private Expression ProcessOpenParenExpressionNode(BinaryExpressionNode node) {
return new InlineScriptExpression("", objectType);
}

if (args.Count > 1) {
// Check whether the script is a valid string format string
try {
object[] argValues = new object[args.Count - 1];
String.Format(CultureInfo.InvariantCulture, script, argValues);
}
catch {
_errorHandler.ReportError("The argument to Script.Literal must be a valid String.Format string.",
argNodes.Expressions[0].Token.Location);
return new InlineScriptExpression("", objectType);
}
}

InlineScriptExpression scriptExpression = new InlineScriptExpression(script, objectType);
for (int i = 1; i < args.Count; i++) {
scriptExpression.AddParameterValue(args[i]);
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Compiler/Generator/ExpressionGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ private static void GenerateInlineScriptExpression(ScriptGenerator generator, Me
}
}

script = String.Format(script, parameterScripts);
script = String.Format(CultureInfo.InvariantCulture, script, parameterScripts);
}

writer.Write(script);
Expand Down
1 change: 1 addition & 0 deletions tests/TestCases/Validation/InlineScript/Code.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public void Test(int arg) {
string scriptTemplate = "alert({0} + {1})";

Script.Literal(scriptTemplate, a, a);
Script.Literal("alert({name:{0}})", "aaa");
}
}
}
3 changes: 2 additions & 1 deletion tests/ValidationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ public void TestImplicitEnums() {
[TestMethod]
public void TestInlineScript() {
string expectedErrors =
"The argument to Script.Literal must be a constant string. Code.cs(15, 28)";
"The argument to Script.Literal must be a constant string. Code.cs(15, 28)" + Environment.NewLine +
"The argument to Script.Literal must be a valid String.Format string. Code.cs(16, 28)";

Compilation compilation = CreateCompilation();
compilation.AddSource("Code.cs");
Expand Down

0 comments on commit 40dc7ea

Please sign in to comment.