Skip to content

feat: support offset mapping for preprocessed SFC templates - #1537

Open
vis97c wants to merge 1 commit into
denoland:mainfrom
vis97c:main
Open

vis97c wants to merge 1 commit into
denoland:mainfrom
vis97c:main

Conversation

@vis97c

@vis97c vis97c commented Jul 29, 2026 •

Copy link
Copy Markdown

Addresses: #1463

Alongside denoland/deno#36345 this PR is intended to bring linting to other extensions (.vue, .svelte, etc...) through an extended plugins api. Following a similar approach to eslint. The deno Pr is required for this one to work properly. We could probably expect some issue with generics.

For the sake of transparency, gemini models (3.6 flash) were used during the development and testing of the changes.

AI summary

This change adds support for translating diagnostic ranges and fixes back to their parent file coordinates, which is essential when linting extracted code blocks from multi-format files (like Vue or Svelte Single File Components).

  • SourceMapping Structure (src/linter.rs): Introduced the SourceMapping struct containing the parent template's original_source code and the byte_offset where the script segment begins.
  • Linter API Extension: Updated LintFileOptions, Linter::lint_file, and Linter::lint_with_ast to accept an optional SourceMapping instance.
  • Position & Range Translation: When mapping information is provided, diagnostic positions (start/end ranges) and internal fixer change spans are shifted by the byte_offset value, and the diagnostic's text_info is replaced with the parent file's text structure.
// my-vue-plugin.ts
export default {
  name: "vue-plugin",
  rules: {
    // Custom rules...
  },
  processors: {
    ".vue": {
      // Preprocess is called to extract the script blocks from a Vue SFC file.
      preprocess(text, filename) {
        // Match the <script> tag attributes and its inner contents
        const match = text.match(/<script([^>]*)>([\s\S]*?)<\/script>/);

        if (match) {
          const attributes = match[1];
          const scriptCode = match[2];
          
          // Check if TypeScript is specified in the script attributes
          const isTypeScript = attributes.includes('lang="ts"') || attributes.includes("lang='ts'");
          
          // Generate the virtual filename with the correct extension
          const virtualFilename = isTypeScript ? `${filename}.ts` : `${filename}.js`;

          // Return the code block to be linted with the matching extension
          return [{ text: scriptCode, filename: virtualFilename }];
        }
        
        // Return an empty array if no <script> block is present in the file
        return [];
      },
      // Postprocess is called to filter, merge, or adjust diagnostics.
      postprocess(diagnostics, filename) {
        // diagnostics is a 2D array: Vec<Vec<SerializableLintDiagnostic>>
        // We flatten the list to return a 1D array of diagnostics
        return diagnostics.flat();
      }
    },
    ".svelte": {
      // Further processing...
    },
  }
};
// deno.json
{
  "lint": {
    "plugins": ["./my-vue-plugin.ts"]
  }
}

@CLAassistant

CLAassistant commented Jul 29, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

This branch has not been deployed

No deployments
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.

2 participants