Skip to content

Commit 20c2a97

Browse files
committed
fix(pass-style)!: only well-formed strings are passable
1 parent 5b2537c commit 20c2a97

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

packages/pass-style/src/passStyleOf.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,28 @@ const { details: X, Fail, quote: q } = assert;
2626
const { ownKeys } = Reflect;
2727
const { isFrozen } = Object;
2828

29+
// @ts-expect-error TS does not yet know about `isWellFormed`
30+
const hasWellFormedMethod = !!String.prototype.isWellFormed;
31+
32+
const isWellFormed = hasWellFormedMethod
33+
? // @ts-expect-error TS does not yet know about `isWellFormed`
34+
str => typeof str === 'string' && str.isWellFormed()
35+
: str => {
36+
if (typeof str !== 'string') {
37+
return false;
38+
}
39+
for (const ch of str) {
40+
const cp = /** @type {number} */ (ch.codePointAt(0));
41+
if (cp >= 0xd800 && cp <= 0xdfff) {
42+
return false;
43+
}
44+
}
45+
return true;
46+
};
47+
48+
const assertWellFormed = str =>
49+
isWellFormed(str) || Fail`Expected well-formed unicode string: ${str}`;
50+
2951
/**
3052
* @param {PassStyleHelper[]} passStyleHelpers
3153
* @returns {Record<HelperPassStyle, PassStyleHelper> }
@@ -124,12 +146,15 @@ const makePassStyleOf = passStyleHelpers => {
124146
const typestr = typeof inner;
125147
switch (typestr) {
126148
case 'undefined':
127-
case 'string':
128149
case 'boolean':
129150
case 'number':
130151
case 'bigint': {
131152
return typestr;
132153
}
154+
case 'string': {
155+
assertWellFormed(inner);
156+
return 'string';
157+
}
133158
case 'symbol': {
134159
assertPassableSymbol(inner);
135160
return 'symbol';

0 commit comments

Comments
 (0)