microsoft / microsoft/TypeScript

[Feature request] use Proxy in __importStar when target >= es6 with esModuleInterop

Open
#31,724 0 comments 0 reactions 1 assignee View on GitHub

@rbuckton is already working on this.

Since Jun 13, 2019.

Needs Investigation
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Search Terms

Suggestion

use Proxy in __importStar when target >= es6 with esModuleInterop

Use Cases

import * as xxxx from yyyy with esModuleInterop

Examples

import console from 'debug-color2';
import util from 'util';

util.inspect.defaultOptions.colors = true;

function __importStar(mod)
{
	if (mod && mod.__esModule) return mod;
	var result = {};
	if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
	result["default"] = mod;
	return result;
}

function __importStar2(mod)
{
	if (mod && mod.__esModule) return mod;

	let _type = typeof mod;

	if (mod == null || _type !== 'function' && _type !== 'object')
	{
		return {
			default: mod
		}
	}

	return new Proxy(mod, {
		get(target, property)
		{
			if (property === 'default')
			{
				return mod
			}

			return Reflect.get(mod, property);
		},
		has(target, property)
		{
			if (property === 'default')
			{
				return true
			}

			return Reflect.has(mod, property);
		},
	});
}

let _a1 = __importStar(require('./temp2'));

let _a2 = __importStar2(require('./temp2'));

let _a3 = require('./temp2');

print(_a1, 1);

print(_a2, 2);

print(_a3, 3);

function print(mod, n)
{
	console.log('----------');
	_dir(n);

	_dir(mod, `mod`);
	_dir(mod.v1, `mod.v1`);

	mod.notExistsProp = 777;

	try
	{
		_dir(mod.default(), `mod.default()`);
	}
	catch (e)
	{
		console.red.log(e.message);
	}

	mod.v1++;

	_dir(mod.v1, `mod.v1`);

	try
	{
		_dir(mod.default(), `mod.default()`);
	}
	catch (e)
	{
		console.red.log(e.message);
	}

	_dir(mod, `mod`);
	_dir(mod.default, `mod.default`);

	_dir(Object.keys(mod));

	_dir('default' in mod, `'default' in mod`);

	_dir('__esModule' in mod, `'__esModule' in mod`);
	_dir(mod.__esModule, `mod.__esModule`);
	_dir(mod.__cccc, `mod.__cccc`);

	try
	{
		_dir('__esModule' in mod.default, `'__esModule' in mod.default`);
		_dir(mod.default.__esModule, `mod.default.__esModule`);
		_dir(mod.default.__cccc, `mod.default.__cccc`);
	}
	catch (e)
	{
		console.red.log(e.message);
	}

	try
	{
		_dir(mod(), `mod()`);
	}
	catch (e)
	{
		console.red.log(e.message);
	}
}

function _dir(value, label?)
{
	if (typeof label !== 'undefined')
	{
		console.log(`[${label}]`, util.inspect(value));
	}
	else
	{
		console.log(util.inspect(value));
	}
}
import util from 'util';

function aaaa()
{
	console.log(`module.exports`, util.inspect(module.exports));

	return 1
}

aaaa.v1 = 777;

Object.defineProperty(aaaa, '__cccc', {
	value: true
});

export = aaaa

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.