-
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
JavaScript LS scaffolding + JS module inference #5266
Changes from 6 commits
1a36fce
2f7719b
61b7100
5725fc4
efa63b9
eda6eca
abf270a
91eb758
7a94031
d880d4f
3f4e5a4
7dd1bf4
e630ce2
69ca1f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,8 @@ namespace ts { | |
|
||
export const enum ModuleInstanceState { | ||
NonInstantiated = 0, | ||
Instantiated = 1, | ||
ConstEnumOnly = 2 | ||
Instantiated = 1, | ||
ConstEnumOnly = 2 | ||
} | ||
|
||
export function getModuleInstanceState(node: Node): ModuleInstanceState { | ||
|
@@ -90,6 +90,8 @@ namespace ts { | |
let lastContainer: Node; | ||
let seenThisKeyword: boolean; | ||
|
||
let isJavaScriptFile = isSourceFileJavaScript(file); | ||
|
||
// If this file is an external module, then it is automatically in strict-mode according to | ||
// ES6. If it is not an external module, then we'll determine if it is in strict mode or | ||
// not depending on if we see "use strict" in certain places (or if we hit a class/namespace). | ||
|
@@ -164,6 +166,9 @@ namespace ts { | |
return "__export"; | ||
case SyntaxKind.ExportAssignment: | ||
return (<ExportAssignment>node).isExportEquals ? "export=" : "default"; | ||
case SyntaxKind.BinaryExpression: | ||
// Binary expression case is for JS module 'module.exports = expr' | ||
return "export="; | ||
case SyntaxKind.FunctionDeclaration: | ||
case SyntaxKind.ClassDeclaration: | ||
return node.flags & NodeFlags.Default ? "default" : undefined; | ||
|
@@ -779,7 +784,7 @@ namespace ts { | |
return "__" + indexOf((<SignatureDeclaration>node.parent).parameters, node); | ||
} | ||
|
||
function bind(node: Node) { | ||
function bind(node: Node): void { | ||
node.parent = parent; | ||
|
||
let savedInStrictMode = inStrictMode; | ||
|
@@ -851,9 +856,18 @@ namespace ts { | |
|
||
function bindWorker(node: Node) { | ||
switch (node.kind) { | ||
/* Strict mode checks */ | ||
case SyntaxKind.Identifier: | ||
return checkStrictModeIdentifier(<Identifier>node); | ||
case SyntaxKind.BinaryExpression: | ||
if (isJavaScriptFile) { | ||
if (isExportsPropertyAssignment(node)) { | ||
bindExportsPropertyAssignment(<BinaryExpression>node); | ||
} | ||
else if (isModuleExportsAssignment(node)) { | ||
bindModuleExportsAssignment(<BinaryExpression>node); | ||
} | ||
} | ||
return checkStrictModeBinaryExpression(<BinaryExpression>node); | ||
case SyntaxKind.CatchClause: | ||
return checkStrictModeCatchClause(<CatchClause>node); | ||
|
@@ -919,6 +933,14 @@ namespace ts { | |
checkStrictModeFunctionName(<FunctionExpression>node); | ||
let bindingName = (<FunctionExpression>node).name ? (<FunctionExpression>node).name.text : "__function"; | ||
return bindAnonymousDeclaration(<FunctionExpression>node, SymbolFlags.Function, bindingName); | ||
|
||
case SyntaxKind.CallExpression: | ||
if (isJavaScriptFile) { | ||
bindCallExpression(<CallExpression>node); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would move the |
||
} | ||
break; | ||
|
||
// Members of classes, interfaces, and modules | ||
case SyntaxKind.ClassExpression: | ||
case SyntaxKind.ClassDeclaration: | ||
return bindClassLikeDeclaration(<ClassLikeDeclaration>node); | ||
|
@@ -930,6 +952,8 @@ namespace ts { | |
return bindEnumDeclaration(<EnumDeclaration>node); | ||
case SyntaxKind.ModuleDeclaration: | ||
return bindModuleDeclaration(<ModuleDeclaration>node); | ||
|
||
// Imports and exports | ||
case SyntaxKind.ImportEqualsDeclaration: | ||
case SyntaxKind.NamespaceImport: | ||
case SyntaxKind.ImportSpecifier: | ||
|
@@ -949,16 +973,21 @@ namespace ts { | |
function bindSourceFileIfExternalModule() { | ||
setExportContextFlag(file); | ||
if (isExternalModule(file)) { | ||
bindAnonymousDeclaration(file, SymbolFlags.ValueModule, `"${removeFileExtension(file.fileName)}"`); | ||
bindSourceFileAsExternalModule(); | ||
} | ||
} | ||
|
||
function bindExportAssignment(node: ExportAssignment) { | ||
function bindSourceFileAsExternalModule() { | ||
bindAnonymousDeclaration(file, SymbolFlags.ValueModule, `"${removeFileExtension(file.fileName) }"`); | ||
} | ||
|
||
function bindExportAssignment(node: ExportAssignment|BinaryExpression) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We usually put a blank on both sides of the |
||
let boundExpression = node.kind === SyntaxKind.ExportAssignment ? (<ExportAssignment>node).expression : (<BinaryExpression>node).right; | ||
if (!container.symbol || !container.symbol.exports) { | ||
// Export assignment in some sort of block construct | ||
bindAnonymousDeclaration(node, SymbolFlags.Alias, getDeclarationName(node)); | ||
} | ||
else if (node.expression.kind === SyntaxKind.Identifier) { | ||
else if (boundExpression.kind === SyntaxKind.Identifier) { | ||
// An export default clause with an identifier exports all meanings of that identifier | ||
declareSymbol(container.symbol.exports, container.symbol, node, SymbolFlags.Alias, SymbolFlags.PropertyExcludes | SymbolFlags.AliasExcludes); | ||
} | ||
|
@@ -985,6 +1014,34 @@ namespace ts { | |
} | ||
} | ||
|
||
function setCommonJsModuleIndicator(node: Node) { | ||
if (!file.commonJsModuleIndicator) { | ||
file.commonJsModuleIndicator = node; | ||
bindSourceFileAsExternalModule(); | ||
} | ||
} | ||
|
||
function bindExportsPropertyAssignment(node: BinaryExpression) { | ||
// When we create a property via 'exports.foo = bar', the 'exports.foo' property access | ||
// expression is the declaration | ||
setCommonJsModuleIndicator(node); | ||
declareSymbol(file.symbol.exports, file.symbol, <PropertyAccessExpression>node.left, SymbolFlags.Property | SymbolFlags.Export, SymbolFlags.None); | ||
} | ||
|
||
function bindModuleExportsAssignment(node: BinaryExpression) { | ||
// 'module.exports = expr' assignment | ||
setCommonJsModuleIndicator(node); | ||
bindExportAssignment(node); | ||
} | ||
|
||
function bindCallExpression(node: CallExpression) { | ||
// We're only inspecting call expressions to detect CommonJS modules, so we can skip | ||
// this check if we've already seen the module indicator | ||
if (!file.commonJsModuleIndicator && isRequireCall(node)) { | ||
setCommonJsModuleIndicator(node); | ||
} | ||
} | ||
|
||
function bindClassLikeDeclaration(node: ClassLikeDeclaration) { | ||
if (node.kind === SyntaxKind.ClassDeclaration) { | ||
bindBlockScopedDeclaration(node, SymbolFlags.Class, SymbolFlags.ClassExcludes); | ||
|
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.
Very distracting to have all these unrelated changes.
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.
I can roll back the LKG update portion of this if it's truly problematic.