-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathJavaScript-Language.ps.ps1
More file actions
75 lines (64 loc) · 2.18 KB
/
Copy pathJavaScript-Language.ps.ps1
File metadata and controls
75 lines (64 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
Language function JavaScript {
<#
.SYNOPSIS
JavaScript PipeScript 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
Invoke-PipeScript {
Hello.js template '
msg = null /*{param($msg = ''hello world'') "`"$msg`""}*/ ;
if (console) {
console.log(msg);
}
'
}
.EXAMPLE
Invoke-PipeScript {
$helloMsg = {param($msg = 'hello world') "`"$msg`""}
$helloJs = HelloWorld.js template "
msg = null /*{$helloMsg}*/;
if (console) {
console.log(msg);
}
"
$helloJs
}
.EXAMPLE
"console.log('hi')" > .\Hello.js
Invoke-PipeScript .\Hello.js
#>
[ValidatePattern('\.m?js$')]
param(
)
# JavaScript's file Pattern is `\.js$` or `.mjs$`
$FilePattern = '\.m?js$'
# and JavaScript is a case sensitive language.
$CaseSensitive = $true
# 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})"
$Interpreter = 'node' # Get the first node, if present
$ForeachObject = {
$in = $_
if (($in -is [string]) -or
($in -ne $null -and $in.GetType().IsPrimitive)) {
"$in"
} else {
"$(ConvertTo-Json -Depth 100 -InputObject $in)"
}
}
}