Skip to content

Commit

Permalink
Correctly handling number key (#5328)
Browse files Browse the repository at this point in the history
* handling __proto__

* handling __proto__ key

* handling __proto__ key

* handling __proto__ key

* handling __proto__ key

* handling __proto__ key

* handling __proto__ key

* handling __proto__ key

* handling __proto__ key

* handling __proto__ key

* handling __proto__ key

* handling __proto__ key

* handling string key

* handling __proto__ key

* handling string name

* handling __proto__ key

* handling __proto__ key

* handling string name

* hanlding __proto__ key

* handling __proto__ key

* handling __proto__ key

* handling string name

* handling string name

* handling string name

* handling __proto__ key

* handling __proto__ key

* handling __proto__ key

* Correctly handling number key

* Correctly handling number key

* Correctly handling number key
  • Loading branch information
LongTengDao authored Jan 6, 2024
1 parent c249413 commit 47e9bd1
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/utils/identifierHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ export function makeLegal(value: string): string {
}

export const VALID_IDENTIFIER_REGEXP = /^[$_\p{ID_Start}][$\u200C\u200D\p{ID_Continue}]*$/u;
export const NUMBER_REGEXP = /^\d+$/;
const NUMBER_REGEXP = /^(?:0|[1-9]\d*)$/;

export function stringifyObjectKeyIfNeeded(key: string) {
if (VALID_IDENTIFIER_REGEXP.test(key) || NUMBER_REGEXP.test(key)) {
if (VALID_IDENTIFIER_REGEXP.test(key)) {
return key === '__proto__' ? '["__proto__"]' : key;
}
if (NUMBER_REGEXP.test(key) && +key <= Number.MAX_SAFE_INTEGER) {
return key;
}
return JSON.stringify(key);
}

Expand Down
11 changes: 11 additions & 0 deletions test/form/samples/index-key/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = defineTest({
description: 'index key',
options: {
external: ['x'],
output: {
globals: {
x: 'x'
}
}
}
});
11 changes: 11 additions & 0 deletions test/form/samples/index-key/_expected/amd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
define(['x'], (function (x$1) { 'use strict';

var x = /*#__PURE__*/Object.freeze({
__proto__: null,
"00": x$1["00"],
"9007199254740993": x$1["9007199254740993"]
});

console.log(x);

}));
11 changes: 11 additions & 0 deletions test/form/samples/index-key/_expected/cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

var x$1 = require('x');

var x = /*#__PURE__*/Object.freeze({
__proto__: null,
"00": x$1["00"],
"9007199254740993": x$1["9007199254740993"]
});

console.log(x);
9 changes: 9 additions & 0 deletions test/form/samples/index-key/_expected/es.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { "00" as _00, "9007199254740993" as _9007199254740993 } from 'x';

var x = /*#__PURE__*/Object.freeze({
__proto__: null,
"00": _00,
"9007199254740993": _9007199254740993
});

console.log(x);
12 changes: 12 additions & 0 deletions test/form/samples/index-key/_expected/iife.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(function (x$1) {
'use strict';

var x = /*#__PURE__*/Object.freeze({
__proto__: null,
"00": x$1["00"],
"9007199254740993": x$1["9007199254740993"]
});

console.log(x);

})(x);
21 changes: 21 additions & 0 deletions test/form/samples/index-key/_expected/system.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
System.register(['x'], (function () {
'use strict';
var _00, _9007199254740993;
return {
setters: [function (module) {
_00 = module["00"];
_9007199254740993 = module["9007199254740993"];
}],
execute: (function () {

var x = /*#__PURE__*/Object.freeze({
__proto__: null,
"00": _00,
"9007199254740993": _9007199254740993
});

console.log(x);

})
};
}));
15 changes: 15 additions & 0 deletions test/form/samples/index-key/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('x')) :
typeof define === 'function' && define.amd ? define(['x'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.x));
})(this, (function (x$1) { 'use strict';

var x = /*#__PURE__*/Object.freeze({
__proto__: null,
"00": x$1["00"],
"9007199254740993": x$1["9007199254740993"]
});

console.log(x);

}));
2 changes: 2 additions & 0 deletions test/form/samples/index-key/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import * as x from './x';
console.log(x);
1 change: 1 addition & 0 deletions test/form/samples/index-key/x.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { '00', '9007199254740993' } from 'x';

0 comments on commit 47e9bd1

Please sign in to comment.