-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Depends on #3115 Implements RFC at graphql/graphql-spec#849. * Replaces `isSourceCharacter` with `isUnicodeScalarValue` * Adds `isSupplementaryCodePoint`, used in String, BlockStrings, and Comments to ensure correct lexing of JavaScript's UTF-16 source. * Updates `printCodePointAt` to correctly print supplementary code points. * Adds variable-width Unicode escape sequences * Adds explicit support for legacy JSON-style fixed-width Unicode escape sequence surrogate pairs. * Adds `printString` to no longer rely on `JSON.stringify`. Borrows some implementation details from Node.js internals for string printing. Implements: > When producing a {StringValue}, implementations should use escape sequences to > represent non-printable control characters (U+0000 to U+001F and U+007F to > U+009F). Other escape sequences are not necessary, however an implementation may > use escape sequences to represent any other range of code points. Closes #2449 Co-authored-by: Andreas Marek <andimarek@fastmail.fm>
- Loading branch information
Showing
5 changed files
with
453 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { expect } from 'chai'; | ||
import { describe, it } from 'mocha'; | ||
|
||
import { printString } from '../printString'; | ||
|
||
describe('printString', () => { | ||
it('prints a simple string', () => { | ||
expect(printString('hello world')).to.equal('"hello world"'); | ||
}); | ||
|
||
it('escapes quotes', () => { | ||
expect(printString('"hello world"')).to.equal('"\\"hello world\\""'); | ||
}); | ||
|
||
it('does not escape single quote', () => { | ||
expect(printString("who's test")).to.equal('"who\'s test"'); | ||
}); | ||
|
||
it('escapes backslashes', () => { | ||
expect(printString('escape: \\n')).to.equal('"escape: \\\\n"'); | ||
}); | ||
|
||
it('escapes well-known control chars', () => { | ||
expect(printString('\b\f\n\r\t')).to.equal('"\\b\\f\\n\\r\\t"'); | ||
}); | ||
|
||
it('escapes zero byte', () => { | ||
expect(printString('\x00')).to.equal('"\\u0000"'); | ||
}); | ||
|
||
it('does not escape space', () => { | ||
expect(printString(' ')).to.equal('" "'); | ||
}); | ||
|
||
it('escapes all other control chars', () => { | ||
for (let i = 1; i <= 0x9f; i++) { | ||
const source = String.fromCharCode(i); | ||
if (/[\b\f\n\r\t]/.test(source) || (i >= 0x0020 && i <= 0x007e)) { | ||
continue; | ||
} | ||
expect(printString(source)).to.equal( | ||
`"\\u00${i <= 0x000f ? '0' : ''}${i.toString(16).toUpperCase()}"`, | ||
); | ||
} | ||
}); | ||
|
||
it('does not escape non-ascii character', () => { | ||
expect(printString('\u21BB')).to.equal('"\u21BB"'); | ||
}); | ||
|
||
it('does not escape supplementary character', () => { | ||
expect(printString('\u{1f600}')).to.equal('"\u{1f600}"'); | ||
}); | ||
}); |
Oops, something went wrong.