Skip to content

Commit 08a9d12

Browse files
authored
chore: use codePointAt instead of implementing it ourselves (#14923)
seems to be some sort of codePointAt polyfill from ancient times before that was available on String.prototype
1 parent 8201d7a commit 08a9d12

File tree

4 files changed

+41
-73
lines changed

4 files changed

+41
-73
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import { isIdentifierStart, isIdentifierChar } from 'acorn';
44
import fragment from './state/fragment.js';
55
import { regex_whitespace } from '../patterns.js';
6-
import full_char_code_at from './utils/full_char_code_at.js';
76
import * as e from '../../errors.js';
87
import { create_fragment } from './utils/create.js';
98
import read_options from './read/options.js';
@@ -230,13 +229,13 @@ export class Parser {
230229

231230
let i = this.index;
232231

233-
const code = full_char_code_at(this.template, i);
232+
const code = /** @type {number} */ (this.template.codePointAt(i));
234233
if (!isIdentifierStart(code, true)) return null;
235234

236235
i += code <= 0xffff ? 1 : 2;
237236

238237
while (i < this.template.length) {
239-
const code = full_char_code_at(this.template, i);
238+
const code = /** @type {number} */ (this.template.codePointAt(i));
240239

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

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

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
/** @import { Location } from 'locate-character' */
22
/** @import { Pattern } from 'estree' */
33
/** @import { Parser } from '../index.js' */
4-
// @ts-expect-error acorn type definitions are borked in the release we use
5-
import { isIdentifierStart } from 'acorn';
6-
import full_char_code_at from '../utils/full_char_code_at.js';
7-
import {
8-
is_bracket_open,
9-
is_bracket_close,
10-
is_bracket_pair,
11-
get_bracket_close
12-
} from '../utils/bracket.js';
4+
import { is_bracket_open, is_bracket_close, get_bracket_close } from '../utils/bracket.js';
135
import { parse_expression_at } from '../acorn.js';
146
import { regex_not_newline_characters } from '../../patterns.js';
157
import * as e from '../../../errors.js';
@@ -23,9 +15,9 @@ export default function read_pattern(parser) {
2315
const start = parser.index;
2416
let i = parser.index;
2517

26-
const code = full_char_code_at(parser.template, i);
27-
if (isIdentifierStart(code, true)) {
28-
const name = /** @type {string} */ (parser.read_identifier());
18+
const name = parser.read_identifier();
19+
20+
if (name !== null) {
2921
const annotation = read_type_annotation(parser);
3022

3123
return {
@@ -41,28 +33,32 @@ export default function read_pattern(parser) {
4133
};
4234
}
4335

44-
if (!is_bracket_open(code)) {
36+
if (!is_bracket_open(parser.template[i])) {
4537
e.expected_pattern(i);
4638
}
4739

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

5143
while (i < parser.template.length) {
52-
const code = full_char_code_at(parser.template, i);
53-
if (is_bracket_open(code)) {
54-
bracket_stack.push(code);
55-
} else if (is_bracket_close(code)) {
56-
const popped = /** @type {number} */ (bracket_stack.pop());
57-
if (!is_bracket_pair(popped, code)) {
58-
e.expected_token(i, String.fromCharCode(/** @type {number} */ (get_bracket_close(popped))));
44+
const char = parser.template[i];
45+
46+
if (is_bracket_open(char)) {
47+
bracket_stack.push(char);
48+
} else if (is_bracket_close(char)) {
49+
const popped = /** @type {string} */ (bracket_stack.pop());
50+
const expected = /** @type {string} */ (get_bracket_close(popped));
51+
52+
if (char !== expected) {
53+
e.expected_token(i, expected);
5954
}
55+
6056
if (bracket_stack.length === 0) {
61-
i += code <= 0xffff ? 1 : 2;
57+
i += 1;
6258
break;
6359
}
6460
}
65-
i += code <= 0xffff ? 1 : 2;
61+
i += 1;
6662
}
6763

6864
parser.index = i;

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

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,30 @@
1-
import full_char_code_at from './full_char_code_at.js';
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 = ')';
27

3-
const SQUARE_BRACKET_OPEN = '['.charCodeAt(0);
4-
const SQUARE_BRACKET_CLOSE = ']'.charCodeAt(0);
5-
const CURLY_BRACKET_OPEN = '{'.charCodeAt(0);
6-
const CURLY_BRACKET_CLOSE = '}'.charCodeAt(0);
7-
const PARENTHESES_OPEN = '('.charCodeAt(0);
8-
const PARENTHESES_CLOSE = ')'.charCodeAt(0);
9-
10-
/** @param {number} code */
11-
export function is_bracket_open(code) {
12-
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;
1311
}
1412

15-
/** @param {number} code */
16-
export function is_bracket_close(code) {
17-
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;
1816
}
1917

20-
/**
21-
* @param {number} open
22-
* @param {number} close
23-
*/
24-
export function is_bracket_pair(open, close) {
25-
return (
26-
(open === SQUARE_BRACKET_OPEN && close === SQUARE_BRACKET_CLOSE) ||
27-
(open === CURLY_BRACKET_OPEN && close === CURLY_BRACKET_CLOSE)
28-
);
29-
}
30-
31-
/** @param {number} open */
18+
/** @param {string} open */
3219
export function get_bracket_close(open) {
3320
if (open === SQUARE_BRACKET_OPEN) {
3421
return SQUARE_BRACKET_CLOSE;
3522
}
23+
3624
if (open === CURLY_BRACKET_OPEN) {
3725
return CURLY_BRACKET_CLOSE;
3826
}
27+
3928
if (open === PARENTHESES_OPEN) {
4029
return PARENTHESES_CLOSE;
4130
}
@@ -132,8 +121,7 @@ function count_leading_backslashes(string, search_start_index) {
132121
* @returns {number | undefined} The index of the closing bracket, or undefined if not found.
133122
*/
134123
export function find_matching_bracket(template, index, open) {
135-
const open_code = full_char_code_at(open, 0);
136-
const close_code = get_bracket_close(open_code);
124+
const close = get_bracket_close(open);
137125
let brackets = 1;
138126
let i = index;
139127
while (brackets > 0 && i < template.length) {
@@ -159,10 +147,10 @@ export function find_matching_bracket(template, index, open) {
159147
continue;
160148
}
161149
default: {
162-
const code = full_char_code_at(template, i);
163-
if (code === open_code) {
150+
const char = template[i];
151+
if (char === open) {
164152
brackets++;
165-
} else if (code === close_code) {
153+
} else if (char === close) {
166154
brackets--;
167155
}
168156
if (brackets === 0) {

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

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)