-
Notifications
You must be signed in to change notification settings - Fork 681
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
Reduce reliance on internal TS apis #949
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import * as ts from 'typescript'; | ||
import * as _ts from '../../ts-internal'; | ||
import { toArray } from 'lodash'; | ||
|
||
import { Comment, CommentTag } from '../../models/comments/index'; | ||
|
||
|
@@ -32,14 +32,7 @@ export function createComment(node: ts.Node): Comment | undefined { | |
* @return TRUE if the given node is the topmost module declaration, FALSE otherwise. | ||
*/ | ||
function isTopmostModuleDeclaration(node: ts.ModuleDeclaration): boolean { | ||
if (node.nextContainer && node.nextContainer.kind === ts.SyntaxKind.ModuleDeclaration) { | ||
let next = <ts.ModuleDeclaration> node.nextContainer; | ||
if (node.name.end + 1 === next.name.pos) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
return node.getChildren().some(ts.isModuleBlock); | ||
} | ||
|
||
/** | ||
|
@@ -65,6 +58,29 @@ function getRootModuleDeclaration(node: ts.ModuleDeclaration): ts.Node { | |
return node; | ||
} | ||
|
||
/** | ||
* Derived from the internal ts utility | ||
* https://github.com/Microsoft/TypeScript/blob/v3.2.2/src/compiler/utilities.ts#L954 | ||
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. Thanks for including the source link |
||
* @param node | ||
* @param text | ||
*/ | ||
function getJSDocCommentRanges(node: ts.Node, text: string): ts.CommentRange[] { | ||
const hasTrailingCommentRanges = [ | ||
ts.SyntaxKind.Parameter, | ||
ts.SyntaxKind.FunctionExpression, | ||
ts.SyntaxKind.ArrowFunction, | ||
ts.SyntaxKind.ParenthesizedExpression | ||
].includes(node.kind); | ||
|
||
let commentRanges = toArray(ts.getLeadingCommentRanges(text, node.pos)); | ||
if (hasTrailingCommentRanges) { | ||
commentRanges = toArray(ts.getTrailingCommentRanges(text, node.pos)).concat(commentRanges); | ||
} | ||
|
||
// True if the comment starts with '/**' but not if it is '/**/' | ||
return commentRanges.filter(({ pos }) => text.substr(pos, 3) === '/**' && text[pos + 4] !== '/'); | ||
} | ||
|
||
/** | ||
* Return the raw comment string for the given node. | ||
* | ||
|
@@ -82,9 +98,9 @@ export function getRawComment(node: ts.Node): string | undefined { | |
} | ||
} | ||
|
||
const sourceFile = _ts.getSourceFileOfNode(node); | ||
const comments = _ts.getJSDocCommentRanges(node, sourceFile.text); | ||
if (comments && comments.length) { | ||
const sourceFile = node.getSourceFile(); | ||
const comments = getJSDocCommentRanges(node, sourceFile.text); | ||
if (comments.length) { | ||
let comment: ts.CommentRange; | ||
if (node.kind === ts.SyntaxKind.SourceFile) { | ||
if (comments.length === 1) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import * as ts from 'typescript'; | ||
import * as _ts from '../../ts-internal'; | ||
|
||
import { Reflection } from '../../models/index'; | ||
import { Context } from '../context'; | ||
|
@@ -24,7 +23,7 @@ export class VariableStatementConverter extends ConverterNodeComponent<ts.Variab | |
convert(context: Context, node: ts.VariableStatement): Reflection { | ||
if (node.declarationList && node.declarationList.declarations) { | ||
node.declarationList.declarations.forEach((variableDeclaration) => { | ||
if (_ts.isBindingPattern(variableDeclaration.name)) { | ||
if (ts.isArrayBindingPattern(variableDeclaration.name) || ts.isObjectBindingPattern(variableDeclaration.name)) { | ||
this.convertBindingPattern(context, variableDeclaration.name); | ||
} else { | ||
this.owner.convertNode(context, variableDeclaration); | ||
|
@@ -42,10 +41,14 @@ export class VariableStatementConverter extends ConverterNodeComponent<ts.Variab | |
* @param node The binding pattern node that should be analyzed. | ||
*/ | ||
convertBindingPattern(context: Context, node: ts.BindingPattern) { | ||
(node.elements as ts.NodeArray<ts.BindingElement>).forEach((element: ts.BindingElement) => { | ||
node.elements.forEach((element) => { | ||
this.owner.convertNode(context, element); | ||
|
||
if (_ts.isBindingPattern(element.name)) { | ||
if (!ts.isBindingElement(element)) { | ||
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 additional check is required since the internal 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. Would it be better to inline this with the if block below? |
||
return; | ||
} | ||
|
||
if (ts.isArrayBindingPattern(element.name) || ts.isObjectBindingPattern(element.name)) { | ||
this.convertBindingPattern(context, element.name); | ||
} | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ export class VariableConverter extends ConverterNodeComponent<ts.VariableDeclara | |
|
||
/** | ||
* Analyze the given variable declaration node and create a suitable reflection. | ||
* TODO: the type of `node` is incorrect, it should be a union of ts.PropertySignature | ts.PropertyDeclaration | ... | ||
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. Leaving resolving this to a later PR since changing to the actual type caused quite a few type errors. |
||
* | ||
* @param context The context object describing the current state the converter is in. | ||
* @param node The variable declaration node that should be analyzed. | ||
|
@@ -50,9 +51,9 @@ export class VariableConverter extends ConverterNodeComponent<ts.VariableDeclara | |
|
||
let name: string | undefined; | ||
let isBindingPattern: boolean; | ||
if (_ts.isBindingPattern(node.name)) { | ||
if (node['propertyName']) { | ||
name = _ts.declarationNameToString(node['propertyName']); | ||
if (ts.isArrayBindingPattern(node.name) || ts.isObjectBindingPattern(node.name)) { | ||
if (ts.isBindingElement(node) && node.propertyName) { | ||
name = node.propertyName.getText(); | ||
isBindingPattern = true; | ||
} else { | ||
return; | ||
|
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.
Toss
namespace A.B.C {}
into https://ts-ast-viewer.com/ and it's immediately obvious that a module declaration is "topmost" if it has a module block as an immediate child.