microsoft / microsoft/TypeScript

No supported way to get js doc tags from extraneous comments

Offen
#42,895 7 Kommentare 1 Reaktion 1 zugewiesene Person Auf GitHub ansehen

@sandersn arbeitet bereits daran.

Seit 23.2.2021.

Experience Enhancement Help Wanted Needs Investigation
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

🔎 Search Terms

ts.getJSDocTags
JSDoc tag not found

🕗 Version & Regression Information

Seen in TS 4.0 and 4.1

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about the compiler API
💻 Code

Consider the input file:

/** @foo */

/** @bar */

var x;

As far as I can tell, there's no supported compiler API to access the @foo jsdoc tag. I've resorted to casting ts.Node to {jsDoc?: ts.JSDoc[]}.

🙁 Actual behavior

ts.getJSDocTags and friends only give the @bar JSDoc tag

🙂 Expected behavior

That method, or another, to return both the @foo and @bar tags.

Full compiler code sample to repro
import * as ts from 'typescript';

const filename = 'some_code.ts';
const program = ts.createProgram(['some_code.ts'], {noEmit: true});
program.emit();
const sourceFile = program.getSourceFile(filename);
if (!sourceFile) {
  throw new Error('could not get source file');
}

console.log(`getJSDocTags:`);
console.log([...getAllTags(sourceFile, (node) => ts.getJSDocTags(node))].map(
    (t) => t.tagName.text));
console.log(`Accessing jsDoc directly:`);
console.log(
    [...getAllTags(sourceFile, accessJsDoc)].map((t) => t.tagName.text));

function accessJsDoc(node: ts.Node): ts.JSDocTag[] {
  const tags = [];
  for (const jsDoc of (node as {jsDoc?: ts.JSDoc[]}).jsDoc ?? []) {
    for (const tag of jsDoc.tags ?? []) {
      tags.push(tag);
    }
  }
  return tags;
}

type TagGetter = (node: ts.Node) => Iterable<ts.JSDocTag>;
function getAllTags(
    node: ts.Node, getter: TagGetter, tags = new Set<ts.JSDocTag>()) {
  for (const tag of getter(node)) {
    tags.add(tag);
  }
  ts.forEachChild(node, (child) => {
    getAllTags(child, getter, tags);
  });
  return tags;
}

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.