Skip to content

improve stripInternal with inline comments #23611

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 14 commits into from
Mar 12, 2019

Conversation

Kingwl
Copy link
Contributor

@Kingwl Kingwl commented Apr 22, 2018

Fixes #23589

@mhegazy
Copy link
Contributor

mhegazy commented Apr 23, 2018

@weswigham can you please review.

@@ -758,6 +758,10 @@ namespace ts {
return node.kind !== SyntaxKind.JsxText ? getLeadingCommentRanges(sourceFileOfNode.text, node.pos) : undefined;
}

export function getTrailingCommentRangesOfNode(node: Node, sourceFileOfNode: SourceFile) {
Copy link
Member

@weswigham weswigham Apr 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is misleadingly named - the trailing comment ranges of the node would be the trailing comment ranges of the end of the node - this is the trailing comment ranges working backwards from the beginning of the node - which are actually the trailing comment ranges of the end of the prior token. Also, consider the quite crazily formatted:

class A {
   constructor(
        public foo: string, /* @internal */
        // This next one
        public bar: number,
  ) {}
}

I don't think that should count as an internal annotation for the second parameter declaration, due to the intervening single-line comment. (On a related note, I'd love to see a few more tests for some odd internal comment locations around parameter properties (esp. mixed with single-line comments) with this change).

@Kingwl
Copy link
Contributor Author

Kingwl commented Apr 25, 2018

need some help:
don't understand why APISample test is borken

@weswigham
Copy link
Member

Looks like our declaration file output has errors in it (we use @internal ourselves, which probably means either some things printed that shouldn't or didn't that should), which causes the sample tests to break, in addition to the API ones.

Copy link
Member

@weswigham weswigham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should move all the @internals back to where they were and ensure there's no changes to typescript.d.ts or typescriptserver.d.ts, too. We don't wanna change current behavior.

// A name that supports late-binding (used in checker)
/* @internal */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought, changing the logic such that the @internal annotation must be immediately preceding the node isn't a change we probably want to make lightly; I'd keep the old behavior if you're not looking for an inline annotation.

let pendingPos: number;
let pendingEnd: number;
let pendingKind: CommentKind;
let pendingHasTrailingNewLine: boolean;
let hasPendingCommentRange = false;
let collecting = trailing || pos === 0;
let collecting = inline || trailing || pos === 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't change this. I assure you, though the terminology is confusing, it is at least consistent. What you want to change instead is the position you begin scanning from, waaaaay up the callstack closer to shouldStripInternal. Rather than looking for the node's leading or trailing comments, you want the trailing comments of the first non-trivia position before the node. For example, in:

(/* whatever*/ Foo);

The /*whatever*/ comment is neither a leading nor trailing comment of the Foo identifier. Nor is is a leading or trailing comment of the parenthesized expression/argument list. What it is is a trailing comment of the ( - the OpenParen token. This token doesn't actually exist in our AST, so recovering comments on it can be difficult. But in this case it's not hard - you just want the trailing comments of the argument list start for the first parameter, or the trailing comments of the end of the previous parameter + skip trivia length + 1 if it's not the first param. Our emitter has a bunch of code that does similar stuff to preserve comments on these tokens - checkout the emitTrailingCommentsOfPosition and emitLeadingCommentsOfPosition calls in emitNodeList in emitter.ts.

@@ -7658,7 +7658,6 @@ declare namespace ts.server {
*/
type ProjectRoot = ScriptInfo | NormalizedPath;
abstract class Project implements LanguageServiceHost, ModuleResolutionHost {
readonly projectName: string;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems something has been lgnored

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, looks exactly like an instance of the bug you're fixing that we never noticed :D

@Kingwl
Copy link
Contributor Author

Kingwl commented Jun 18, 2018

⬆️

@RyanCavanaugh
Copy link
Member

@weswigham & @rbuckton 👍 / 👎 ?

@RyanCavanaugh RyanCavanaugh removed this from the TypeScript 3.3 milestone Feb 1, 2019
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.4.0 milestone Feb 1, 2019
@weswigham weswigham merged commit bd27296 into microsoft:master Mar 12, 2019
@Kingwl Kingwl deleted the fix-stripInternal branch March 13, 2019 02:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants