-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhelper.js
More file actions
91 lines (82 loc) · 3.11 KB
/
helper.js
File metadata and controls
91 lines (82 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* Copyright (c) 2025, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
const { readFileSync } = require('fs');
const { basename, dirname, join, parse, extname } = require('path');
const { Linter } = require('eslint');
const lwcGraphAnalyzer = require('../../../../lib/index');
const LwcBundle = require('../../../../lib/lwc-bundle');
const baseConfig = require('../../../../lib/configs/base');
/**
* Creates ESLint configuration for a given rule
* @param {string} rulename - Name of the rule to configure
* @returns {Object} ESLint configuration
*/
function createLinterConfig(rulename) {
const pluginPrefix = '@salesforce/lwc-graph-analyzer';
const ruleConfig = {
[`${pluginPrefix}/${rulename}`]: 'error'
};
const config = baseConfig;
config.plugins = { [pluginPrefix]: lwcGraphAnalyzer };
config.rules = ruleConfig;
return config;
}
/**
* Determines which code blocks should be processed by ESLint.
* This function is used to filter which virtual files (code blocks) generated by the processor
* should be analyzed by ESLint. In our case, we want to process both JavaScript and HTML files
* that are part of the LWC bundle.
*
* @param {string} blockFilename - The filename of the code block being processed
* @returns {boolean} True if the code block should be processed (has .js or .html extension), false otherwise
*/
function filterCodeBlock(blockFilename) {
return blockFilename.endsWith('.js') || blockFilename.endsWith('.html');
}
/**
* Runs ESLint on a bundle using file-based bundle setup
* @param {string} rulePath - Path to the rule being tested
* @param {string} target - Target file to lint
* @returns {Array} ESLint messages
*/
function lintBundle(rulePath, target) {
const rulename = parse(basename(rulePath)).name;
const config = createLinterConfig(rulename);
const testPath = join(dirname(rulePath), target);
const srcCode = readFileSync(testPath).toString();
const linter = new Linter();
return linter.verify(srcCode, config, {
filename: testPath,
filterCodeBlock,
reportUnusedDisableDirectives: false
});
}
/**
* Runs ESLint on a bundle using programmatic bundle setup
* @param {string} rulePath - Path to the rule being tested
* @param {string} target - Target file to lint
* @returns {Array} ESLint messages
*/
function lintProgrammaticBundle(rulePath, target) {
const rulename = parse(basename(rulePath)).name;
const config = createLinterConfig(rulename);
const testPath = join(dirname(rulePath), target);
const srcCode = readFileSync(testPath).toString();
// Create and set the bundle before linting
const bundle = LwcBundle.lwcBundleFromFilesystem(srcCode, testPath, extname(testPath));
lwcGraphAnalyzer.lwcBundle = bundle;
const linter = new Linter();
return linter.verify(srcCode, config, {
filename: testPath,
filterCodeBlock,
reportUnusedDisableDirectives: false
});
}
module.exports = {
lintBundle,
lintProgrammaticBundle
};