-
Notifications
You must be signed in to change notification settings - Fork 12.9k
JavaScript LS scaffolding + JS module inference #5266
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1a36fce
JavaScript LS scaffolding + JS module inference
RyanCavanaugh 2f7719b
Address CR feedback
RyanCavanaugh 61b7100
Remove obsolute AMD logic from reference preprocessing in services
RyanCavanaugh 5725fc4
CR feedback
RyanCavanaugh efa63b9
LKG update
RyanCavanaugh eda6eca
Merge remote-tracking branch 'upstream/master' into javaScriptModules
RyanCavanaugh abf270a
do not look into nested es6 exports / imports when collecting externa…
vladima 91eb758
CR feedback
RyanCavanaugh 7a94031
Rename `isTsx` for clarity
RyanCavanaugh d880d4f
Don't look for .js files when resolving node modules
RyanCavanaugh 3f4e5a4
Merge branch 'master' into javaScriptModules
RyanCavanaugh 7dd1bf4
Merge branch 'master' into javaScriptModules
RyanCavanaugh e630ce2
Fix merge problems from master
RyanCavanaugh 69ca1f2
Merge remote-tracking branch 'origin/master' into javaScriptModules
RyanCavanaugh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
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); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 would move the
!file.commonJsModuleIndicator
test intobindCallExpression
so we have all the logic in one place.