oxc/napi/parser/test/magic_string.test.ts
Boshen 4bd3d103d3 feat(napi/parser): introduce experimental MagicString (#7529)
Hold magic string instance on the Rust side for utf8 string manipulation.
2024-12-10 08:33:47 +00:00

26 lines
915 B
TypeScript

import { describe, expect, it } from 'vitest';
import type { StringLiteral, VariableDeclaration } from '../index.js';
import { parseSync } from '../index.js';
describe('simple', () => {
const code = 'const s: String = "测试"';
it('calls magic string APIs', () => {
// `oxc` holds a magic string instance on the Rust side.
const { program, magicString: ms } = parseSync('test.ts', code);
const declaration = program.body[0] as VariableDeclaration;
const stringLiteral = declaration.declarations[0].init as StringLiteral;
// These spans are in utf8 offsets.
const start = stringLiteral.start + 1;
const end = stringLiteral.end - 1;
// Access source text by utf8 offset.
expect(ms.getSourceText(start, end)).toEqual('测试');
// Magic string manipulation.
ms.remove(start, end).append(';');
expect(ms.toString()).toEqual('const s: String = "";');
});
});