Skip to content

Commit 4fd4d9d

Browse files
committed
Add optional variable replacement in comments.
1 parent 213714b commit 4fd4d9d

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Most lines should begin with one of these words:
77
## [Unreleased](https://github.com/sharpjs/Prequel/compare/release/1.0.3..HEAD)
88
- Change license from ISC to MIT.
99
- Add .NET 8.0 target framework.
10+
- Add optional variable replacement in comments. Set the `SqlCmdPreprocessor`
11+
property `EnableVariableReplacementInComments` to `true` to enable this feature.
1012
- Fix `-` not recognized in `$(…)` syntax.
1113

1214
<!--

Prequel.Tests/SqlCmdPreprocessorTests.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,19 +221,38 @@ public void Process_VariableReplacement_Normal(string eol, string eof)
221221

222222
[Test]
223223
[TestCaseSource(nameof(EolEofCases))]
224-
public void Process_VariableReplacement_WithComments(string eol, string eof)
224+
public void Process_VariableReplacement_WithComments_Default(string eol, string eof)
225225
{
226226
var preprocessor = new SqlCmdPreprocessor
227227
{
228228
Variables = { ["foo"] = "bar" }
229229
};
230230

231231
var batches = preprocessor.Process(
232-
Lines(eol, eof, "a $(FOO) b /* $(FOO) */ $(FOO) -- $(FOO)")
232+
Lines(eol, eof, "-- $(FOO)", "a $(FOO) b /* $(FOO) */ $(FOO) -- $(FOO)")
233233
);
234234

235235
batches.Should().Equal(
236-
Lines(eol, eof, "a bar b /* $(FOO) */ bar -- $(FOO)")
236+
Lines(eol, eof, "-- $(FOO)", "a bar b /* $(FOO) */ bar -- $(FOO)")
237+
);
238+
}
239+
240+
[Test]
241+
[TestCaseSource(nameof(EolEofCases))]
242+
public void Process_VariableReplacement_WithComments_ReplacementEnabled(string eol, string eof)
243+
{
244+
var preprocessor = new SqlCmdPreprocessor
245+
{
246+
Variables = { ["foo"] = "bar" },
247+
EnableVariableReplacementInComments = true
248+
};
249+
250+
var batches = preprocessor.Process(
251+
Lines(eol, eof, "-- $(FOO)", "a $(FOO) b /* $(FOO) */ $(FOO) -- $(FOO)")
252+
);
253+
254+
batches.Should().Equal(
255+
Lines(eol, eof, "-- bar", "a bar b /* bar */ bar -- bar")
237256
);
238257
}
239258

Prequel/SqlCmdPreprocessor.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ public SqlCmdPreprocessor()
6363
public IDictionary<string, string> Variables
6464
=> _variables;
6565

66+
/// <summary>
67+
/// Gets or sets whether the preprocessor performs variable replacement
68+
/// in comments. The default value is <see langword="false"/> to match
69+
/// SQLCMD behavior.
70+
/// </summary>
71+
public bool EnableVariableReplacementInComments { get; set; }
72+
6673
/// <summary>
6774
/// Preprocesses the specified text.
6875
/// </summary>
@@ -151,7 +158,11 @@ private IEnumerable<string> ProcessCore([DisallowNull] Input? input)
151158
default:
152159
case '-':
153160
case '/':
154-
// Comments are verbatim
161+
// Variable expansion requires switch to builder mode
162+
if (EnableVariableReplacementInComments && HasVariableReplacement(match.Value))
163+
return BuildNextBatch(input, start, match);
164+
165+
// Other comments are verbatim
155166
continue;
156167

157168
// Quoted
@@ -211,8 +222,11 @@ private IEnumerable<string> ProcessCore([DisallowNull] Input? input)
211222
default:
212223
case '-':
213224
case '/':
214-
// Comments are verbatim
215-
builder.Append(match.Value);
225+
// Comments are subject to variable replacement if enabled
226+
if (EnableVariableReplacementInComments)
227+
PerformVariableReplacement(match.Value);
228+
else
229+
builder.Append(match.Value);
216230
break;
217231

218232
// Quoted

0 commit comments

Comments
 (0)