@scalar/highlight 0.3.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/CHANGELOG.md +59 -0
  2. package/README.md +52 -13
  3. package/dist/all.d.ts.map +1 -1
  4. package/dist/all.js +2 -0
  5. package/dist/compat/languages.d.ts.map +1 -1
  6. package/dist/compat/languages.js +2 -4
  7. package/dist/compat/syntax-highlight.d.ts.map +1 -1
  8. package/dist/compat/syntax-highlight.js +101 -27
  9. package/dist/core/compile.d.ts +9 -1
  10. package/dist/core/compile.d.ts.map +1 -1
  11. package/dist/core/compile.js +168 -9
  12. package/dist/core/index.d.ts +1 -0
  13. package/dist/core/index.d.ts.map +1 -1
  14. package/dist/core/index.js +1 -0
  15. package/dist/core/tokenize.d.ts.map +1 -1
  16. package/dist/core/tokenize.js +147 -7
  17. package/dist/core/types.d.ts +76 -1
  18. package/dist/core/types.d.ts.map +1 -1
  19. package/dist/core/types.js +5 -1
  20. package/dist/index.d.ts +1 -0
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js +1 -0
  23. package/dist/langs/bash.d.ts.map +1 -1
  24. package/dist/langs/bash.js +60 -13
  25. package/dist/langs/cpp.d.ts.map +1 -1
  26. package/dist/langs/cpp.js +8 -0
  27. package/dist/langs/csharp.d.ts.map +1 -1
  28. package/dist/langs/csharp.js +11 -5
  29. package/dist/langs/css.d.ts.map +1 -1
  30. package/dist/langs/css.js +111 -9
  31. package/dist/langs/diff.d.ts +2 -1
  32. package/dist/langs/diff.d.ts.map +1 -1
  33. package/dist/langs/diff.js +19 -7
  34. package/dist/langs/go.d.ts.map +1 -1
  35. package/dist/langs/go.js +22 -1
  36. package/dist/langs/haskell.d.ts.map +1 -1
  37. package/dist/langs/haskell.js +33 -0
  38. package/dist/langs/html.d.ts +0 -10
  39. package/dist/langs/html.d.ts.map +1 -1
  40. package/dist/langs/html.js +63 -24
  41. package/dist/langs/http.d.ts.map +1 -1
  42. package/dist/langs/http.js +25 -9
  43. package/dist/langs/javascript.d.ts.map +1 -1
  44. package/dist/langs/javascript.js +279 -30
  45. package/dist/langs/json.d.ts.map +1 -1
  46. package/dist/langs/json.js +7 -1
  47. package/dist/langs/julia.d.ts +4 -0
  48. package/dist/langs/julia.d.ts.map +1 -0
  49. package/dist/langs/julia.js +181 -0
  50. package/dist/langs/kotlin.d.ts.map +1 -1
  51. package/dist/langs/kotlin.js +8 -1
  52. package/dist/langs/markdown.d.ts +9 -3
  53. package/dist/langs/markdown.d.ts.map +1 -1
  54. package/dist/langs/markdown.js +127 -6
  55. package/dist/langs/nginx.d.ts.map +1 -1
  56. package/dist/langs/nginx.js +26 -1
  57. package/dist/langs/ocaml.d.ts.map +1 -1
  58. package/dist/langs/ocaml.js +16 -7
  59. package/dist/langs/php.d.ts.map +1 -1
  60. package/dist/langs/php.js +69 -19
  61. package/dist/langs/python.d.ts.map +1 -1
  62. package/dist/langs/python.js +9 -2
  63. package/dist/langs/r.js +9 -9
  64. package/dist/langs/ruby.d.ts.map +1 -1
  65. package/dist/langs/ruby.js +8 -7
  66. package/dist/langs/rust.d.ts.map +1 -1
  67. package/dist/langs/rust.js +27 -11
  68. package/dist/langs/sql.d.ts.map +1 -1
  69. package/dist/langs/sql.js +9 -3
  70. package/dist/langs/swift.d.ts.map +1 -1
  71. package/dist/langs/swift.js +27 -1
  72. package/dist/langs/yaml.d.ts +0 -7
  73. package/dist/langs/yaml.d.ts.map +1 -1
  74. package/dist/langs/yaml.js +157 -28
  75. package/dist/lazy.d.ts +1 -0
  76. package/dist/lazy.d.ts.map +1 -1
  77. package/dist/lazy.js +2 -0
  78. package/package.json +2 -2
@@ -5,9 +5,11 @@
5
5
  * and character entities are pulled out of the text so `&` reads as markup
6
6
  * rather than as content.
7
7
  *
8
- * `<script>` and `<style>` bodies are left unstyled: highlighting them means
9
- * running another grammar, which is a follow-up (see README).
8
+ * `<script>` and `<style>` bodies are highlighted as JavaScript and CSS when
9
+ * those grammars are registered, and left unstyled otherwise — the HTML
10
+ * grammar never imports them, so it does not cost their weight.
10
11
  */
