From 81bf0609697f0e9b1838738fb9bd1a5d2a460bb3 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 12 Dec 2016 12:11:13 -0500 Subject: [PATCH] quote reserved properties in legacy mode --- src/Declaration.js | 3 ++- src/utils/makeLegalIdentifier.js | 2 +- test/form/legacy-reified-namespace/_config.js | 7 +++++++ .../legacy-reified-namespace/_expected/amd.js | 14 ++++++++++++++ .../legacy-reified-namespace/_expected/cjs.js | 12 ++++++++++++ .../legacy-reified-namespace/_expected/es.js | 10 ++++++++++ .../legacy-reified-namespace/_expected/iife.js | 15 +++++++++++++++ .../legacy-reified-namespace/_expected/umd.js | 18 ++++++++++++++++++ test/form/legacy-reified-namespace/main.js | 3 +++ .../form/legacy-reified-namespace/namespace.js | 3 +++ test/form/legacy/_config.js | 2 +- 11 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 test/form/legacy-reified-namespace/_config.js create mode 100644 test/form/legacy-reified-namespace/_expected/amd.js create mode 100644 test/form/legacy-reified-namespace/_expected/cjs.js create mode 100644 test/form/legacy-reified-namespace/_expected/es.js create mode 100644 test/form/legacy-reified-namespace/_expected/iife.js create mode 100644 test/form/legacy-reified-namespace/_expected/umd.js create mode 100644 test/form/legacy-reified-namespace/main.js create mode 100644 test/form/legacy-reified-namespace/namespace.js diff --git a/src/Declaration.js b/src/Declaration.js index 021d0bf..1d13315 100644 --- a/src/Declaration.js +++ b/src/Declaration.js @@ -1,5 +1,5 @@ import { blank, forOwn, keys } from './utils/object.js'; -import makeLegalIdentifier from './utils/makeLegalIdentifier.js'; +import makeLegalIdentifier, { reservedWords } from './utils/makeLegalIdentifier.js'; import { UNKNOWN } from './ast/values.js'; export default class Declaration { @@ -83,6 +83,7 @@ export class SyntheticNamespaceDeclaration { return `${indentString}get ${name} () { return ${original.getName( es )}; }`; } + if ( legacy && ~reservedWords.indexOf( name ) ) name = `'${name}'`; return `${indentString}${name}: ${original.getName( es )}`; }); diff --git a/src/utils/makeLegalIdentifier.js b/src/utils/makeLegalIdentifier.js index c9733e5..846c42e 100644 --- a/src/utils/makeLegalIdentifier.js +++ b/src/utils/makeLegalIdentifier.js @@ -1,6 +1,6 @@ import { blank } from './object.js'; -const reservedWords = 'break case class catch const continue debugger default delete do else export extends finally for function if import in instanceof let new return super switch this throw try typeof var void while with yield enum await implements package protected static interface private public'.split( ' ' ); +export const reservedWords = 'break case class catch const continue debugger default delete do else export extends finally for function if import in instanceof let new return super switch this throw try typeof var void while with yield enum await implements package protected static interface private public'.split( ' ' ); const builtins = 'Infinity NaN undefined null true false eval uneval isFinite isNaN parseFloat parseInt decodeURI decodeURIComponent encodeURI encodeURIComponent escape unescape Object Function Boolean Symbol Error EvalError InternalError RangeError ReferenceError SyntaxError TypeError URIError Number Math Date String RegExp Array Int8Array Uint8Array Uint8ClampedArray Int16Array Uint16Array Int32Array Uint32Array Float32Array Float64Array Map Set WeakMap WeakSet SIMD ArrayBuffer DataView JSON Promise Generator GeneratorFunction Reflect Proxy Intl'.split( ' ' ); const blacklisted = blank(); diff --git a/test/form/legacy-reified-namespace/_config.js b/test/form/legacy-reified-namespace/_config.js new file mode 100644 index 0000000..1905df3 --- /dev/null +++ b/test/form/legacy-reified-namespace/_config.js @@ -0,0 +1,7 @@ +module.exports = { + description: 'quotes reserved words in object literals', + options: { + moduleName: 'myBundle', + legacy: true + } +}; diff --git a/test/form/legacy-reified-namespace/_expected/amd.js b/test/form/legacy-reified-namespace/_expected/amd.js new file mode 100644 index 0000000..5fb5bd2 --- /dev/null +++ b/test/form/legacy-reified-namespace/_expected/amd.js @@ -0,0 +1,14 @@ +define(function () { 'use strict'; + + const _typeof = 'typeof'; + const foo = 1; + + + var namespace = (Object.freeze || Object)({ + 'typeof': _typeof, + foo: foo + }); + + console.log( namespace ); + +}); diff --git a/test/form/legacy-reified-namespace/_expected/cjs.js b/test/form/legacy-reified-namespace/_expected/cjs.js new file mode 100644 index 0000000..10ecf7c --- /dev/null +++ b/test/form/legacy-reified-namespace/_expected/cjs.js @@ -0,0 +1,12 @@ +'use strict'; + +const _typeof = 'typeof'; +const foo = 1; + + +var namespace = (Object.freeze || Object)({ + 'typeof': _typeof, + foo: foo +}); + +console.log( namespace ); diff --git a/test/form/legacy-reified-namespace/_expected/es.js b/test/form/legacy-reified-namespace/_expected/es.js new file mode 100644 index 0000000..7b52e33 --- /dev/null +++ b/test/form/legacy-reified-namespace/_expected/es.js @@ -0,0 +1,10 @@ +const _typeof = 'typeof'; +const foo = 1; + + +var namespace = (Object.freeze || Object)({ + 'typeof': _typeof, + foo: foo +}); + +console.log( namespace ); diff --git a/test/form/legacy-reified-namespace/_expected/iife.js b/test/form/legacy-reified-namespace/_expected/iife.js new file mode 100644 index 0000000..9b60efd --- /dev/null +++ b/test/form/legacy-reified-namespace/_expected/iife.js @@ -0,0 +1,15 @@ +(function () { + 'use strict'; + + const _typeof = 'typeof'; + const foo = 1; + + + var namespace = (Object.freeze || Object)({ + 'typeof': _typeof, + foo: foo + }); + + console.log( namespace ); + +}()); diff --git a/test/form/legacy-reified-namespace/_expected/umd.js b/test/form/legacy-reified-namespace/_expected/umd.js new file mode 100644 index 0000000..fe542c0 --- /dev/null +++ b/test/form/legacy-reified-namespace/_expected/umd.js @@ -0,0 +1,18 @@ +(function (global, factory) { + typeof exports === 'object' && typeof module !== 'undefined' ? factory() : + typeof define === 'function' && define.amd ? define(factory) : + (factory()); +}(this, (function () { 'use strict'; + + const _typeof = 'typeof'; + const foo = 1; + + + var namespace = (Object.freeze || Object)({ + 'typeof': _typeof, + foo: foo + }); + + console.log( namespace ); + +}))); diff --git a/test/form/legacy-reified-namespace/main.js b/test/form/legacy-reified-namespace/main.js new file mode 100644 index 0000000..5585c1e --- /dev/null +++ b/test/form/legacy-reified-namespace/main.js @@ -0,0 +1,3 @@ +import * as namespace from './namespace.js'; + +console.log( namespace ); diff --git a/test/form/legacy-reified-namespace/namespace.js b/test/form/legacy-reified-namespace/namespace.js new file mode 100644 index 0000000..46fe09d --- /dev/null +++ b/test/form/legacy-reified-namespace/namespace.js @@ -0,0 +1,3 @@ +const _typeof = 'typeof'; +export { _typeof as typeof }; +export const foo = 1; diff --git a/test/form/legacy/_config.js b/test/form/legacy/_config.js index 8ccc71d..a461d8e 100644 --- a/test/form/legacy/_config.js +++ b/test/form/legacy/_config.js @@ -1,7 +1,7 @@ module.exports = { description: 'supports environments without Object.freeze, Object.defined', options: { - moduleName: 'myBundle', + moduleName: 'myBundle', legacy: true } };