Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
fix(ts-estree): make sure that every node can be converted to tsNode#287
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
10 commits Select commitHold shift + click to select a range
02a3137
fix(ts-estree): improve types in fix export
armano2533c03c
fix(ts-estree): fix issue with registering in maps exported nodes
armano2e6cfa76
fix(ts-estree): register node to tsNodeToESTreeNodeMap only once
armano2bbc4b21
fix(eslint-plugin): fix crash in promise-function-async
armano2cbb9858
fix(ts-estree): make sure that every node can be translated to tsNode
armano21867b9d
chore(ts-estree): add missing function description
armano2c4cd0af
test(ts-estree): add test case for esTreeNodeToTSNodeMap
armano269970f7
chore(ts-estree): fix code formatting
armano29277075
Merge branch 'master' into fix-export
armano25e0d993
Merge branch 'master' into fix-export
armano2File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
7 changes: 5 additions & 2 deletionspackages/eslint-plugin/src/rules/promise-function-async.ts
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
6 changes: 5 additions & 1 deletionpackages/eslint-plugin/tests/rules/promise-function-async.test.ts
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
105 changes: 76 additions & 29 deletionspackages/typescript-estree/src/convert.ts
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 |
---|---|---|
@@ -10,7 +10,6 @@ import { | ||
canContainDirective, | ||
createError, | ||
findNextToken, | ||
getBinaryExpressionType, | ||
getDeclarationKind, | ||
getLastModifier, | ||
@@ -111,29 +110,78 @@ export class Converter { | ||
this.allowPattern = allowPattern; | ||
} | ||
let result = this.convertNode(node as TSNode, parent || node.parent); | ||
this.registerTSNodeInNodeMap(node, result); | ||
this.inTypeMode = typeMode; | ||
this.allowPattern = pattern; | ||
return result; | ||
} | ||
/** | ||
* Fixes the exports of the given ts.Node | ||
* @param node the ts.Node | ||
* @param result result | ||
* @returns the ESTreeNode with fixed exports | ||
*/ | ||
private fixExports<T extends TSESTree.ExportDeclaration>( | ||
node: ts.Node, | ||
result: T | ||
): TSESTree.ExportDefaultDeclaration | TSESTree.ExportNamedDeclaration | T { | ||
// check for exports | ||
if (node.modifiers && node.modifiers[0].kind === SyntaxKind.ExportKeyword) { | ||
/** | ||
* Make sure that original node is registered instead of export | ||
*/ | ||
this.registerTSNodeInNodeMap(node, result); | ||
const exportKeyword = node.modifiers[0]; | ||
const nextModifier = node.modifiers[1]; | ||
const declarationIsDefault = | ||
nextModifier && nextModifier.kind === SyntaxKind.DefaultKeyword; | ||
const varToken = declarationIsDefault | ||
? findNextToken(nextModifier, this.ast, this.ast) | ||
: findNextToken(exportKeyword, this.ast, this.ast); | ||
result.range[0] = varToken!.getStart(this.ast); | ||
result.loc = getLocFor(result.range[0], result.range[1], this.ast); | ||
if (declarationIsDefault) { | ||
return this.createNode<TSESTree.ExportDefaultDeclaration>(node, { | ||
type: AST_NODE_TYPES.ExportDefaultDeclaration, | ||
declaration: result, | ||
range: [exportKeyword.getStart(this.ast), result.range[1]] | ||
}); | ||
} else { | ||
return this.createNode<TSESTree.ExportNamedDeclaration>(node, { | ||
type: AST_NODE_TYPES.ExportNamedDeclaration, | ||
declaration: result, | ||
specifiers: [], | ||
source: null, | ||
range: [exportKeyword.getStart(this.ast), result.range[1]] | ||
}); | ||
} | ||
} | ||
return result; | ||
} | ||
/** | ||
* Register specific TypeScript node into map with first ESTree node provided | ||
*/ | ||
private registerTSNodeInNodeMap( | ||
node: ts.Node, | ||
result: TSESTree.BaseNode | null | ||
) { | ||
if (result && this.options.shouldProvideParserServices) { | ||
if (!this.tsNodeToESTreeNodeMap.has(node)) { | ||
this.tsNodeToESTreeNodeMap.set(node, result); | ||
} | ||
} | ||
} | ||
/** | ||
* Converts a TypeScript node into an ESTree node. | ||
* @param child the child ts.Node | ||
@@ -176,6 +224,9 @@ export class Converter { | ||
result.loc = getLocFor(result.range[0], result.range[1], this.ast); | ||
} | ||
if (result && this.options.shouldProvideParserServices) { | ||
this.esTreeNodeToTSNodeMap.set(result, node); | ||
armano2 marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
} | ||
return result as T; | ||
} | ||
@@ -410,11 +461,7 @@ export class Converter { | ||
break; | ||
} | ||
this.registerTSNodeInNodeMap(node, result); | ||
return result; | ||
} | ||
@@ -715,7 +762,7 @@ export class Converter { | ||
} | ||
// check for exports | ||
returnthis.fixExports(node, result); | ||
} | ||
case SyntaxKind.VariableDeclaration: { | ||
@@ -764,7 +811,7 @@ export class Converter { | ||
} | ||
// check for exports | ||
returnthis.fixExports(node, result); | ||
} | ||
// mostly for for-of, for-in | ||
@@ -1431,7 +1478,7 @@ export class Converter { | ||
} | ||
// check for exports | ||
returnthis.fixExports(node, result); | ||
} | ||
// Modules | ||
@@ -2095,7 +2142,7 @@ export class Converter { | ||
} | ||
// check for exports | ||
returnthis.fixExports(node, result); | ||
} | ||
case SyntaxKind.MethodSignature: { | ||
@@ -2310,7 +2357,7 @@ export class Converter { | ||
result.declare = true; | ||
} | ||
// check for exports | ||
returnthis.fixExports(node, result); | ||
} | ||
case SyntaxKind.FirstTypeNode: { | ||
@@ -2356,7 +2403,7 @@ export class Converter { | ||
result.decorators = node.decorators.map(el => this.convertChild(el)); | ||
} | ||
// ...then check for exports | ||
returnthis.fixExports(node, result); | ||
} | ||
case SyntaxKind.EnumMember: { | ||
@@ -2384,7 +2431,7 @@ export class Converter { | ||
result.global = true; | ||
} | ||
// ...then check for exports | ||
returnthis.fixExports(node, result); | ||
} | ||
// TypeScript specific types | ||
48 changes: 0 additions & 48 deletionspackages/typescript-estree/src/node-utils.ts
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
3 changes: 2 additions & 1 deletionpackages/typescript-estree/src/ts-estree/ts-estree.ts
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
33 changes: 33 additions & 0 deletionspackages/typescript-estree/tests/lib/convert.ts
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
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.