Hacktoberfest 2026:維護者為十月標記出來的 issue,仍然開放、適合新手。 瀏覽 Hacktoberfest issue

Type after `as` or `satisfies` at end of line is tokenized as an expression

未關閉
#1,079 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
4/5
預估耗時
3-5 天
新手友好度
68/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
typescript
領域
tooling

研究方向

Start with TypeScript.YAML-tmLanguage around lines 1755-1761 and line 62, then run repro.mjs with the listed vscode-textmate and vscode-oniguruma versions. Compare scopes for the inline and multiline as and satisfies examples; done means the next-line type receives the same type scopes without breaking the semicolonless single-line case.

由索引模型根據 Issue 內容生成。

描述

When a line ends with as or satisfies, the type on the next line gets expression scopes instead of type scopes.

JavaScript and TypeScript Nightly version: not tested in the extension. Reproduced with TypeScript.tmLanguage at eeeb0dc4daa8793b8227bb3b34ddfc267c85fd81 (current master), vscode-textmate 9.3.2, vscode-oniguruma 2.0.1, Node 26.7.0, macOS arm64.

Code

const a = b as A | undefined

const a = b as
  A | undefined

const a = b satisfies
  A | undefined

Prettier 3.9.8 emits this layout when an as expression exceeds the print width, e.g.:

const inlineSourceMap = convertSourceMap.fromSource(content)?.toObject() as
  SourceMap | undefined

Actual

Token Same line Next line
A entity.name.type.ts variable.other.constant.ts
| keyword.operator.type.ts keyword.operator.bitwise.ts
undefined support.type.builtin.ts constant.language.undefined.ts

A type on the line after satisfies gets the same scopes as a type on the line after as.

Expected

The same scopes on both layouts, because TypeScript parses both as a type assertion with the type A | undefined.

Repro script
npm install [email protected] [email protected]
curl -sLO https://raw.githubusercontent.com/microsoft/TypeScript-TmLanguage/eeeb0dc4daa8793b8227bb3b34ddfc267c85fd81/TypeScript.tmLanguage
node repro.mjs
// repro.mjs
import { readFileSync } from "node:fs";
import { createRequire } from "node:module";
import vsctm from "vscode-textmate";
import oniguruma from "vscode-oniguruma";

const require = createRequire(import.meta.url);
await oniguruma.loadWASM(
  readFileSync(require.resolve("vscode-oniguruma/release/onig.wasm")).buffer,
);
const registry = new vsctm.Registry({
  onigLib: Promise.resolve({
    createOnigScanner: (patterns) => new oniguruma.OnigScanner(patterns),
    createOnigString: (s) => new oniguruma.OnigString(s),
  }),
  loadGrammar: async () =>
    vsctm.parseRawGrammar(
      readFileSync("TypeScript.tmLanguage", "utf8"),
      "TypeScript.tmLanguage",
    ),
});
const grammar = await registry.loadGrammar("source.ts");

const tokenize = (code) => {
  let state = vsctm.INITIAL;
  for (const line of code.split("\n")) {
    const { tokens, ruleStack } = grammar.tokenizeLine(line, state);
    state = ruleStack;
    for (const { startIndex, endIndex, scopes } of tokens) {
      const text = line.slice(startIndex, endIndex);
      if (text.trim())
        console.log(JSON.stringify(text).padEnd(10), scopes.at(-1));
    }
  }
  console.log();
};

tokenize("const a = b as A | undefined");
tokenize("const a = b as\n  A | undefined");
tokenize("const a = b satisfies\n  A | undefined");

Possible cause (unverified)

The as/satisfies rule's end includes $ through lookAheadEndOfType, so the type ends at the line break right after the keyword:

https://github.com/microsoft/TypeScript-TmLanguage/blob/eeeb0dc4daa8793b8227bb3b34ddfc267c85fd81/TypeScript.YAML-tmLanguage#L1755-L1761
https://github.com/microsoft/TypeScript-TmLanguage/blob/eeeb0dc4daa8793b8227bb3b34ddfc267c85fd81/TypeScript.YAML-tmLanguage#L62

The $ is presumably what ends the type at the end of a line without a semicolon (x as T followed by a new statement), so dropping it could break that case.

主要語言
TypeScript
星號
471
分支
150
PR 合併指標
30 天內沒有已合併 PR

環境準備

這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

microsoft/TypeScript-TmLanguage 的其他 Issue

查看 microsoft/TypeScript-TmLanguage 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。