From 08be12bc74f54d42852b5a01222c324aa29a1bea Mon Sep 17 00:00:00 2001 From: Adrian Heine Date: Tue, 16 Oct 2018 20:40:02 +0200 Subject: [PATCH] Transpile U+2028 and U+2029 (json-superset) --- src/program/types/Literal.js | 11 +++++++++++ test/samples/json-superset.js | 12 ++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 test/samples/json-superset.js diff --git a/src/program/types/Literal.js b/src/program/types/Literal.js index 379f9a81..8be87946 100644 --- a/src/program/types/Literal.js +++ b/src/program/types/Literal.js @@ -2,6 +2,8 @@ import Node from '../Node.js'; import CompileError from '../../utils/CompileError.js'; import rewritePattern from 'regexpu-core'; +const nonAsciiLsOrPs = /[\u2028-\u2029]/g; + export default class Literal extends Node { initialise() { if (typeof this.value === 'string') { @@ -37,6 +39,15 @@ export default class Literal extends Node { } ); } + } else if (typeof this.value === "string" && this.value.match(nonAsciiLsOrPs)) { + code.overwrite( + this.start, + this.end, + this.raw.replace(nonAsciiLsOrPs, m => m == '\u2028' ? '\\u2028' : '\\u2029'), + { + contentOnly: true + } + ); } } } diff --git a/test/samples/json-superset.js b/test/samples/json-superset.js new file mode 100644 index 00000000..51a31631 --- /dev/null +++ b/test/samples/json-superset.js @@ -0,0 +1,12 @@ +module.exports = [ + { + description: 'transpiles U+2028 LINE SEPARATOR', + input: `const x = "a\u2028b\u1010"`, + output: `var x = "a\\u2028b\u1010"` + }, + { + description: 'transpiles U+2029 PARAGRAPH SEPARATOR', + input: `const x = "a\u2029b\u1010"`, + output: `var x = "a\\u2029b\u1010"` + } +]