-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support ES6 import and export declarations #1983
Conversation
NamedImports : { } { ImportsList } { ImportsList , } ImportsList : ImportSpecifier ImportsList , ImportSpecifier ImportSpecifier : ImportedBinding IdentifierName as ImportedBinding Conflicts: src/compiler/parser.ts
…void creating missing symbols Missing symbols are defined when the declaration doesnt have name, so if we created node for missing identifier it would end up binding symbol with name (Missing)
…claration This helps binder to use it directly to bind the default binding
Conflicts: src/compiler/checker.ts
Notes from the design meeting If we rewrite references to an imported function Brian notes that |
@sheetalkamat That should be an error. We currently put all external symbols of a module into scope, but we should exclude those that came from an export declaration. I will fix. |
@RyanCavanaugh Regarding effects of rewriting on |
Latest commit adds support for An example: // a.ts
export var x = 1;
export var y = 2;
export function foo() {
console.log("Hello from a");
} // b.ts
export function foo() {
console.log("Hello from b");
}
export { foo as bar };
export * from "./a"; // t.ts
import * as a from "./a";
import * as b from "./b";
a.foo(); // Hello from a
b.foo(); // Hello from b
b.bar(); // Hello from b
console.log(a.x); // 1
console.log(b.x); // 1 This generates the following when compiled with // a.js
exports.x = 1;
exports.y = 2;
function foo() {
console.log("Hello from a");
}
exports.foo = foo; // b.js
function foo() {
console.log("Hello from b");
}
exports.foo = foo;
exports.bar = foo;
var _a = require("./a");
for (var _b in _a) if (!exports.hasOwnProperty(_b)) exports[_b] = _a[_b]; // t.js
var a = require("./a");
var b = require("./b");
a.foo();
b.foo();
b.bar();
console.log(a.x);
console.log(b.x); |
return links.localModuleName; | ||
|
||
function isExistingName(name: string) { | ||
return hasProperty(sourceFile.identifiers, name) || hasProperty(generatedNames, name); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it possible to have a situation when name does not appear in current file but exists on the top level in some another file (like window
or name
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, we should probably check globals
as well. Will fix.
return links.localModuleName; | ||
|
||
function isExistingName(name: string) { | ||
return hasProperty(globals, name) || hasProperty(sourceFile.identifiers, name) || hasProperty(generatedNames, name); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
identifiers
can be incomplete after incremental parsing so 'compile on save' will be broken. that is why we use a separate table nameTable
in part of SourceFile
that belongs to services layer. We can pull initializeNameTable function that builds nameTable on demand down to the base compiler.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we should do that.
@@ -1820,7 +1847,7 @@ module ts { | |||
function isReusableModuleElement(node: Node) { | |||
if (node) { | |||
switch (node.kind) { | |||
case SyntaxKind.ImportDeclaration: | |||
case SyntaxKind.ImportEqualsDeclaration: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the new import and export node types should be in this list.
Conflicts: src/compiler/diagnosticInformationMap.generated.ts src/compiler/diagnosticMessages.json src/compiler/emitter.ts tests/baselines/reference/APISample_compile.js tests/baselines/reference/APISample_compile.types tests/baselines/reference/APISample_linter.js tests/baselines/reference/APISample_linter.types tests/baselines/reference/APISample_transform.js tests/baselines/reference/APISample_transform.types tests/baselines/reference/APISample_watcher.js tests/baselines/reference/APISample_watcher.types tests/baselines/reference/recursiveClassReferenceTest.js.map tests/baselines/reference/recursiveClassReferenceTest.sourcemap.txt
Support ES6 import and export declarations
Super nice. Thanks! |
how to import templates like this with the es6 syntax, please: |
@daslicht First, you need a loader that interpolates them into a named module export, usually this is the export named declare module '*.handlebars' {
// export matching the shape produced by the loader.
} In your case, it looks like using ES2015 module syntax is not supported, and you are stuck with |
Any example on that ? please? I am trying to use handlebars in an TypeScript setup |
@daslicht say I were using SystemJS to load and bundle my application. In that case I would have a setup like this (simplified) systemjs.config.js SystemJS.config({
meta: {
'*.handlebars': {
loader: 'text-loader-plugin'
}
}
}); If I were using webpack it would look like this (simplified) webpack.config.js module.exports = {
loaders: [
{
test: /\.handlebars$/,
loader: 'text-loader-plugin'
}
]
}; The plugin would sort of look like this (changed and simplified for exposition) text-loader-plugin.js export default function (path) {
const content = load(path); // XHR or fs.readFile
return {
'default': content
};
} Then in my TypeScript project I would have a file like globals.d.ts declare module '*.handlebars' {
export default '';
} Then I would use it like this app/home.ts import template from 'app/home.handlebars'; |
OK, thank you !, I keep trying, my approach was using the |
This PR adds support for ES6 import and export declarations. Supported import forms include:
Supported export forms include:
The following export forms are not yet implemented:
ES6 modules are very similar to TypeScript's external modules. ES6 modules permit individual entities to be exported using an
export
modifier and additionally support an optionalexport default
in each module. Theexport default
corresponds to TypeScript'sexport =
, except that in ES6 a module can have both individual exports and a default export.Given the following external module
utils.ts
:here are some ways it might be imported:
When ES6 modules are compiled down-level for ES3 or ES5 (which is currently the only supported way), a module cannot have both individual exports and a default export.