-
Notifications
You must be signed in to change notification settings - Fork 606
Add basic CommonJS support #748
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
Changes from 17 commits
93a5b2f
f329913
2dfca09
872ae19
29a1403
be697d6
c203acb
8b198fe
8ae9f66
3dc17ac
fc2a5d7
f01b883
2f19775
d3141dc
94cd60e
dda9a73
174be26
81286b4
288ca86
e2f23c5
15c1409
4985943
05a9e36
b77bdc1
dbd8b24
b2e0c0f
f9912a7
20f0198
7d428ac
7ff9d6e
9ec25a0
4c5c54e
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.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -645,6 +645,7 @@ func isDeclarationStatementKind(kind Kind) bool { | |
KindImportEqualsDeclaration, | ||
KindExportDeclaration, | ||
KindExportAssignment, | ||
KindJSExportAssignment, | ||
KindNamespaceExportDeclaration: | ||
return true | ||
} | ||
|
@@ -1282,7 +1283,7 @@ func GetElementOrPropertyAccessArgumentExpressionOrName(node *Node) *Node { | |
} | ||
|
||
func GetElementOrPropertyAccessName(node *Node) string { | ||
name := getElementOrPropertyAccessArgumentExpressionOrName(node) | ||
name := GetElementOrPropertyAccessArgumentExpressionOrName(node) | ||
if name == nil { | ||
return "" | ||
} | ||
|
@@ -1329,11 +1330,11 @@ func GetNonAssignedNameOfDeclaration(declaration *Node) *Node { | |
switch declaration.Kind { | ||
case KindBinaryExpression: | ||
if IsFunctionPropertyAssignment(declaration) { | ||
return getElementOrPropertyAccessArgumentExpressionOrName(declaration.AsBinaryExpression().Left) | ||
return GetElementOrPropertyAccessArgumentExpressionOrName(declaration.AsBinaryExpression().Left) | ||
} | ||
return nil | ||
case KindExportAssignment: | ||
expr := declaration.AsExportAssignment().Expression | ||
case KindExportAssignment, KindJSExportAssignment: | ||
expr := declaration.Expression() | ||
if IsIdentifier(expr) { | ||
return expr | ||
} | ||
|
@@ -1392,22 +1393,6 @@ func IsFunctionPropertyAssignment(node *Node) bool { | |
return false | ||
} | ||
|
||
// Does not handle signed numeric names like `a[+0]` - handling those would require handling prefix unary expressions | ||
// throughout late binding handling as well, which is awkward (but ultimately probably doable if there is demand) | ||
func getElementOrPropertyAccessArgumentExpressionOrName(node *Node) *Node { | ||
switch node.Kind { | ||
case KindPropertyAccessExpression: | ||
return node.Name() | ||
case KindElementAccessExpression: | ||
arg := SkipParentheses(node.AsElementAccessExpression().ArgumentExpression) | ||
if IsStringOrNumericLiteralLike(arg) { | ||
return arg | ||
} | ||
return node | ||
} | ||
panic("Unhandled case in getElementOrPropertyAccessArgumentExpressionOrName") | ||
} | ||
|
||
/** | ||
* A declaration has a dynamic name if all of the following are true: | ||
* 1. The declaration has a computed property name. | ||
|
@@ -1473,10 +1458,6 @@ func IsEffectiveExternalModule(node *SourceFile, compilerOptions *core.CompilerO | |
return IsExternalModule(node) || (isCommonJSContainingModuleKind(compilerOptions.GetEmitModuleKind()) && node.CommonJSModuleIndicator != nil) | ||
} | ||
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. this is unused |
||
|
||
func IsEffectiveExternalModuleWorker(node *SourceFile, moduleKind core.ModuleKind) bool { | ||
return IsExternalModule(node) || (isCommonJSContainingModuleKind(moduleKind) && node.CommonJSModuleIndicator != nil) | ||
} | ||
|
||
func isCommonJSContainingModuleKind(kind core.ModuleKind) bool { | ||
return kind == core.ModuleKindCommonJS || kind == core.ModuleKindNode16 || kind == core.ModuleKindNodeNext | ||
} | ||
|
@@ -1646,20 +1627,7 @@ func IsEnumConst(node *Node) bool { | |
} | ||
|
||
func ExportAssignmentIsAlias(node *Node) bool { | ||
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 believe this was for syntactic |
||
return isAliasableExpression(getExportAssignmentExpression(node)) | ||
} | ||
|
||
func getExportAssignmentExpression(node *Node) *Node { | ||
switch node.Kind { | ||
case KindExportAssignment: | ||
return node.AsExportAssignment().Expression | ||
case KindBinaryExpression: | ||
return node.AsBinaryExpression().Right | ||
} | ||
panic("Unhandled case in getExportAssignmentExpression") | ||
} | ||
|
||
func isAliasableExpression(e *Node) bool { | ||
e := node.AsExportAssignment().Expression | ||
return IsEntityNameExpression(e) || IsClassExpression(e) | ||
} | ||
|
||
|
@@ -2063,6 +2031,7 @@ func GetMeaningFromDeclaration(node *Node) SemanticMeaning { | |
KindImportEqualsDeclaration, | ||
KindImportDeclaration, | ||
KindExportAssignment, | ||
KindJSExportAssignment, | ||
KindExportDeclaration: | ||
return SemanticMeaningAll | ||
|
||
|
@@ -2451,22 +2420,29 @@ func IsNonLocalAlias(symbol *Symbol, excludes SymbolFlags) bool { | |
// An alias symbol is created by one of the following declarations: | ||
// | ||
// import <symbol> = ... | ||
// const <symbol> = ... (JS only) | ||
// const { <symbol>, ... } = ... (JS only) | ||
// import <symbol> from ... | ||
// import * as <symbol> from ... | ||
// import { x as <symbol> } from ... | ||
// export { x as <symbol> } from ... | ||
// export * as ns <symbol> from ... | ||
// export = <EntityNameExpression> | ||
// export default <EntityNameExpression> | ||
// module.exports = <EntityNameExpression> (JS only) | ||
func IsAliasSymbolDeclaration(node *Node) bool { | ||
switch node.Kind { | ||
case KindImportEqualsDeclaration, KindNamespaceExportDeclaration, KindNamespaceImport, KindNamespaceExport, | ||
KindImportSpecifier, KindExportSpecifier: | ||
return true | ||
case KindImportClause: | ||
return node.AsImportClause().Name() != nil | ||
case KindExportAssignment: | ||
case KindExportAssignment, KindJSExportAssignment: | ||
return ExportAssignmentIsAlias(node) | ||
case KindVariableDeclaration: | ||
return IsVariableDeclarationInitializedToRequire(node) | ||
case KindBindingElement: | ||
return IsVariableDeclarationInitializedToRequire(node.Parent.Parent) | ||
} | ||
return false | ||
} | ||
|
@@ -2639,3 +2615,13 @@ func getPragmaArgument(pragma *Pragma, name string) string { | |
} | ||
return "" | ||
} | ||
|
||
func IsVariableDeclarationInitializedToRequire(node *Node) bool { | ||
return node.Kind == KindVariableDeclaration && | ||
node.AsVariableDeclaration().Initializer != nil && | ||
IsRequireCall(node.AsVariableDeclaration().Initializer, true /*requireStringLiteralLikeArgument*/) | ||
} | ||
|
||
func IsBindingElementOfRequire(node *Node) bool { | ||
return IsBindingElement(node) && IsVariableDeclarationInitializedToRequire(node.Parent.Parent) | ||
} |
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.
we have a private and public duplicate of this function so I deleted the private one