Skip to content

Commit 6914cc9

Browse files
committed
chore: use codePointAt instead of implementing it ourselves
1 parent fb67bad commit 6914cc9

File tree

4 files changed

+13
-25
lines changed

4 files changed

+13
-25
lines changed

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

Lines changed: 7 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';
@@ -214,6 +213,11 @@ export class Parser {
214213
}
215214
}
216215

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

231235
let i = this.index;
232236

233-
const code = full_char_code_at(this.template, i);
237+
const code = this.code_point_at(i);
234238
if (!isIdentifierStart(code, true)) return null;
235239

236240
i += code <= 0xffff ? 1 : 2;
237241

238242
while (i < this.template.length) {
239-
const code = full_char_code_at(this.template, i);
243+
const code = this.code_point_at(i);
240244

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

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
/** @import { Parser } from '../index.js' */
44
// @ts-expect-error acorn type definitions are borked in the release we use
55
import { isIdentifierStart } from 'acorn';
6-
import full_char_code_at from '../utils/full_char_code_at.js';
76
import {
87
is_bracket_open,
98
is_bracket_close,
@@ -23,7 +22,8 @@ export default function read_pattern(parser) {
2322
const start = parser.index;
2423
let i = parser.index;
2524

26-
const code = full_char_code_at(parser.template, i);
25+
const code = parser.code_point_at(i);
26+
2727
if (isIdentifierStart(code, true)) {
2828
const name = /** @type {string} */ (parser.read_identifier());
2929
const annotation = read_type_annotation(parser);
@@ -49,7 +49,8 @@ export default function read_pattern(parser) {
4949
i += code <= 0xffff ? 1 : 2;
5050

5151
while (i < parser.template.length) {
52-
const code = full_char_code_at(parser.template, i);
52+
const code = parser.code_point_at(i);
53+
5354
if (is_bracket_open(code)) {
5455
bracket_stack.push(code);
5556
} else if (is_bracket_close(code)) {

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import full_char_code_at from './full_char_code_at.js';
2-
31
const SQUARE_BRACKET_OPEN = '['.charCodeAt(0);
42
const SQUARE_BRACKET_CLOSE = ']'.charCodeAt(0);
53
const CURLY_BRACKET_OPEN = '{'.charCodeAt(0);
@@ -132,7 +130,7 @@ function count_leading_backslashes(string, search_start_index) {
132130
* @returns {number | undefined} The index of the closing bracket, or undefined if not found.
133131
*/
134132
export function find_matching_bracket(template, index, open) {
135-
const open_code = full_char_code_at(open, 0);
133+
const open_code = open.charCodeAt(0);
136134
const close_code = get_bracket_close(open_code);
137135
let brackets = 1;
138136
let i = index;
@@ -159,7 +157,7 @@ export function find_matching_bracket(template, index, open) {
159157
continue;
160158
}
161159
default: {
162-
const code = full_char_code_at(template, i);
160+
const code = template.codePointAt(i);
163161
if (code === open_code) {
164162
brackets++;
165163
} else if (code === close_code) {

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)