Skip to content

Commit

Permalink
chore: use codePointAt instead of implementing it ourselves
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Jan 7, 2025
1 parent fb67bad commit 6914cc9
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 25 deletions.
10 changes: 7 additions & 3 deletions packages/svelte/src/compiler/phases/1-parse/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import { isIdentifierStart, isIdentifierChar } from 'acorn';
import fragment from './state/fragment.js';
import { regex_whitespace } from '../patterns.js';
import full_char_code_at from './utils/full_char_code_at.js';
import * as e from '../../errors.js';
import { create_fragment } from './utils/create.js';
import read_options from './read/options.js';
Expand Down Expand Up @@ -214,6 +213,11 @@ export class Parser {
}
}

/** @param {number} i */
code_point_at(i) {
return /** @type {number} */ (this.template.codePointAt(i));
}

/**
* Search for a regex starting at the current index and return the result if it matches
* @param {RegExp} pattern Should have a ^ anchor at the start so the regex doesn't search past the beginning, resulting in worse performance
Expand All @@ -230,13 +234,13 @@ export class Parser {

let i = this.index;

const code = full_char_code_at(this.template, i);
const code = this.code_point_at(i);
if (!isIdentifierStart(code, true)) return null;

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

while (i < this.template.length) {
const code = full_char_code_at(this.template, i);
const code = this.code_point_at(i);

if (!isIdentifierChar(code, true)) break;
i += code <= 0xffff ? 1 : 2;
Expand Down
7 changes: 4 additions & 3 deletions packages/svelte/src/compiler/phases/1-parse/read/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
/** @import { Parser } from '../index.js' */
// @ts-expect-error acorn type definitions are borked in the release we use
import { isIdentifierStart } from 'acorn';
import full_char_code_at from '../utils/full_char_code_at.js';
import {
is_bracket_open,
is_bracket_close,
Expand All @@ -23,7 +22,8 @@ export default function read_pattern(parser) {
const start = parser.index;
let i = parser.index;

const code = full_char_code_at(parser.template, i);
const code = parser.code_point_at(i);

if (isIdentifierStart(code, true)) {
const name = /** @type {string} */ (parser.read_identifier());
const annotation = read_type_annotation(parser);
Expand All @@ -49,7 +49,8 @@ export default function read_pattern(parser) {
i += code <= 0xffff ? 1 : 2;

while (i < parser.template.length) {
const code = full_char_code_at(parser.template, i);
const code = parser.code_point_at(i);

if (is_bracket_open(code)) {
bracket_stack.push(code);
} else if (is_bracket_close(code)) {
Expand Down
6 changes: 2 additions & 4 deletions packages/svelte/src/compiler/phases/1-parse/utils/bracket.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import full_char_code_at from './full_char_code_at.js';

const SQUARE_BRACKET_OPEN = '['.charCodeAt(0);
const SQUARE_BRACKET_CLOSE = ']'.charCodeAt(0);
const CURLY_BRACKET_OPEN = '{'.charCodeAt(0);
Expand Down Expand Up @@ -132,7 +130,7 @@ function count_leading_backslashes(string, search_start_index) {
* @returns {number | undefined} The index of the closing bracket, or undefined if not found.
*/
export function find_matching_bracket(template, index, open) {
const open_code = full_char_code_at(open, 0);
const open_code = open.charCodeAt(0);
const close_code = get_bracket_close(open_code);
let brackets = 1;
let i = index;
Expand All @@ -159,7 +157,7 @@ export function find_matching_bracket(template, index, open) {
continue;
}
default: {
const code = full_char_code_at(template, i);
const code = template.codePointAt(i);
if (code === open_code) {
brackets++;
} else if (code === close_code) {
Expand Down

This file was deleted.

0 comments on commit 6914cc9

Please sign in to comment.