From 2a39a1d686ca47ee4465c69ec765a9cdbdfb8af7 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Sun, 14 Jun 2020 18:36:34 -0700 Subject: [PATCH] support backslash escaping, retain exact newline escaping --- src/utils/escapeId.ts | 15 +++++---------- test/form/samples/quote-id/_config.js | 13 +++++++++++-- test/form/samples/quote-id/_expected/amd.js | 6 ++++-- test/form/samples/quote-id/_expected/cjs.js | 9 ++++++--- test/form/samples/quote-id/_expected/es.js | 9 ++++++--- test/form/samples/quote-id/_expected/iife.js | 6 +++--- test/form/samples/quote-id/_expected/system.js | 10 +++++++--- test/form/samples/quote-id/_expected/umd.js | 14 +++++++++----- test/form/samples/quote-id/main.js | 3 ++- 9 files changed, 53 insertions(+), 32 deletions(-) diff --git a/src/utils/escapeId.ts b/src/utils/escapeId.ts index da413d41c95..45384811a9d 100644 --- a/src/utils/escapeId.ts +++ b/src/utils/escapeId.ts @@ -1,12 +1,7 @@ -const quoteNewlineRegEx = /['\r\n\u2028\u2029]/g; -const replacements = { - '\n': '\\n', - '\r': '\\r', - "'": "\\'", - '\u2028': '\\u2028', - '\u2029': '\\u2029' -}; - +const needsEscapeRegEx = /[\\'\r\n\u2028\u2029]/; +const quoteNewlineRegEx = /(['\r\n\u2028\u2029])/g; +const backSlashRegEx = /\\/g; export function escapeId(id: string) { - return id.replace(quoteNewlineRegEx, match => replacements[match as keyof typeof replacements]); + if (!id.match(needsEscapeRegEx)) return id; + return id.replace(backSlashRegEx, '\\\\').replace(quoteNewlineRegEx, '\\$1'); } diff --git a/test/form/samples/quote-id/_config.js b/test/form/samples/quote-id/_config.js index 3dfa80c9996..e46f5b0a7d9 100644 --- a/test/form/samples/quote-id/_config.js +++ b/test/form/samples/quote-id/_config.js @@ -2,15 +2,21 @@ const path = require('path'); const external1 = "quoted'\r\n\u2028\u2029external1"; const external2 = path.join(__dirname, "quoted'\r\n\u2028\u2029external2"); +const external3 = 'C:\\File\\Path.js'; module.exports = { - description: 'supports quote characters in external ids', + description: 'handles escaping for external ids', options: { output: { + paths: id => { + if (id.startsWith('C:')) return id; + return path.relative(__dirname, id); + }, name: 'Q', globals: { [external1]: 'quotedExternal1', - [external2]: 'quotedExternal2' + [external2]: 'quotedExternal2', + [external3]: 'quotedExternal3' } }, plugins: [ @@ -22,6 +28,9 @@ module.exports = { if (id === 'external2') { return { id: external2, external: true }; } + if (id === 'external3') { + return { id: external3, external: true }; + } } } ] diff --git a/test/form/samples/quote-id/_expected/amd.js b/test/form/samples/quote-id/_expected/amd.js index b908f89dd23..2e1243729a0 100644 --- a/test/form/samples/quote-id/_expected/amd.js +++ b/test/form/samples/quote-id/_expected/amd.js @@ -1,5 +1,7 @@ -define(['quoted\'\r\n\u2028\u2029external1', './quoted\'\r\n\u2028\u2029external2'], function (quoted_____external1, quoted_____external2) { 'use strict'; +define(['quoted\'\ \ +\
\
external1', 'quoted\'\ \ +\
\
external2', 'C:\\File\\Path.js'], function (quoted_____external1, quoted_____external2, Path_js) { 'use strict'; - console.log(quoted_____external1.foo, quoted_____external2.bar); + console.log(quoted_____external1.foo, quoted_____external2.bar, Path_js.baz); }); diff --git a/test/form/samples/quote-id/_expected/cjs.js b/test/form/samples/quote-id/_expected/cjs.js index 28ce92f286d..be514723275 100644 --- a/test/form/samples/quote-id/_expected/cjs.js +++ b/test/form/samples/quote-id/_expected/cjs.js @@ -1,6 +1,9 @@ 'use strict'; -var quoted_____external1 = require('quoted\'\r\n\u2028\u2029external1'); -var quoted_____external2 = require('./quoted\'\r\n\u2028\u2029external2'); +var quoted_____external1 = require('quoted\'\ \ +\
\
external1'); +var quoted_____external2 = require('quoted\'\ \ +\
\
external2'); +var Path_js = require('C:\\File\\Path.js'); -console.log(quoted_____external1.foo, quoted_____external2.bar); +console.log(quoted_____external1.foo, quoted_____external2.bar, Path_js.baz); diff --git a/test/form/samples/quote-id/_expected/es.js b/test/form/samples/quote-id/_expected/es.js index 7d54a160fef..cb0fe35580d 100644 --- a/test/form/samples/quote-id/_expected/es.js +++ b/test/form/samples/quote-id/_expected/es.js @@ -1,4 +1,7 @@ -import { foo } from 'quoted\'\r\n\u2028\u2029external1'; -import { bar } from './quoted\'\r\n\u2028\u2029external2'; +import { foo } from 'quoted\'\ \ +\
\
external1'; +import { bar } from 'quoted\'\ \ +\
\
external2'; +import { baz } from 'C:\\File\\Path.js'; -console.log(foo, bar); +console.log(foo, bar, baz); diff --git a/test/form/samples/quote-id/_expected/iife.js b/test/form/samples/quote-id/_expected/iife.js index 2f853d9bceb..2ba4ceac1bf 100644 --- a/test/form/samples/quote-id/_expected/iife.js +++ b/test/form/samples/quote-id/_expected/iife.js @@ -1,6 +1,6 @@ -(function (quoted_____external1, quoted_____external2) { +(function (quoted_____external1, quoted_____external2, Path_js) { 'use strict'; - console.log(quoted_____external1.foo, quoted_____external2.bar); + console.log(quoted_____external1.foo, quoted_____external2.bar, Path_js.baz); -}(quotedExternal1, quotedExternal2)); +}(quotedExternal1, quotedExternal2, quotedExternal3)); diff --git a/test/form/samples/quote-id/_expected/system.js b/test/form/samples/quote-id/_expected/system.js index e6face3d7c8..7572577c40d 100644 --- a/test/form/samples/quote-id/_expected/system.js +++ b/test/form/samples/quote-id/_expected/system.js @@ -1,15 +1,19 @@ -System.register('Q', ['quoted\'\r\n\u2028\u2029external1', './quoted\'\r\n\u2028\u2029external2'], function () { +System.register('Q', ['quoted\'\ \ +\
\
external1', 'quoted\'\ \ +\
\
external2', 'C:\\File\\Path.js'], function () { 'use strict'; - var foo, bar; + var foo, bar, baz; return { setters: [function (module) { foo = module.foo; }, function (module) { bar = module.bar; + }, function (module) { + baz = module.baz; }], execute: function () { - console.log(foo, bar); + console.log(foo, bar, baz); } }; diff --git a/test/form/samples/quote-id/_expected/umd.js b/test/form/samples/quote-id/_expected/umd.js index 605d5b089b1..8f78fbc0b02 100644 --- a/test/form/samples/quote-id/_expected/umd.js +++ b/test/form/samples/quote-id/_expected/umd.js @@ -1,9 +1,13 @@ (function (global, factory) { - typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('quoted\'\r\n\u2028\u2029external1'), require('./quoted\'\r\n\u2028\u2029external2')) : - typeof define === 'function' && define.amd ? define(['quoted\'\r\n\u2028\u2029external1', './quoted\'\r\n\u2028\u2029external2'], factory) : - (global = global || self, factory(global.quotedExternal1, global.quotedExternal2)); -}(this, (function (quoted_____external1, quoted_____external2) { 'use strict'; + typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('quoted\'\ \ +\
\
external1'), require('quoted\'\ \ +\
\
external2'), require('C:\\File\\Path.js')) : + typeof define === 'function' && define.amd ? define(['quoted\'\ \ +\
\
external1', 'quoted\'\ \ +\
\
external2', 'C:\\File\\Path.js'], factory) : + (global = global || self, factory(global.quotedExternal1, global.quotedExternal2, global.quotedExternal3)); +}(this, (function (quoted_____external1, quoted_____external2, Path_js) { 'use strict'; - console.log(quoted_____external1.foo, quoted_____external2.bar); + console.log(quoted_____external1.foo, quoted_____external2.bar, Path_js.baz); }))); diff --git a/test/form/samples/quote-id/main.js b/test/form/samples/quote-id/main.js index f392585109d..01a0522ebd3 100644 --- a/test/form/samples/quote-id/main.js +++ b/test/form/samples/quote-id/main.js @@ -1,3 +1,4 @@ import { foo } from 'external1'; import { bar } from 'external2'; -console.log(foo, bar); +import { baz } from 'external3'; +console.log(foo, bar, baz);