Skip to content

Commit a930f2c

Browse files
committed
simplify further
1 parent 6914cc9 commit a930f2c

File tree

3 files changed

+38
-44
lines changed

3 files changed

+38
-44
lines changed

packages/svelte/src/compiler/phases/1-parse/index.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,6 @@ export class Parser {
213213
}
214214
}
215215

216-
/** @param {number} i */
217-
code_point_at(i) {
218-
return /** @type {number} */ (this.template.codePointAt(i));
219-
}
220-
221216
/**
222217
* Search for a regex starting at the current index and return the result if it matches
223218
* @param {RegExp} pattern Should have a ^ anchor at the start so the regex doesn't search past the beginning, resulting in worse performance
@@ -234,13 +229,13 @@ export class Parser {
234229

235230
let i = this.index;
236231

237-
const code = this.code_point_at(i);
232+
const code = /** @type {number} */ (this.template.codePointAt(i));
238233
if (!isIdentifierStart(code, true)) return null;
239234

240235
i += code <= 0xffff ? 1 : 2;
241236

242237
while (i < this.template.length) {
243-
const code = this.code_point_at(i);
238+
const code = /** @type {number} */ (this.template.codePointAt(i));
244239

245240
if (!isIdentifierChar(code, true)) break;
246241
i += code <= 0xffff ? 1 : 2;

packages/svelte/src/compiler/phases/1-parse/read/context.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
/** @import { Pattern } from 'estree' */
33
/** @import { Parser } from '../index.js' */
44
// @ts-expect-error acorn type definitions are borked in the release we use
5-
import { isIdentifierStart } from 'acorn';
65
import {
76
is_bracket_open,
87
is_bracket_close,
@@ -22,10 +21,9 @@ export default function read_pattern(parser) {
2221
const start = parser.index;
2322
let i = parser.index;
2423

25-
const code = parser.code_point_at(i);
24+
const name = parser.read_identifier();
2625

27-
if (isIdentifierStart(code, true)) {
28-
const name = /** @type {string} */ (parser.read_identifier());
26+
if (name !== null) {
2927
const annotation = read_type_annotation(parser);
3028

3129
return {
@@ -41,29 +39,29 @@ export default function read_pattern(parser) {
4139
};
4240
}
4341

44-
if (!is_bracket_open(code)) {
42+
if (!is_bracket_open(parser.template[i])) {
4543
e.expected_pattern(i);
4644
}
4745

48-
const bracket_stack = [code];
49-
i += code <= 0xffff ? 1 : 2;
46+
/** @type {string[]} */
47+
const bracket_stack = [];
5048

5149
while (i < parser.template.length) {
52-
const code = parser.code_point_at(i);
53-
54-
if (is_bracket_open(code)) {
55-
bracket_stack.push(code);
56-
} else if (is_bracket_close(code)) {
57-
const popped = /** @type {number} */ (bracket_stack.pop());
58-
if (!is_bracket_pair(popped, code)) {
59-
e.expected_token(i, String.fromCharCode(/** @type {number} */ (get_bracket_close(popped))));
50+
const char = parser.template[i];
51+
52+
if (is_bracket_open(char)) {
53+
bracket_stack.push(char);
54+
} else if (is_bracket_close(char)) {
55+
const popped = /** @type {string} */ (bracket_stack.pop());
56+
if (!is_bracket_pair(popped, char)) {
57+
e.expected_token(i, /** @type {string} */ (get_bracket_close(popped)));
6058
}
6159
if (bracket_stack.length === 0) {
62-
i += code <= 0xffff ? 1 : 2;
60+
i += 1;
6361
break;
6462
}
6563
}
66-
i += code <= 0xffff ? 1 : 2;
64+
i += 1;
6765
}
6866

6967
parser.index = i;

packages/svelte/src/compiler/phases/1-parse/utils/bracket.js

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
const SQUARE_BRACKET_OPEN = '['.charCodeAt(0);
2-
const SQUARE_BRACKET_CLOSE = ']'.charCodeAt(0);
3-
const CURLY_BRACKET_OPEN = '{'.charCodeAt(0);
4-
const CURLY_BRACKET_CLOSE = '}'.charCodeAt(0);
5-
const PARENTHESES_OPEN = '('.charCodeAt(0);
6-
const PARENTHESES_CLOSE = ')'.charCodeAt(0);
1+
const SQUARE_BRACKET_OPEN = '[';
2+
const SQUARE_BRACKET_CLOSE = ']';
3+
const CURLY_BRACKET_OPEN = '{';
4+
const CURLY_BRACKET_CLOSE = '}';
5+
const PARENTHESES_OPEN = '(';
6+
const PARENTHESES_CLOSE = ')';
77

8-
/** @param {number} code */
9-
export function is_bracket_open(code) {
10-
return code === SQUARE_BRACKET_OPEN || code === CURLY_BRACKET_OPEN;
8+
/** @param {string} char */
9+
export function is_bracket_open(char) {
10+
return char === SQUARE_BRACKET_OPEN || char === CURLY_BRACKET_OPEN;
1111
}
1212

13-
/** @param {number} code */
14-
export function is_bracket_close(code) {
15-
return code === SQUARE_BRACKET_CLOSE || code === CURLY_BRACKET_CLOSE;
13+
/** @param {string} char */
14+
export function is_bracket_close(char) {
15+
return char === SQUARE_BRACKET_CLOSE || char === CURLY_BRACKET_CLOSE;
1616
}
1717

1818
/**
19-
* @param {number} open
20-
* @param {number} close
19+
* @param {string} open
20+
* @param {string} close
2121
*/
2222
export function is_bracket_pair(open, close) {
2323
return (
@@ -26,14 +26,16 @@ export function is_bracket_pair(open, close) {
2626
);
2727
}
2828

29-
/** @param {number} open */
29+
/** @param {string} open */
3030
export function get_bracket_close(open) {
3131
if (open === SQUARE_BRACKET_OPEN) {
3232
return SQUARE_BRACKET_CLOSE;
3333
}
34+
3435
if (open === CURLY_BRACKET_OPEN) {
3536
return CURLY_BRACKET_CLOSE;
3637
}
38+
3739
if (open === PARENTHESES_OPEN) {
3840
return PARENTHESES_CLOSE;
3941
}
@@ -130,8 +132,7 @@ function count_leading_backslashes(string, search_start_index) {
130132
* @returns {number | undefined} The index of the closing bracket, or undefined if not found.
131133
*/
132134
export function find_matching_bracket(template, index, open) {
133-
const open_code = open.charCodeAt(0);
134-
const close_code = get_bracket_close(open_code);
135+
const close = get_bracket_close(open);
135136
let brackets = 1;
136137
let i = index;
137138
while (brackets > 0 && i < template.length) {
@@ -157,10 +158,10 @@ export function find_matching_bracket(template, index, open) {
157158
continue;
158159
}
159160
default: {
160-
const code = template.codePointAt(i);
161-
if (code === open_code) {
161+
const char = template[i];
162+
if (char === open) {
162163
brackets++;
163-
} else if (code === close_code) {
164+
} else if (char === close) {
164165
brackets--;
165166
}
166167
if (brackets === 0) {

0 commit comments

Comments
 (0)