12
+ const ENTITY = { match: '&(?:#x?[0-9a-fA-F]+|[a-zA-Z][a-zA-Z0-9]*);', scope: 'string.escape' };
11
13
  const html = {
12
14
  name: 'html',
13
15
  aliases: ['xml', 'svg', 'vue'],
@@ -15,28 +17,77 @@ const html = {
15
17
  root: {
16
18
  ignoreCase: true,
17
19
  rules: [
18
- { match: '<!--[\\s\\S]*?-->', scope: 'comment' },
19
- { match: '<!\\[CDATA\\[[\\s\\S]*?\\]\\]>', scope: 'string' },
20
+ // States rather than lazy `[\\s\\S]*?` scans: those run to EOF and
21
+ // fail once per opener, which is O(n²) on unterminated `<!--` runs.
22
+ { match: '<!--', scope: 'comment', push: 'comment' },
23
+ { match: '<!\\[CDATA\\[', scope: 'string', push: 'cdata' },
20
24
  {
21
25
  match: '(<!)(DOCTYPE)([^>]*)(>)',
22
26
  scope: ['punctuation.bracket', 'keyword', null, 'punctuation.bracket'],
23
27
  },
24
- { match: '<\\?[\\s\\S]*?\\?>', scope: 'comment' },
28
+ // `<?xml version="1.0"?>` and other processing instructions are
29
+ // metadata about the document, not commentary on it.
30
+ { match: '<\\?', scope: 'decorator', push: 'processing-instruction' },
31
+ { match: '(<)(script)(?=[\\s/>])', scope: ['punctuation.bracket', 'tag'], push: 'script-tag' },
32
+ { match: '(<)(style)(?=[\\s/>])', scope: ['punctuation.bracket', 'tag'], push: 'style-tag' },
25
33
  {
26
34
  match: '(</?)([A-Za-z_][\\w:.-]*)',
27
35
  scope: ['punctuation.bracket', 'tag'],
28
36
  push: 'tag',
29
37
  },
30
- {
31
- match: '&(?:#x?[0-9a-fA-F]+|[a-zA-Z][a-zA-Z0-9]*);',
32
- scope: 'string.escape',
33
- },
38
+ ENTITY,
34
39
  ],
35
40
  },
41
+ comment: {
42
+ default: 'comment',
43
+ rules: [{ match: '-->', scope: 'comment', pop: true }],
44
+ },
45
+ cdata: {
46
+ default: 'string',
47
+ rules: [{ match: '\\]\\]>', scope: 'string', pop: true }],
48
+ },
49
+ 'processing-instruction': {
50
+ default: 'decorator',
51
+ rules: [{ match: '\\?>', scope: 'decorator', pop: true }],
52
+ },
36
53
  tag: {
54
+ ignoreCase: true,
55
+ rules: [{ match: '/?>', scope: 'punctuation.bracket', pop: true }, { include: 'attributes' }],
56
+ },
57
+ // The opening tag of an element whose body is another language. `>` hands
58
+ // over to the body; a self-closing tag has none.
59
+ 'script-tag': {
60
+ ignoreCase: true,
61
+ rules: [
62
+ { match: '/>', scope: 'punctuation.bracket', pop: true },
63
+ { match: '>', scope: 'punctuation.bracket', set: 'script-body' },
64
+ { include: 'attributes' },
65
+ ],
66
+ },
67
+ 'style-tag': {
68
+ ignoreCase: true,
69
+ rules: [
70
+ { match: '/>', scope: 'punctuation.bracket', pop: true },
71
+ { match: '>', scope: 'punctuation.bracket', set: 'style-body' },
72
+ { include: 'attributes' },
73
+ ],
74
+ },
75
+ 'script-body': {
76
+ ignoreCase: true,
77
+ embed: { language: 'javascript', until: '</script(?=[\\s/>])' },
78
+ rules: [],
79
+ },
80
+ 'style-body': {
81
+ ignoreCase: true,
82
+ embed: { language: 'css', until: '</style(?=[\\s/>])' },
83
+ rules: [],
84
+ },
85
+ attributes: {
37
86
  ignoreCase: true,
38
87
  rules: [
39
- { match: '/?>', scope: 'punctuation.bracket', pop: true },
88
+ // An unquoted value, before the attribute-name rule: `type=text` is a
89
+ // name and a value, not two names.
90
+ { match: '(=)([ \\t]*)([^\\s"\'=<>`]+)', scope: ['operator', null, 'string'] },
40
91
  // Framework-flavoured names (`:prop`, `@click`, `v-if`, `{...}`) are
41
92
  // common enough in templates to be worth matching as attributes.
42
93
  { match: '[@:#v-]?[A-Za-z_][\\w:.-]*', scope: 'tag.attribute' },
@@ -48,23 +99,11 @@ const html = {
48
99
  },
49
100
  'attr-double': {
50
101
  default: 'string',
51
- rules: [
52
- {
53
- match: '&(?:#x?[0-9a-fA-F]+|[a-zA-Z][a-zA-Z0-9]*);',
54
- scope: 'string.escape',
55
- },
56
- { match: '"', scope: 'string', pop: true },
57
- ],
102
+ rules: [ENTITY, { match: '"', scope: 'string', pop: true }],
58
103
  },
59
104
  'attr-single': {
60
105
  default: 'string',
61
- rules: [
62
- {
63
- match: '&(?:#x?[0-9a-fA-F]+|[a-zA-Z][a-zA-Z0-9]*);',
64
- scope: 'string.escape',
65
- },
66
- { match: "'", scope: 'string', pop: true },
67
- ],
106
+ rules: [ENTITY, { match: "'", scope: 'string', pop: true }],
68
107
  },
69
108
  },
70
109
  };
@@ -1 +1 @@
1
- {"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../src/langs/http.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAiC5C,QAAA,MAAM,IAAI,EAAE,OAsKX,CAAA;AAED,eAAe,IAAI,CAAA"}
1
+ {"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../src/langs/http.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AA2C5C,QAAA,MAAM,IAAI,EAAE,OAgLX,CAAA;AAED,eAAe,IAAI,CAAA"}
@@ -9,21 +9,29 @@
9
9
  * - a header name is only matched from the start of a line, which is the only
10
10
  * way to tell it from the colons inside `Host: localhost:8080` or a timestamp
11
11
  * - the blank line after the headers switches into the body, where JSON — the
12
- * body that actually shows up in API docs — is highlighted as JSON
12
+ * body that actually shows up in API docs — is highlighted as JSON, and an
13
+ * XML body is handed to the HTML grammar when that is registered
13
14
  *
14
15
  * Comments are not part of HTTP. The `#` and `//` lines and the `###` separator
15
16
  * come from the request-file format editors and REST clients use, and they are
16
17
  * recognised outside the body only, where a `#` is far more likely to be data.
17
18
  *
18
- * What it does not model: a method has to be uppercase, a body that is not JSON
19
- * — form-encoded, XML, multipart — renders as plain text, and chunk sizes are
20
- * not distinguished from the chunks around them.
19
+ * What it does not model: a method has to be uppercase, a form-encoded or
20
+ * multipart body renders as plain text, and chunk sizes are not distinguished
21
+ * from the chunks around them.
21
22
  */
22
23
  /**
23
24
  * The methods RFC 9110 defines. Doubles as the anchor for "a new message starts
24
25
  * here", which is how a body knows where it ends.
25
26
  */
26
27
  const METHOD = 'GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS|TRACE|CONNECT';
28
+ /** What begins the next message in a document: a separator, a status line or a request line. */
29
+ const MESSAGE_START = `###|HTTP/\\d|(?:${METHOD})[ \\t]`;
30
+ /**
31
+ * Where a body ends: the next message, or a comment line directly above it.
32
+ * The same two exits the `body` state takes, as one pattern for an embed.
33
+ */
34
+ const NEXT_MESSAGE = `^(?:${MESSAGE_START}|[ \\t]*(?://|#)[^\\n]*\\n(?:${MESSAGE_START}))`;
27
35
  /** `HTTP/1.1`, `HTTP/2` — scoped in three pieces so the version reads as a number. */
28
36
  const VERSION = '(HTTP)(/)(\\d(?:\\.\\d)?)';
29
37
  const http = {
@@ -114,9 +122,9 @@ const http = {
114
122
  ],
115
123
  },
116
124
  /**
117
- * The body. JSON is highlighted as JSON; XML, form-encoded and plain bodies
118
- * fall through unscoped rather than being guessed at, which is the quiet
119
- * failure rather than the loud one.
125
+ * The body. JSON is highlighted as JSON and XML or HTML with the HTML
126
+ * grammar; form-encoded and plain bodies fall through unscoped rather than
127
+ * being guessed at, which is the quiet failure rather than the loud one.
120
128
  */
121
129
  body: {
122
130
  rules: [
@@ -125,7 +133,7 @@ const http = {
125
133
  // of the three can begin a line of JSON — a plain-text body that starts
126
134
  // a line with `GET ` is read as a new message, which is the price of
127
135
  // supporting multi-message documents at all.
128
- { match: `^(?=###|HTTP/\\d|(?:${METHOD})[ \\t])`, set: 'root' },
136
+ { match: `^(?=${MESSAGE_START})`, set: 'root' },
129
137
  // A comment introducing the next message also ends the body, but only
130
138
  // when the message starts on the very next line. A bare `^#` exit
131
139
  // would be wrong far more often than right — `#` is ordinary data in a
@@ -148,9 +156,12 @@ const http = {
148
156
  // exits the body to a state that cannot parse what the lookahead
149
157
  // matched, leaving the line unscoped and reading the line after it
150
158
  // as a header.
151
- match: `^(?=[ \\t]*(?://|#)[^\\n]*\\n(?:###|HTTP/\\d|(?:${METHOD})[ \\t]))`,
159
+ match: `^(?=[ \\t]*(?://|#)[^\\n]*\\n(?:${MESSAGE_START}))`,
152
160
  set: 'root',
153
161
  },
162
+ // A body that opens a line with `<` is XML or HTML, which the HTML
163
+ // grammar highlights up to where the next message starts.
164
+ { match: '^(?=[ \\t]*<)', push: 'markup' },
154
165
  // A string followed by `:` is a key, at any depth.
155
166
  {
156
167
  match: '("(?:[^"\\\\\\n]|\\\\.)*")([ \\t]*)(:)',
@@ -167,6 +178,11 @@ const http = {
167
178
  { match: '[,:]', scope: 'punctuation.delimiter' },
168
179
  ],
169
180
  },
181
+ /** An XML or HTML body. Unscoped when the HTML grammar is not registered. */
182
+ markup: {
183
+ embed: { language: 'html', until: NEXT_MESSAGE },
184
+ rules: [],
185
+ },
170
186
  string: {
171
187
  default: 'string',
172
188
  rules: [
@@ -1 +1 @@
1
- {"version":3,"file":"javascript.d.ts","sourceRoot":"","sources":["../../src/langs/javascript.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAsC5C,QAAA,MAAM,UAAU,EAAE,OAmNjB,CAAA;AAED,eAAe,UAAU,CAAA"}
1
+ {"version":3,"file":"javascript.d.ts","sourceRoot":"","sources":["../../src/langs/javascript.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAkF5C,QAAA,MAAM,UAAU,EAAE,OAwajB,CAAA;AAED,eAAe,UAAU,CAAA"}
@@ -12,6 +12,9 @@
12
12
  * is the cheapest reliable way to tell `a / b` from `/ab+/g`.
13
13
  * - Template literals nest properly: `` `${ {a: `x`} }` `` keeps its states
14
14
  * straight because interpolations track brace depth.
15
+ * - JSX children are text, not code. Once an opening tag closes, everything up
16
+ * to the matching closing tag is read as text with `{…}` expressions, so the
17
+ * apostrophe in `<p>Don't</p>` cannot open a string.
15
18
  */
16
19
  /**
17
20
  * An identifier, with its tail length-capped.
@@ -27,6 +30,41 @@
27
30
  const ID = '[A-Za-z_$\\u0080-\\uFFFF][\\w$\\u0080-\\uFFFF]{0,128}';
28
31
  /** Contexts after which a `/` begins a regex literal rather than division. */
29
32
  const BEFORE_REGEX = '(?:[=(,:;\\[!&|?{}+\\-*%^~<>]|=>|\\breturn|\\bcase|\\btypeof|\\bin|\\bof|\\bnew|\\bdo|\\byield|\\bawait|\\belse)';
33
+ /**
34
+ * Contexts after which a `<` can open a JSX element: wherever an expression
35
+ * can start. Everything else — an identifier, `)` or `]` — makes `<` a
36
+ * comparison or a type argument list.
37
+ */
38
+ const BEFORE_JSX = '(?:^|[=(,?:{\\[;!&|]|=>|\\breturn|\\byield|\\bdefault|\\bawait)';
39
+ /**
40
+ * TypeScript angle brackets that look like an element with no attributes: a
41
+ * generic arrow function `<T>(x: T) => x`, a constrained one `<T extends X>`,
42
+ * a defaulted one `<T = unknown>`, and the old-style assertion `<HTMLElement>el`. Reading any of them as an
43
+ * opening tag would turn the rest of the file into JSX text.
44
+ *
45
+ * An assertion is followed by an expression on the same line, and so is an
46
+ * element's text, so the line decides: an element without attributes opens
47
+ * only when its line ends after the `>` or holds another tag. A closing tag
48
+ * counts, and so does a `<` that does not follow a name, which keeps the type
49
+ * arguments in `get<Store>()` out. An element whose text runs on to a later
50
+ * closing tag reads as code instead, which costs that one element and never
51
+ * the rest of the file.
52
+ */
53
+ const NOT_TS_AFTER = '(?!>[ \\t]*\\()(?!\\s+(?:extends\\b|=))(?!>(?![ \\t]*$)(?![^\\n]{0,512}(?:<\\/|(?<![\\w$)\\]])<[A-Za-z>])))';
54
+ /**
55
+ * Ends a keyword rule: the word is not followed by the `:` of an object key or
56
+ * a type member. Checked only after a keyword has matched, so it costs nothing
57
+ * on ordinary identifiers.
58
+ */
59
+ const NOT_KEY = '(?!\\??:)';
60
+ /** A JSX tag name: a component, a member expression, or a lowercase element. */
61
+ const TAG = '[A-Z][\\w.]*|[a-z][\\w-]*(?:[.:][\\w-]+)*';
62
+ /**
63
+ * What follows a name that is assigned a function: `= () =>`, `= async x =>`,
64
+ * `= function`, and the generic `= <T,>(x: T) =>`. The parameter list cannot
65
+ * nest parentheses, which covers real code and keeps the scan bounded.
66
+ */
67
+ const ASSIGNS_FUNCTION = `[ \\t]*=[ \\t]*(?:async[ \\t]*)?(?:function\\b|(?:<[^<>()=]{0,128}>[ \\t]*)?\\([^()]{0,512}\\)[ \\t]*(?::[^=;\\n]{0,256})?=>|${ID}[ \\t]*=>)`;
30
68
  /** Body of a regex literal: no unescaped `/`, character classes may hold one. */
31
69
  const REGEX = '/(?![/*])(?:[^/\\\\\\n\\[]|\\\\.|\\[(?:[^\\]\\\\\\n]|\\\\.)*\\])+/[dgimsuvy]*';
32
70
  const javascript = {
@@ -36,8 +74,10 @@ const javascript = {
36
74
  root: { rules: [{ include: 'expression' }] },
37
75
  expression: {
38
76
  rules: [
39
- { match: '/\\*\\*(?![/*])[\\s\\S]*?\\*/', scope: 'comment.doc' },
40
- { match: '/\\*[\\s\\S]*?\\*/', scope: 'comment' },
77
+ // A state, not a lazy `[\\s\\S]*?` scan: that form runs to EOF and fails
78
+ // once per opener, which is O(n²) on a run of unterminated `/*`.
79
+ { match: '/\\*\\*(?![/*])', scope: 'comment.doc', push: 'doc-comment' },
80
+ { match: '/\\*', scope: 'comment', push: 'block-comment' },
41
81
  { match: '//[^\\n]*', scope: 'comment' },
42
82
  { match: '^#!.*', scope: 'comment' },
43
83
  { include: 'strings' },
@@ -47,32 +87,86 @@ const javascript = {
47
87
  match: `(${BEFORE_REGEX})(\\s*)(${REGEX})`,
48
88
  scope: ['operator', null, 'regexp'],
49
89
  },
50
- { match: `^(\\s*)(${REGEX})`, scope: [null, 'regexp'] },
90
+ // `if (ok) /re/.test(s)`: a `)` that closes a control condition starts
91
+ // an expression. The lookbehind sits after the `/` so the rule still
92
+ // starts with a literal character.
93
+ {
94
+ match: `/(?<=(?<![\\w$.])(?:if|while|for)[ \\t]*\\([^()\\n]{0,256}\\)[ \\t]*/)${REGEX.slice(1)}`,
95
+ scope: 'regexp',
96
+ },
97
+ // Blanks only: `\s*` would run across every blank line below, and
98
+ // retrying that at each line start is O(n²) on a run of blank lines.
99
+ { match: `^([ \\t]*)(${REGEX})`, scope: [null, 'regexp'] },
51
100
  { include: 'numbers' },
52
101
  { include: 'jsx' },
53
102
  { match: `(@)(${ID}(?:\\.${ID})*)`, scope: ['decorator', 'decorator'] },
54
103
  {
55
- match: `(\\.)([ \\t]*)(${ID})(?=[ \\t]*\\()`,
104
+ match: `(\\.)([ \\t]*)(#?${ID})(?=[ \\t]*\\()`,
56
105
  scope: ['punctuation', null, 'function.method'],
57
106
  },
58
107
  {
59
- match: `(\\?\\.|\\.)([ \\t]*)(${ID})`,
108
+ match: `(\\?\\.|\\.)([ \\t]*)(#?${ID})`,
60
109
  scope: ['punctuation', null, 'variable.member'],
61
110
  },
111
+ // A private field or method where it is declared, or `#x in obj`.
112
+ { match: `#${ID}`, scope: 'variable.member' },
113
+ // `handler = function …` and `this.run = () => …` name a function as
114
+ // surely as a declaration does.
115
+ { match: `\\b${ID}(?=${ASSIGNS_FUNCTION})`, scope: 'function' },
116
+ // A keyword followed by `:` is an object key or a property in a type —
117
+ // `{ type: 'object' }`, `{ from: start, to: end }` — so each keyword
118
+ // rule ends with `NOT_KEY` and the word falls through to the property
119
+ // rule. `default:` needs telling apart: in a `switch` it is followed by
120
+ // a statement or nothing, as an object key by a value. A braced block
121
+ // after it reads as a key, since `responses: { default: { … } }` is the
122
+ // commoner of the two in API code.
123
+ //
124
+ // The contextual keywords that are also common method names (`get`,
125
+ // `set`, `of`, `delete`, `from`, `type`) stay keywords only when no `(`
126
+ // follows directly, so `static of(x)` and `cache.get(key)` read as calls.
127
+ {
128
+ match: '\\bdefault\\b(?=[ \\t]*:[ \\t]*(?:$|\\/[/*]|\\b(?:return|break|continue|throw|case)\\b))|\\bdefault\\b(?!\\??:)',
129
+ scope: 'keyword.control',
130
+ },
62
131
  {
63
- match: '\\b(?:if|else|for|while|do|switch|case|default|break|continue|return|throw|try|catch|finally|await|yield)\\b',
132
+ match: `\\b(?:if|else|for|while|do|switch|case|break|continue|return|throw|try|catch|finally|await|yield)\\b${NOT_KEY}`,
64
133
  scope: 'keyword.control',
65
134
  },
135
+ // A TypeScript assertion ends an expression; it does not declare the
136
+ // next name, even when that name begins a new line without a semicolon.
137
+ { match: '\\bconst\\b(?<=\\bas\\s+const)', scope: 'keyword.declaration' },
138
+ // The declared name is scoped by a state of its own rather than by a
139
+ // rule here: a rule that has to look back at `const` runs at every
140
+ // identifier in the file, while the state only runs after the keyword.
141
+ { match: '\\b(?:const|let|var)\\b', scope: 'keyword.declaration', push: 'declared-name' },
142
+ // A parameter list reads its names as parameters: after `function` and
143
+ // its optional name, or before the `=>` of an arrow function. The arrow
144
+ // lookahead cannot see past nested parentheses, so a list holding a
145
+ // call in a default value or a function type keeps reading as plain
146
+ // code. Quotes and slashes also force that fallback: a string, comment
147
+ // or regex can contain `) =>` without introducing an arrow function.
148
+ {
149
+ match: `(\\bfunction\\b)([ \\t]*\\*?[ \\t]*)(?:(${ID})([ \\t]*))?(\\()`,
150
+ scope: ['keyword.declaration', null, 'function', null, 'punctuation.bracket'],
151
+ push: 'params',
152
+ },
153
+ {
154
+ match: '\\((?=[^()"\'`/]{0,512}\\)[ \\t]*(?::[^=;\\n"\'`/]{0,256})?=>)',
155
+ scope: 'punctuation.bracket',
156
+ push: 'params',
157
+ },
66
158
  {
67
- match: '\\b(?:function|class|const|let|var|interface|enum|namespace|module|declare|abstract|implements|extends|constructor|static|readonly|override|public|private|protected|async|get|set|type)\\b',
159
+ match: `\\b(?:function|class|interface|enum|namespace|module|declare|abstract|implements|extends|constructor|static|readonly|override|public|private|protected|async|get(?!\\()|set(?!\\()|type(?!\\())\\b${NOT_KEY}`,
68
160
  scope: 'keyword.declaration',
69
161
  },
70
162
  {
71
- match: '\\b(?:import|export|from|require)\\b',
163
+ match: `\\b(?:import|export|from(?!\\()|require)\\b${NOT_KEY}`,
72
164
  scope: 'keyword.import',
73
165
  },
166
+ // Import attributes: `import data from './x.json' with { type: 'json' }`.
167
+ { match: '\\bwith(?=[ \\t]*\\{)', scope: 'keyword.import' },
74
168
  {
75
- match: '\\b(?:typeof|instanceof|in|of|new|delete|void|satisfies|keyof|infer|asserts|as)\\b',
169
+ match: `\\b(?:typeof|instanceof|in|of(?!\\()|new|delete(?!\\()|void|satisfies|keyof|infer|asserts|as)\\b${NOT_KEY}`,
76
170
  scope: 'keyword.operator',
77
171
  },
78
172
  { match: '\\b(?:true|false)\\b', scope: 'boolean' },
@@ -85,20 +179,23 @@ const javascript = {
85
179
  scope: 'variable.builtin',
86
180
  },
87
181
  {
88
- match: '\\b(?:console|window|document|process|module|exports)\\b',
182
+ match: `\\b(?:console|window|document|process|module|exports)\\b${NOT_KEY}`,
89
183
  scope: 'variable.builtin',
90
184
  },
91
185
  {
92
- match: '\\b(?:string|number|boolean|symbol|bigint|object|unknown|never|any|void|readonly)\\b(?=[^\\w$]|$)',
186
+ match: `\\b(?:string|number|boolean|symbol|bigint|object|unknown|never|any|void|readonly)\\b(?=[^\\w$]|$)${NOT_KEY}`,
93
187
  scope: 'type.builtin',
94
188
  },
95
189
  { match: '\\b[A-Z][A-Z0-9_]+\\b(?![\\w$])', scope: 'constant' },
190
+ // A capitalised name followed by a call is still a call — `Error('x')`,
191
+ // `new Date()`, a React component invoked directly — not a type.
192
+ { match: '\\b[A-Z][\\w$]{0,128}(?=[ \\t]*\\()', scope: 'function.call' },
96
193
  { match: '\\b[A-Z][A-Za-z0-9_$]*\\b', scope: 'type' },
97
194
  // `key:` in an object literal, and the name half of a TS annotation.
98
195
  // Requiring no space before the colon keeps `cond ? a : b` out.
99
196
  { match: `\\b${ID}(?=\\??:)`, scope: 'property' },
100
197
  { match: `\\b${ID}(?=\\s*=>)`, scope: 'variable.parameter' },
101
- { match: `\\b${ID}(?=[ \\t]*[(<])`, scope: 'function.call' },
198
+ { match: `\\b${ID}(?=[ \\t]*[(<]|\`)`, scope: 'function.call' },
102
199
  {
103
200
  // Bare `=` goes last so `=>`, `==`, `===` and the compound forms all
104
201
  // get first refusal. Without it plain assignment matched nothing.
@@ -109,6 +206,133 @@ const javascript = {
109
206
  { match: '[,;:]', scope: 'punctuation.delimiter' },
110
207
  ],
111
208
  },
209
+ /**
210
+ * Right after `const`, `let` or `var`: the one name it declares. A
211
+ * destructuring pattern or anything else ends the state without a scope.
212
+ */
213
+ 'declared-name': {
214
+ rules: [
215
+ { match: '\\benum\\b', scope: 'keyword.declaration', pop: true },
216
+ { match: `${ID}(?=${ASSIGNS_FUNCTION})`, scope: 'function', pop: true },
217
+ { match: ID, scope: 'variable', pop: true },
218
+ { match: '(?=\\S)', pop: true },
219
+ ],
220
+ },
221
+ // ---- parameter lists -----------------------------------------------------
222
+ /** After the `(` of a parameter list, up to the `)` that closes it. */
223
+ params: {
224
+ rules: [{ match: '\\)', scope: 'punctuation.bracket', pop: true }, { include: 'param-pattern' }],
225
+ },
226
+ /**
227
+ * What a parameter can be: a name, a destructuring pattern, a rest
228
+ * element, and after it a TypeScript annotation or a default value.
229
+ */
230
+ 'param-pattern': {
231
+ rules: [
232
+ { match: '/\\*', scope: 'comment', push: 'block-comment' },
233
+ { match: '//[^\\n]*', scope: 'comment' },
234
+ { match: ':', scope: 'punctuation.delimiter', push: 'param-type' },
235
+ // Leave the operator for expression rules that need it to recognize a regex.
236
+ { match: '(?==(?!>))', push: 'param-default' },
237
+ { match: '\\{', scope: 'punctuation.bracket', push: 'param-object' },
238
+ { match: '\\[', scope: 'punctuation.bracket', push: 'param-array' },
239
+ { match: '\\.{3}|\\?', scope: 'operator' },
240
+ { match: '\\bthis\\b', scope: 'variable.builtin' },
241
+ // A constructor's parameter properties: `constructor(private x: X)`.
242
+ {
243
+ match: '\\b(?:public|private|protected|readonly|override)\\b(?=[ \\t]+[\\w$\\[{])',
244
+ scope: 'keyword.declaration',
245
+ },
246
+ { match: `@${ID}`, scope: 'decorator' },
247
+ { match: ID, scope: 'variable.parameter' },
248
+ { match: ',', scope: 'punctuation.delimiter' },
249
+ ],
250
+ },
251
+ /**
252
+ * `{ a, b: renamed, c = 1, ...rest }`: keys are not bindings. A computed
253
+ * key uses expression rules, unlike the bindings inside an array pattern.
254
+ */
255
+ 'param-object': {
256
+ rules: [
257
+ { match: '\\}', scope: 'punctuation.bracket', pop: true },
258
+ { match: '\\[', scope: 'punctuation.bracket', push: 'bracket' },
259
+ // Inside an object pattern a colon renames a key, never annotates a type.
260
+ { match: ':', scope: 'punctuation.delimiter' },
261
+ { include: 'numbers' },
262
+ // Quoted keys rename a binding; their colon does not introduce a type.
263
+ {
264
+ match: `("(?:[^"\\\\\\r\\n]|\\\\.)*"|'(?:[^'\\\\\\r\\n]|\\\\.)*')([ \\t]*)(:)`,
265
+ scope: ['string', null, 'punctuation.delimiter'],
266
+ },
267
+ {
268
+ match: `(${ID})([ \\t]*)(:)`,
269
+ scope: ['property', null, 'punctuation.delimiter'],
270
+ },
271
+ { include: 'param-pattern' },
272
+ ],
273
+ },
274
+ /** Array destructuring binds its elements, including nested patterns. */
275
+ 'param-array': {
276
+ rules: [{ match: '\\]', scope: 'punctuation.bracket', pop: true }, { include: 'param-pattern' }],
277
+ },
278
+ /**
279
+ * A parameter's type, up to the `,`, `)` or `=` that ends it. Brackets
280
+ * nest so the commas in `Map<K, V>` and `{ a: A, b: B }` stay inside.
281
+ */
282
+ 'param-type': {
283
+ rules: [
284
+ { match: '(?=[,)}\\]]|=(?!>))', pop: true },
285
+ { match: '<', scope: 'punctuation.bracket', push: 'type-angle' },
286
+ { include: 'nesting' },
287
+ { include: 'expression' },
288
+ ],
289
+ },
290
+ 'type-angle': {
291
+ rules: [
292
+ { match: '=>', scope: 'operator' },
293
+ { match: '>', scope: 'punctuation.bracket', pop: true },
294
+ { match: '<', scope: 'punctuation.bracket', push: 'type-angle' },
295
+ { include: 'nesting' },
296
+ { include: 'expression' },
297
+ ],
298
+ },
299
+ /** A default value, up to the `,` or closing bracket that ends it. */
300
+ 'param-default': {
301
+ rules: [{ match: '(?=[,)}\\]])', pop: true }, { include: 'nesting' }, { include: 'expression' }],
302
+ },
303
+ /** Bracket depth, so a comma inside a nested call or literal stays nested. */
304
+ nesting: {
305
+ rules: [
306
+ // Consume the regex with its opener, then track the bracket around it.
307
+ { match: `(\\()(\\s*)(${REGEX})`, scope: ['punctuation.bracket', null, 'regexp'], push: 'paren' },
308
+ { match: `(\\[)(\\s*)(${REGEX})`, scope: ['punctuation.bracket', null, 'regexp'], push: 'bracket' },
309
+ // Generic calls nest commas too. Require a following call and stop at an
310
+ // assignment or an unmatched closing parenthesis or brace. Balanced function/object
311
+ // types are allowed, but the scan cannot escape into the function body.
312
+ {
313
+ match: '<(?=(?:[^;=(){}\\n]|=>|\\([^(){}\\n]{0,128}\\)|\\{[^{}()\\n]{0,128}\\}){1,512}>[ \\t]*\\()',
314
+ scope: 'punctuation.bracket',
315
+ push: 'type-angle',
316
+ },
317
+ { match: '\\(', scope: 'punctuation.bracket', push: 'paren' },
318
+ { match: '\\{', scope: 'punctuation.bracket', push: 'brace' },
319
+ { match: '\\[', scope: 'punctuation.bracket', push: 'bracket' },
320
+ ],
321
+ },
322
+ paren: {
323
+ rules: [
324
+ { match: '\\)', scope: 'punctuation.bracket', pop: true },
325
+ { include: 'nesting' },
326
+ { include: 'expression' },
327
+ ],
328
+ },
329
+ bracket: {
330
+ rules: [
331
+ { match: '\\]', scope: 'punctuation.bracket', pop: true },
332
+ { include: 'nesting' },
333
+ { include: 'expression' },
334
+ ],
335
+ },
112
336
  numbers: {
113
337
  rules: [
114
338
  { match: '\\b0[xX](?:_?[0-9a-fA-F])+n?\\b', scope: 'number' },
@@ -182,38 +406,55 @@ const javascript = {
182
406
  // ---- JSX ---------------------------------------------------------------
183
407
  jsx: {
184
408
  rules: [
185
- // Two guards, because `<` is the most overloaded character in the
186
- // language. The trailing lookahead rules out `a < b` — a comparison
187
- // has a space or a non-tag character where a tag has `>`, `/` or an
188
- // attribute. The lookbehind rules out type arguments: `Handler<E
189
- // extends Event>` and `Array<string>` both satisfy the lookahead, and
190
- // what separates them from JSX is that a tag never follows an
191
- // identifier, `)` or `]` directly.
409
+ // `<` is the most overloaded character in the language, so an element
410
+ // opens only where an expression can start. The lookbehind sits after
411
+ // the `<` so the rule still starts with a literal character, which
412
+ // keeps the engine's first-character scan.
192
413
  {
193
- match: '(?<![\\w$)\\]])(<)(/?)([A-Z][\\w.]*|[a-z][\\w-]*(?:\\.[\\w-]+)*)(?=[\\s/>])',
194
- scope: ['punctuation.bracket', 'punctuation', 'tag'],
195
- push: 'jsx-tag',
414
+ match: `(<)(?<=${BEFORE_JSX}\\s*<)(${TAG})(?=[\\s/>])${NOT_TS_AFTER}`,
415
+ scope: ['punctuation.bracket', 'tag'],
416
+ push: 'jsx-open-tag',
196
417
  },
197
418
  {
198
- match: '(<)(>)',
199
- scope: ['punctuation.bracket', 'punctuation.bracket'],
419
+ match: `<(?<=${BEFORE_JSX}\\s*<)>`,
420
+ scope: 'punctuation.bracket',
421
+ push: 'jsx-children',
200
422
  },
423
+ // A closing tag with no opener, as in a snippet cut out of a larger file.
201
424
  {
202
- match: '(</)(>)',
203
- scope: ['punctuation.bracket', 'punctuation.bracket'],
425
+ match: `(</)(${TAG})?([ \\t]*)(>)`,
426
+ scope: ['punctuation.bracket', 'tag', null, 'punctuation.bracket'],
204
427
  },
205
428
  ],
206
429
  },
207
- 'jsx-tag': {
430
+ 'jsx-open-tag': {
208
431
  rules: [
209
- { match: '/?>', scope: 'punctuation.bracket', pop: true },
432
+ { match: '/>', scope: 'punctuation.bracket', pop: true },
433
+ { match: '>', scope: 'punctuation.bracket', set: 'jsx-children' },
210
434
  { match: '\\{', scope: 'interpolation', push: 'jsx-expression' },
211
- { match: `(${ID}(?:-${ID})*)(?=\\s*=)`, scope: 'tag.attribute' },
212
- { match: `\\b${ID}(?:-${ID})*`, scope: 'tag.attribute' },
435
+ { match: `\\b${ID}(?:[-:]${ID})*`, scope: 'tag.attribute' },
213
436
  { match: '=', scope: 'operator' },
214
437
  { include: 'strings' },
215
438
  ],
216
439
  },
440
+ /** Between an opening tag and its closing tag: text, elements and `{…}`. */
441
+ 'jsx-children': {
442
+ rules: [
443
+ {
444
+ match: `(</)(${TAG})?([ \\t]*)(>)`,
445
+ scope: ['punctuation.bracket', 'tag', null, 'punctuation.bracket'],
446
+ pop: true,
447
+ },
448
+ {
449
+ match: `(<)(${TAG})(?=[\\s/>])`,
450
+ scope: ['punctuation.bracket', 'tag'],
451
+ push: 'jsx-open-tag',
452
+ },
453
+ { match: '<>', scope: 'punctuation.bracket', push: 'jsx-children' },
454
+ { match: '\\{', scope: 'interpolation', push: 'jsx-expression' },
455
+ { match: '&(?:#\\d{1,8}|#x[0-9a-fA-F]{1,8}|[A-Za-z][A-Za-z0-9]{1,32});', scope: 'string.escape' },
456
+ ],
457
+ },
217
458
  'jsx-expression': {
218
459
  rules: [
219
460
  { match: '\\}', scope: 'interpolation', pop: true },
@@ -221,6 +462,14 @@ const javascript = {
221
462
  { include: 'expression' },
222
463
  ],
223
464
  },
465
+ 'block-comment': {
466
+ default: 'comment',
467
+ rules: [{ match: '\\*/', scope: 'comment', pop: true }],
468
+ },
469
+ 'doc-comment': {
470
+ default: 'comment.doc',
471
+ rules: [{ match: '\\*/', scope: 'comment.doc', pop: true }],
472
+ },
224
473
  },
225
474
  };
226
475
  export default javascript;
@@ -1 +1 @@
1
- {"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../src/langs/json.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAE5C;;;;;GAKG;AACH,QAAA,MAAM,IAAI,EAAE,OAwCX,CAAA;AAED,eAAe,IAAI,CAAA"}
1
+ {"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../src/langs/json.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAE5C;;;;;GAKG;AACH,QAAA,MAAM,IAAI,EAAE,OA8CX,CAAA;AAED,eAAe,IAAI,CAAA"}