microsoft / microsoft/TypeScript

LS Document Symbols / Navigation Tree (or Bar) for JSX trees, like object literals

Aperta
#62,417 0 commenti 4 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

### 🔍 Search Terms

jsx symbols navigation tree language feature tsx

### ✅ Viability Checklist

- [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
- [x] This wouldn't change the runtime behavior of existing JavaScript code
- [x] This could be implemented without emitting different JS based on the types of the expressions
- [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- [x] This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- [x] This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals

### ⭐ Suggestion


Image

### 📃 Motivating Example

Microsoft CSS/HTML languages services, Astro, Svelte, Vue language-tools, all have an outline view and breadcrumbs for elements.
Also, JS object literals have them, and JSX is kinda close in shape.

### 💻 Use Cases

1. What do you want to use this for?

VSCode, maybe Monaco TS worker later

2. What shortcomings exist with current approaches?

It is just non-existent, maybe it's a plain overlook, or a design choice, or lack of ressources.

3. What workarounds are you using in the meantime?

I've implemented this, in a TS plugin (Web Elements Analyzer). It takes an original NavigationTree and extends it. It's a quick PoC (seen in the screenshot, it may have bugs).

```tsx
import type { Ts } from '../../../types.js';

export function extendWithJsx(
ts: typeof Ts,
tree: Ts.NavigationTree,
sourceFile: Ts.SourceFile,
): void {
function toSpan(node: Ts.Node, sourceFile: Ts.SourceFile): Ts.TextSpan {
return {
length: node.getEnd() - node.getStart(sourceFile),
start: node.getStart(sourceFile, /* includeJsDocComment */ false),
};
}

function makeJsxTree(
node: Ts.JsxElement | Ts.JsxSelfClosingElement,
sourceFile: Ts.SourceFile,
): Ts.NavigationTree {
const opening: Ts.JsxOpeningLikeElement = ts.isJsxElement(node)
? node.openingElement
: node;

const tagName = opening.tagName.getText(sourceFile);

const features: string[] = [];
for (const attribute of opening.attributes.properties) {
if (!ts.isJsxAttribute(attribute)) continue;
const name = attribute.name.getText(sourceFile);

if (
name === 'id' &&
attribute.initializer &&
ts.isStringLiteral(attribute.initializer)
) {
features.push(`#${attribute.initializer.text}`);
}
if (
(name === 'className' || name === 'class') &&
attribute.initializer &&
ts.isStringLiteral(attribute.initializer)
) {
features.push(
...attribute.initializer.text
.split(/\s+/)
.filter(Boolean)
.map((c) => `.${c}`),
);
}
if (
(name === 'part' || name === 'name') &&
attribute.initializer &&
ts.isStringLiteral(attribute.initializer)
) {
features.push(`[${name}=${attribute.initializer.text}]`);
}
}

const children: Ts.NavigationTree[] = [];
if (ts.isJsxElement(node)) {
for (const child of node.children) {
if (ts.isJsxElement(child) || ts.isJsxSelfClosingElement(child)) {
children.push(makeJsxTree(child, sourceFile));
}
}
}

return {
childItems: children,
kind: ts.ScriptElementKind.variableElement,
kindModifiers: '',
nameSpan: {
length: opening.tagName.getWidth(sourceFile),
start: opening.tagName.getStart(sourceFile),
},
spans: [toSpan(node, sourceFile)],
text: `${tagName}${features.join('')}`,
};
}

function attachJsx(nav: Ts.NavigationTree, node: Ts.Node): void {
node.forEachChild((child) => {
if (ts.isJsxElement(child) || ts.isJsxSelfClosingElement(child)) {
(nav.childItems ??= []).push(makeJsxTree(child, sourceFile));
return;
}
const matchingNav = findNavForSpan(nav, child, sourceFile);
if (matchingNav) attachJsx(matchingNav, child);
else attachJsx(nav, child);
});
}

attachJsx(tree, sourceFile);
}

function findNavForSpan(
nav: Ts.NavigationTree,
node: Ts.Node,
sourceFile: Ts.SourceFile,
): Ts.NavigationTree | undefined {
const start = node.getStart(sourceFile);
const end = node.getEnd();
return nav.childItems?.find((c) =>
c.spans.some((s) => s.start <= start && end <= s.start + s.length),
);
}

```

- `.class-1.class-2#unique-id[part=unique][name=unique]`
- Accepts both class/className (HTML, Solid, custom JSX runtimes…)

Image

Class / ID / Part / Name are nice for uniqueness, frequency, relevance, greppability.
Everything is displayed as a CSS selector, _à-la_ browser DevTools.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

The issue names no repository files or tests; start by reviewing the language-service NavigationTree API and the provided extendWithJsx and attachJsx proof of concept. Done means JSX elements appear in document symbols and navigation trees with nested structure and the proposed identifying labels, with behavior validated in the relevant language-service consumers.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript, typescript
Ambito
developer-experience, tooling
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.