-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathdotenv.js
More file actions
98 lines (96 loc) · 2.54 KB
/
dotenv.js
File metadata and controls
98 lines (96 loc) · 2.54 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/** @type {LanguageProto<'dotenv'>} */
export default {
id: 'dotenv',
optional: 'bash',
grammar () {
/**
* @param {RegExp} prefix
* @returns {Array<GrammarToken>}
*/
const commonPatterns = prefix => {
return [
{
pattern: RegExp(
prefix.source +
/(?:-?[1-9]\d*|0)(?:\\.\d+)?(?=\s*\}|\s[^}]+$|\s*$|"$)/.source
),
alias: 'number',
},
{
pattern: RegExp(prefix.source + /(?:false|true)(?=\s*\}|\s[^}]+$|\s*$)/.source),
alias: 'boolean',
},
{
// Ignore leading and trailing whitespace characters
pattern: RegExp(prefix.source + /\S[^}]*?\S(?=\s*\})/.source),
alias: 'string',
},
];
};
// Based on https://dotenvx.com/docs/env-file
return {
'comment':
/(?:^|(?<=[\s"'`]))#(?![^\n"'`]*["'`])[^\r\n \t]*(?:[ \t]+[^\r\n \t]+)*[ \t]*/,
'keyword': /^export(?=\s)/m,
'key': {
// Allow bare keys (without values)
pattern: /(?<=^[ \t]*)[a-z_]\w*(?=[ \t]*(?:=|$))/im,
alias: 'constant',
},
'value': [
{
pattern: /(?<==\s*)(?:-?[1-9]\d*|0)(?:\.\d+)?(?=\s*$)/,
alias: 'number',
},
{
pattern: /(?<==\s*)(?:false|true)(?=\s*$)/,
alias: 'boolean',
},
{
pattern:
/(?<==\s*)(?:(['"`])(?:\\[\s\S]|(?!\1)[^\\])*?\1|\S(?:.*?\S)?)(?=\s*$|\s+#.*$)/m,
alias: 'string',
inside: {
'command-substitution': {
// Command substitution is disabled in strings enclosed in "'" (single quotes) and "`" (backticks)
pattern: /(?<!['`][\s\S]*)\$\(.+\)/,
inside: {
'command-substitution-punctuation': {
pattern: /\$\(|\)/,
alias: 'punctuation',
},
'shell-command': {
pattern: /.+/,
inside: 'bash',
},
},
},
'variable-expansion': {
// Variable expansion is disabled in strings enclosed in "'" (single quotes) and "`" (backticks)
pattern: /(?<!['`][\s\S]*)\$(?:\{[^{]+\}|[^\s"]+)/,
inside: {
'variable': /(?<=\$\{|\$)[a-z_]\w*/i,
'default-value': commonPatterns(/(?<=(?::-|-)\s*)/),
'alternative-value': commonPatterns(/(?<=(?::\+|\+)\s*)/),
'variable-expansion-punctuation': {
pattern: /\$\{|:-|:\+|[$}+-]/,
alias: 'punctuation',
},
},
},
'escape-sequence': {
pattern: /\\['"`#\\nrt]/,
alias: 'char',
},
'punctuation': /^['"`]|['"`]$/,
},
},
],
'assignment-operator': {
pattern: /=/,
alias: 'operator',
},
};
},
};
/** @import { GrammarToken, LanguageProto } from '../types.d.ts'; */