|
| 1 | +import type { Linter, ESLint } from 'eslint'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Represents a file in an LWC bundle, with its content and designation as the primary file, |
| 5 | + * i.e. the file requested by ESLint to be linted. |
| 6 | + */ |
| 7 | +export type LwcBundleFile = { |
| 8 | + /** The name of the file */ |
| 9 | + filename: string; |
| 10 | + /** The content of the file */ |
| 11 | + content: string; |
| 12 | + /** Whether this is the primary file being linted by ESLint */ |
| 13 | + isPrimary: boolean; |
| 14 | +}; |
| 15 | + |
| 16 | +/** |
| 17 | + * Represents a bundle of files that make up an LWC component. The bundle contains the JavaScript |
| 18 | + * file and any HTML template files that comprise the component. One file in the bundle will be |
| 19 | + * designated as the primary file, which is the file that ESLint is currently processing. |
| 20 | + */ |
| 21 | +declare class LwcBundle { |
| 22 | + /** |
| 23 | + * Creates a new LWC bundle instance |
| 24 | + * |
| 25 | + * @param componentBaseName - The base name of the component (e.g., 'myComponent' for 'myComponent.js' et al) |
| 26 | + * @param js - The JavaScript file of the bundle, if present |
| 27 | + * @param htmlTemplates - The HTML template files of the bundle, if present |
| 28 | + */ |
| 29 | + constructor(componentBaseName: string, js?: LwcBundleFile, htmlTemplates?: LwcBundleFile[]); |
| 30 | + |
| 31 | + /** Gets the base name of the component */ |
| 32 | + get componentBaseName(): string; |
| 33 | + |
| 34 | + /** Gets the JavaScript file of the bundle, if present */ |
| 35 | + get js(): LwcBundleFile | undefined; |
| 36 | + |
| 37 | + /** Gets the HTML template files of the bundle, if present */ |
| 38 | + get htmlTemplates(): LwcBundleFile[] | undefined; |
| 39 | + |
| 40 | + /** Gets the primary file being linted by ESLint */ |
| 41 | + get primaryFile(): LwcBundleFile; |
| 42 | + |
| 43 | + /** |
| 44 | + * Creates a record of filenames to their content for Komaci analysis |
| 45 | + * |
| 46 | + * @returns A record mapping filenames to their content |
| 47 | + */ |
| 48 | + filesRecord(): Record<string, string>; |
| 49 | + |
| 50 | + /** |
| 51 | + * Sets the primary file in the bundle based on content matching |
| 52 | + * |
| 53 | + * @param content - The content to match against |
| 54 | + * @returns True if a matching file was found and set as primary |
| 55 | + */ |
| 56 | + setPrimaryFileByContent(content: string): boolean; |
| 57 | + |
| 58 | + /** |
| 59 | + * Creates an `LwcBundle` instance from the specified content files. The primary file |
| 60 | + * designation will be set later when the bundle is processed by ESLint. |
| 61 | + * |
| 62 | + * @param componentBaseName - The base name of the component |
| 63 | + * @param jsContent - The content of the JavaScript file, if present |
| 64 | + * @param htmlTemplateContent - The contents of the HTML template files, if any |
| 65 | + * @returns A new LWC bundle instance |
| 66 | + */ |
| 67 | + static lwcBundleFromContent( |
| 68 | + componentBaseName: string, |
| 69 | + jsContent?: string, |
| 70 | + ...htmlTemplateContent: string[] |
| 71 | + ): LwcBundle; |
| 72 | + |
| 73 | + /** |
| 74 | + * Creates a bundle from filesystem files |
| 75 | + * |
| 76 | + * @param lwcFileContent - The content of the file being processed |
| 77 | + * @param lwcFilePath - The path to the file being processed |
| 78 | + * @param lwcFileExtension - The file extension of the file being processed |
| 79 | + * @returns A new LWC bundle instance, or null if the file cannot be found |
| 80 | + */ |
| 81 | + static lwcBundleFromFilesystem( |
| 82 | + lwcFileContent: string, |
| 83 | + lwcFilePath: string, |
| 84 | + lwcFileExtension: string |
| 85 | + ): LwcBundle | null; |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * ESLint processor that analyzes LWC bundles. This will set up the LWC bundle to be processed |
| 90 | + * by Komaci. |
| 91 | + */ |
| 92 | +export class BundleAnalyzer implements Linter.Processor { |
| 93 | + /** Gets the current LWC bundle being processed */ |
| 94 | + get lwcBundle(): LwcBundle | null; |
| 95 | + |
| 96 | + /** Sets the LWC bundle to be processed */ |
| 97 | + set lwcBundle(value: LwcBundle | null); |
| 98 | + |
| 99 | + /** |
| 100 | + * Sets the LWC bundle content from content files |
| 101 | + * |
| 102 | + * @param componentBaseName - The base name of the component |
| 103 | + * @param jsContent - The content of the JavaScript file, if present |
| 104 | + * @param htmlTemplateContent - The contents of the HTML template files, if any |
| 105 | + */ |
| 106 | + setLwcBundleFromContent( |
| 107 | + componentBaseName: string, |
| 108 | + jsContent?: string, |
| 109 | + ...htmlTemplateContent: string[] |
| 110 | + ): void; |
| 111 | + |
| 112 | + /** |
| 113 | + * Preprocesses the input file for ESLint |
| 114 | + * |
| 115 | + * @param text - The content of the ESLint file |
| 116 | + * @param filename - The input filename |
| 117 | + * @returns An array of files and their content to be processed |
| 118 | + */ |
| 119 | + preprocess(text: string, filename: string): string[]; |
| 120 | + |
| 121 | + /** |
| 122 | + * Postprocesses the linting results |
| 123 | + * |
| 124 | + * @param messages - The two-dimensional array of messages returned from linting |
| 125 | + * @param filename - The input filename |
| 126 | + * @returns A one-dimensional flattened array of messages |
| 127 | + */ |
| 128 | + postprocess(messages: Linter.LintMessage[][], filename: string): Linter.LintMessage[]; |
| 129 | + |
| 130 | + /** Whether the processor supports autofix */ |
| 131 | + supportsAutofix: boolean; |
| 132 | +} |
| 133 | + |
| 134 | +/** |
| 135 | + * The ESLint plugin for LWC graph analysis |
| 136 | + */ |
| 137 | +declare const lwcGraphAnalyzerPlugin: ESLint.Plugin & { |
| 138 | + /** The bundle analyzer processor */ |
| 139 | + processors: { |
| 140 | + bundleAnalyzer: BundleAnalyzer; |
| 141 | + }; |
| 142 | + /** The plugin's configuration presets */ |
| 143 | + configs: { |
| 144 | + base: Linter.Config; |
| 145 | + recommended: Linter.Config; |
| 146 | + }; |
| 147 | +}; |
| 148 | + |
| 149 | +export = lwcGraphAnalyzerPlugin; |
| 150 | +export { LwcBundle }; |
0 commit comments