manage-bans

作者: microsoft

创建并管理禁止的AST模式,以防止特定的代码结构。当用户想要禁止某个代码模式、添加tree-sitter lint规则或……时使用。

npx skills add https://github.com/microsoft/vscode-team-kit --skill manage-bans

Managing Banned AST Patterns

Ban rules are discovered from two sources, both checked by the preToolUse hook:

  1. $HOME/.BANNED_AST.md — global rules that apply to all edits, regardless of project.
  2. BANNED_AST.md files in any parent directory of the edited file — can contain multiple rules, scoped to that subtree.

Source 1: Global Ban File ($HOME/.BANNED_AST.md)

Place a BANNED_AST.md in your home directory to define rules that apply globally to every edit. It uses the same multi-rule format as directory-scoped files:

---
name: no-eval
message: "Do not use eval(). It poses a security risk and should be replaced with safer alternatives."
---

(call_expression
  function: (identifier) @fn
  (#eq? @fn "eval"))

Creating a Global Ban

  1. Open or create ~/.BANNED_AST.md.
  2. Add a rule block with --- frontmatter containing name and message, followed by --- and the Tree Sitter query.
  3. Multiple rules can be stacked in the same file, each separated by a new frontmatter block.
  4. Validate the rule — see Validating Rules below.

Source 2: BANNED_AST.md Files (Directory-Scoped)

Place a BANNED_AST.md file in any directory to ban patterns for all files at or below that directory. The hook walks up from each edited file's directory to the filesystem root, collecting rules from every BANNED_AST.md it finds.

A single BANNED_AST.md can contain multiple rules, each separated by its own frontmatter block:

---
name: no-eval
message: "Do not use eval(). It poses a security risk."
---

(call_expression
  function: (identifier) @fn
  (#eq? @fn "eval"))

---
name: no-console-log
message: "Avoid console.log() in production code."
---

(call_expression
  function: (member_expression
    object: (identifier) @obj
    property: (property_identifier) @prop)
  (#eq? @obj "console")
  (#eq? @prop "log"))

Each rule section starts with --- frontmatter containing name and message, followed by ---, then the Tree Sitter query body. The next --- begins the next rule.

When to Use Which

  • ~/.BANNED_AST.md — personal global bans that apply everywhere regardless of file location.
  • BANNED_AST.md — scoped bans for subtrees (e.g. ban any in src/ but allow it in tests/).

When both sources define a rule with the same name, the BANNED_AST.md closer to the edited file takes precedence.

Rule Format

Frontmatter Fields

  • name (required): A unique identifier for this ban (lowercase, hyphens ok). This is used in justification comments (<name> justification: ...).
  • message (required): The rejection message shown when this pattern is detected. Should explain WHY the pattern is banned and suggest alternatives.

Body

The body contains a Tree Sitter query that matches the banned AST nodes. These use S-expression syntax with optional predicates like #eq? and #match?.

Examples

Global ban file (~/.BANNED_AST.md)

---
name: no-eval
message: "Do not use eval(). It poses a security risk. Use Function constructor or a sandboxed interpreter instead."
---

(call_expression
  function: (identifier) @fn
  (#eq? @fn "eval"))

Multi-rule BANNED_AST.md

Place this in a project directory to ban multiple patterns for all files below it:

---
name: no-console-log
message: "Avoid console.log() in production code. Use a structured logging framework instead."
---

(call_expression
  function: (member_expression
    object: (identifier) @obj
    property: (property_identifier) @prop)
  (#eq? @obj "console")
  (#eq? @prop "log"))

---
name: no-any-type
message: "Do not use the 'any' type. Use 'unknown' or a concrete type instead."
---

(predefined_type) @type
(#eq? @type "any")

Validating Rules

After writing a rule, always validate it using the validate-rule.mts script before finishing. This catches query syntax errors and confirms the rule matches the intended patterns — and only those patterns.

node ban-ast/scripts/validate-rule.mts \
  --lang ts \
  --query '<your-tree-sitter-query>' \
  --should-match '<code that should be flagged>' \
  --should-not-match '<code that should be allowed>'
  • --lang — file extension for the language (default: ts). Supported: ts, js, tsx, py, rs, go, c, cpp, cs, java, rb, and more.
  • --query — the Tree Sitter S-expression query from the rule body.
  • --should-match — a code snippet that must trigger the rule. Repeat for multiple cases.
  • --should-not-match — a code snippet that must not trigger the rule. Repeat for multiple cases.

If no --should-match / --should-not-match flags are given, the script only checks that the query is syntactically valid.

The script exits with code 1 if any test fails, so you can see immediately when a rule needs to be revised.

Example

node ban-ast/scripts/validate-rule.mts \
  --lang ts \
  --query '(call_expression function: (identifier) @fn (#eq? @fn "eval"))' \
  --should-match 'eval("code")' \
  --should-not-match 'foo("code")'

Expected output:

PASS [should-match]:     "eval(\"code\")"
PASS [should-not-match]: "foo(\"code\")"

2 test(s): 2 passed, 0 failed.

Justification Override

If a banned pattern is strictly necessary, include a justification comment in the code to bypass the ban for that specific instance:

// <no-eval> justification: required for dynamic plugin loading
const result = eval(expression);

The hook checks for <rule-name> justification: <non-empty reason> anywhere in the new code. If found, that rule is not enforced for that edit. The reason must be non-empty to ensure overrides are intentional and documented.

来自 microsoft 的更多技能

oss-growth
microsoft
OSS增长黑客角色
agent-framework-azure-ai-py
microsoft
使用Microsoft Agent Framework Python SDK(agent-framework-azure-ai)构建Azure AI Foundry代理。在创建使用AzureAIAgentsProvider的持久化代理、使用托管工具(代码解释器、文件搜索、网络搜索)、集成MCP服务器、管理对话线程或实现流式响应时使用。涵盖函数工具、结构化输出和多工具代理。
development
airunway-aks-setup
microsoft
在AKS上设置AI Runway——从裸集群到运行模型。涵盖集群验证、控制器安装、GPU评估、提供商设置和首次部署。适用场景:“设置AI Runway”、“接入AKS集群”、“安装AI Runway”、“airunway设置”、“将模型部署到AKS”、“在AKS上进行GPU推理”、“在AKS上配置KAITO”、“在AKS上运行LLM”、“在AKS上使用vLLM”、“在AKS上设置模型服务”、“AI Runway控制器”。
devops
appinsights-instrumentation
microsoft
使用Azure Application Insights对Web应用进行插桩的指南。提供遥测模式、SDK设置和配置参考。适用场景:如何对应用进行插桩、App Insights SDK、遥测模式、什么是App Insights、Application Insights指南、插桩示例、APM最佳实践。
devops
applicationinsights-web-ts
microsoft
使用Application Insights JavaScript SDK(@microsoft/applicationinsights-web)为浏览器/Web应用添加检测。用于真实用户监控(RUM)——页面视图、点击、AJAX/fetch依赖项、异常、自定义事件,以及与后端OpenTelemetry追踪关联的浏览器端GenAI代理追踪。涵盖SDK加载器脚本和npm设置、框架扩展(React、React Native、Angular)、点击分析、遥测初始化器,以及从浏览器发出的代理/工具/模型跨度所遵循的OTel GenAI语义约定。
devops
azure-ai-anomalydetector-java
microsoft
使用适用于 Java 的 Azure AI 异常检测器 SDK 构建异常检测应用程序。在实现单变量/多变量异常检测、时间序列分析或 AI 驱动的监控时使用。
development
azure-ai-language-conversations-py
microsoft
使用azure-ai-language-conversations Python SDK实现对话语言理解(CLU)。当使用ConversationAnalysisClient分析对话意图和实体、构建NLP功能或将语言理解集成到应用程序中时使用。
development
azure-ai-ml-py
microsoft
Azure Machine Learning SDK v2 for Python。用于机器学习工作区、作业、模型、数据集、计算资源和管道。 触发词:“azure-ai-ml”、“MLClient”、“工作区”、“模型注册表”、“训练作业”、“数据集”。
development