Skip to content

Commit

Permalink
feat: Porting JavaScript Template to Language Function (Fixes #604)
Browse files Browse the repository at this point in the history
  • Loading branch information
James Brundage committed Nov 11, 2023
1 parent 495277e commit 75011c4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 103 deletions.
59 changes: 59 additions & 0 deletions Commands/Languages/JavaScript/JavaScript-Language.ps.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
Language function JavaScript {
<#
.SYNOPSIS
JavaScript Language Definition.
.DESCRIPTION
Allows PipeScript to generate JavaScript.
Multiline comments with /*{}*/ will be treated as blocks of PipeScript.
String output from these blocks will be embedded directly. All other output will be converted to JSON.
Multiline comments can be preceeded or followed by 'empty' syntax, which will be ignored.
The JavaScript Inline Transpiler will consider the following syntax to be empty:
* ```undefined```
* ```null```
* ```""```
* ```''```
.EXAMPLE
$helloJs = Hello.js template '
msg = null /*{param($msg = ''hello world'') "`"$msg`""}*/ ;
if (console) {
console.log(msg);
}
'
.EXAMPLE
$helloMsg = {param($msg = 'hello world') "`"$msg`""}
$helloJs = HelloWorld.js template "
msg = null /*{$helloMsg}*/;
if (console) {
console.log(msg);
}
"
#>
[ValidatePattern('\.js$')]
param(
)

# We start off by declaring a number of regular expressions:
$startComment = '/\*' # * Start Comments ```\*```
$endComment = '\*/' # * End Comments ```/*```
$Whitespace = '[\s\n\r]{0,}'
# * IgnoredContext ```String.empty```, ```null```, blank strings and characters
$IgnoredContext = "(?<ignore>(?>$("undefined", "null", '""', "''" -join '|'))\s{0,}){0,1}"

$StartPattern = "(?<PSStart>${IgnoredContext}${startComment}\{$Whitespace)"
$EndPattern = "(?<PSEnd>$Whitespace\}${endComment}\s{0,}${IgnoredContext})"

$ForeachObject = {
$in = $_
if (($in -is [string]) -or
($in -ne $null -and $in.GetType().IsPrimitive)) {
"$in"
} else {
"$(ConvertTo-Json -Depth 100 -InputObject $in)"
}
}
}
103 changes: 0 additions & 103 deletions Transpilers/Templates/JavaScript.Template.psx.ps1

This file was deleted.

0 comments on commit 75011c4

Please sign in to comment.