Files
pnpm/object/property-path/test/token/StringLiteral.test.ts
Zoltan Kochan 1c8c4e49f5 style: add eslint-plugin-simple-import-sort (#10947)
Add eslint-plugin-simple-import-sort to enforce consistent import ordering:
- Node.js builtins first
- External packages second
- Relative imports last
- Named imports sorted alphabetically within each statement
2026-03-13 02:02:38 +01:00

86 lines
2.8 KiB
TypeScript

import { parseStringLiteral, type StringLiteral } from '../../src/index.js'
test('not a string literal', () => {
expect(parseStringLiteral('')).toBeUndefined()
expect(parseStringLiteral('not a string')).toBeUndefined()
expect(parseStringLiteral('not a string again "this string would be ignored"')).toBeUndefined()
expect(parseStringLiteral('0123')).toBeUndefined()
})
test('simple string literal', () => {
expect(parseStringLiteral('""')).toStrictEqual([{
type: 'string-literal',
quote: '"',
content: '',
} as StringLiteral, ''])
expect(parseStringLiteral("''")).toStrictEqual([{
type: 'string-literal',
quote: "'",
content: '',
} as StringLiteral, ''])
expect(parseStringLiteral('"hello world"')).toStrictEqual([{
type: 'string-literal',
quote: '"',
content: 'hello world',
} as StringLiteral, ''])
expect(parseStringLiteral("'hello world'")).toStrictEqual([{
type: 'string-literal',
quote: "'",
content: 'hello world',
} as StringLiteral, ''])
expect(parseStringLiteral('"hello world".length')).toStrictEqual([{
type: 'string-literal',
quote: '"',
content: 'hello world',
} as StringLiteral, '.length'])
expect(parseStringLiteral("'hello world'.length")).toStrictEqual([{
type: 'string-literal',
quote: "'",
content: 'hello world',
} as StringLiteral, '.length'])
})
test('escape sequences', () => {
expect(parseStringLiteral('"hello \\"world\\"".length')).toStrictEqual([{
type: 'string-literal',
quote: '"',
content: 'hello "world"',
} as StringLiteral, '.length'])
expect(parseStringLiteral('"hello\\nworld".length')).toStrictEqual([{
type: 'string-literal',
quote: '"',
content: 'hello\nworld',
} as StringLiteral, '.length'])
expect(parseStringLiteral('"C:\\\\hello\\\\world\\\\".length')).toStrictEqual([{
type: 'string-literal',
quote: '"',
content: 'C:\\hello\\world\\',
} as StringLiteral, '.length'])
})
test('unsupported escape sequences', () => {
expect(() => parseStringLiteral('"hello \\x22world\\x22"')).toThrow(expect.objectContaining({
code: 'ERR_PNPM_UNSUPPORTED_STRING_LITERAL_ESCAPE_SEQUENCE',
sequence: 'x',
}))
})
test('no closing quote', () => {
expect(() => parseStringLiteral('"hello world')).toThrow(expect.objectContaining({
code: 'ERR_PNPM_INCOMPLETE_STRING_LITERAL',
expectedQuote: '"',
}))
expect(() => parseStringLiteral("'hello world")).toThrow(expect.objectContaining({
code: 'ERR_PNPM_INCOMPLETE_STRING_LITERAL',
expectedQuote: "'",
}))
expect(() => parseStringLiteral('"hello world\\"')).toThrow(expect.objectContaining({
code: 'ERR_PNPM_INCOMPLETE_STRING_LITERAL',
expectedQuote: '"',
}))
expect(() => parseStringLiteral("'hello world\\'")).toThrow(expect.objectContaining({
code: 'ERR_PNPM_INCOMPLETE_STRING_LITERAL',
expectedQuote: "'",
}))
})