-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
[Salsa] Support @typedef for jsdoc #8103
Conversation
…as hydrated with jsdocComment nodes.
… typedefForJsdoc # Conflicts: # src/compiler/diagnosticMessages.json # src/services/services.ts
@RyanCavanaugh @vladima @mhegazy comments? |
@@ -342,6 +342,9 @@ namespace ts { | |||
JSDocReturnTag, | |||
JSDocTypeTag, | |||
JSDocTemplateTag, | |||
JSDocTypedefTag, | |||
JSDocPropertyTag, | |||
JSDocTypeLiteral, |
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.
question: what is the JSDocTypeLiteral?
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.
A @typedef
is similar to a type alias in TS. In TS you can give an alias to an object type literal:
type T = { name: string, age: number}
which is equivalent to the following jsdoc:
/**
* @typedef {Object} T
* @property {string} name
* @property {number} age
*/
So I defined the @property
s as JSDocTypeLiteral
, as it functions like object type literal, but with a different shape.
Adding cross-reference to #7758 |
@@ -269,20 +275,36 @@ namespace ts { | |||
scanner.setText((sourceFile || this.getSourceFile()).text); | |||
children = []; | |||
let pos = this.pos; | |||
const useJSDocScanner = |
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.
Why don't u check like this : const useJSDocScanner = isJSDoc(this)
? actaully question: why you don't use JSDocScanner
on other JSDoc that is not listed here?
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.
JSDocScanner
limits scanning to several allowed characters, while disallowed characters are all scanned as unknown tokens. JSDoc nodes include JSDoc type nodes and JSDoc type expression nodes, and if the current position is inside a JSDoc type expression, it should allow all characters, because that part is essentially the same as code. For example, the <
and >
are not allowed in JSDoc tokens, they are fine when used with generics in JSDoc type expression.
What's the perf impact of parsing the extra comments? Any? |
if (includeJsDocComment) { | ||
const jsDocChildren = ts.filter(current.getChildren(), isJSDocNode); | ||
for (const jsDocChild of jsDocChildren) { | ||
let start = allowPositionInLeadingTrivia ? jsDocChild.getFullStart() : jsDocChild.getStart(sourceFile, includeJsDocComment); |
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.
Should this be "const"
I ran the following sample for the perf bench mark: The Setup:In an empty folder, do
(I chose ember-cli because it is big enough to see the bind/parsing time difference)
The bench mark script is: Write-Output "TSC runs:"
0..10 | % {tsc -p ".\jsconfig.json" --noEmit --diagnostics | Select-String "Bind Time|Parse Time"}
Write-Output "PR runs:"
0..10 | % { node --max-old-space-size=2000 "C:\Users\lizhe\Documents\github\TypeScript\built\local\tsc.js" -p ".\jsconfig.json" --noEmit --diagnostics | Select-String "Bind Time|Parse Time"} The Result: |
… typedefForJsdoc # Conflicts: # src/services/services.ts
So it turns out that my previous benchmark wasn't very accurate at all, for several reasons:
I rerun the benchmarks with these factors fixed. The new environment is:
c) use node for both cases, the new script is: Param($count)
Write-Output "PR runs:"
0..($count - 1) | % { node --max-old-space-size=2000 "C:\Users\lan\Documents\github\TypeScript\built\local\tsc.js" -p ".\jsconfig.json" --noEmit --diagnostics | Select-String "Bind Time"}
Write-Output "Master runs:"
0..($count - 1) | % { node --max-old-space-size=2000 "C:\Program Files\nodejs\node_modules\typescript\lib\tsc.js" -p ".\jsconfig.json" --noEmit --diagnostics | Select-String "Bind Time" } d) use the latest nightly and merge the PR with master locally. Checked the diff of two And the benchmark result shows that the PR is not slower in binding time: |
👍 especially if it's somehow faster 😉 |
… typedefForJsdoc # Conflicts: # src/services/services.ts # tests/cases/unittests/jsDocParsing.ts
… typedefForJsdoc # Conflicts: # src/services/utilities.ts
@@ -1510,6 +1516,25 @@ namespace ts { | |||
typeExpression: JSDocTypeExpression; | |||
} | |||
|
|||
// @kind(SyntaxKind.JSDocTypedefTag) | |||
export interface JSDocTypedefTag extends JSDocTag, Declaration { | |||
name: Identifier; |
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.
name, typeExpresion and type should be optional.
I would add a navigate to test for typedefs |
👍 |
#8849 touches the same areas, can you merge this in after that ones goes in. |
…Script into typedefForJsdoc # Conflicts: # src/compiler/binder.ts
… typedefForJsdoc
This PR adds support for the
@typedef
JSDoc tag. The current implementation includes the following formats:Format 1:
/** @typedef { [Type Expression] } [Type Name] */
Format 2:
Format 3:
Format 4:
Current the "Type name" can only be an identifier. Dotted names (qualified names) support may be added in future updates. The types defined by the
@typedef
tag are the same as type aliases in TypeScript, and they have scopes as well.Basic support in language services like go to definition and rename is added as well.
Fixes #6976