feat(lint): enhance ESLint integration by adding version and plugin support

Signed-off-by: 孙振宇 <>
This commit is contained in:
孙振宇 2025-02-07 17:31:27 +08:00
parent ab424b3d65
commit f9a0b7893a
5 changed files with 22 additions and 16 deletions

View File

@ -1,4 +1,5 @@
module.exports = { module.exports = {
root: true,
parser: "@typescript-eslint/parser", parser: "@typescript-eslint/parser",
parserOptions: { parserOptions: {
ecmaVersion: 6, ecmaVersion: 6,

View File

@ -10,8 +10,9 @@ class CodeLintExecutor {
def workspace def workspace
def configs def configs
def linterType def linterType
def component
CodeLintExecutor(steps, workspace, configs, linterType) { CodeLintExecutor(steps, workspace, configs, linterType, component) {
this.steps = steps this.steps = steps
this.workspace = workspace this.workspace = workspace
this.configs = configs this.configs = configs
@ -43,7 +44,11 @@ class CodeLintExecutor {
linter = new PyLint(steps, workspace, configs) linter = new PyLint(steps, workspace, configs)
break break
case CodeLinterTypes.ESLINT: case CodeLinterTypes.ESLINT:
linter = new ESLint(steps, workspace, configs) if (component.eslintVersion == null || component.eslintVersion.isEmpty()) {
steps.log.error("CodeLintExecutor", "ESLint version is required")
return
}
linter = new ESLint(steps, workspace, configs, component.eslintVersion, component.eslintPlugins)
break break
default: default:
steps.log.error("CodeLintExecutor", "Unknown linter type") steps.log.error("CodeLintExecutor", "Unknown linter type")

View File

@ -4,25 +4,21 @@ import com.freeleaps.devops.enums.CodeLinterTypes
import com.freeleaps.devops.lint.LinterBase import com.freeleaps.devops.lint.LinterBase
class ESLint extends LinterBase { class ESLint extends LinterBase {
def eslintVersion
def plugins
def deps = [ ESLint(steps, workspace, configs, eslintVersion, plugins) {
'eslint-define-config',
'eslint-config-prettier',
'eslint-plugin-prettier',
'eslint-plugin-vue',
'@typescript-eslint/eslint-plugin',
'@typescript-eslint/parser',
'typescript'
]
ESLint(steps, workspace, configs) {
super(steps, workspace, configs, CodeLinterTypes.ESLINT) super(steps, workspace, configs, CodeLinterTypes.ESLINT)
this.eslintVersion = eslintVersion
this.plugins = plugins
} }
def doLint() { def doLint() {
steps.dir(workspace) { steps.dir(workspace) {
steps.log.info("${linterType.linter}", "Install eslint with version: ${eslintVersion}")
steps.sh("npm install -g eslint@${eslintVersion}")
steps.log.info("${linterType.linter}", "Install eslint dependencies...") steps.log.info("${linterType.linter}", "Install eslint dependencies...")
steps.sh("npm install -g ${deps.join(' ')}") steps.sh("npm install -g ${plugins.join(' ')}")
// check if given config file is not end with .json // check if given config file is not end with .json
if (!configs.endsWith('.js')) { if (!configs.endsWith('.js')) {
steps.log.warn("${linterType.linter}", "Invalid eslint configuration file, must be a JS file, convert file as valid JS file...") steps.log.warn("${linterType.linter}", "Invalid eslint configuration file, must be a JS file, convert file as valid JS file...")

View File

@ -47,6 +47,10 @@ executeFreeleapsPipeline {
lintEnabled: true, lintEnabled: true,
// linter used to specify the code linter // linter used to specify the code linter
linter: 'eslint', linter: 'eslint',
// eslintVersion used to specify the eslint version
eslintVersion: '7.32.0',
// eslintPlugins used to specify the eslint plugins
eslintPlugins: ['eslint-plugin-vue@8.0.3'],
// linterConfig used to specify the code linter configuration file path, if not set, will use the default configuration // linterConfig used to specify the code linter configuration file path, if not set, will use the default configuration
// linterConfig: '.eslintrc.js', // linterConfig: '.eslintrc.js',
// sastEnabled used to specify whether to enable SAST scan // sastEnabled used to specify whether to enable SAST scan

View File

@ -142,7 +142,7 @@ def generateComponentStages(component, configurations) {
def linterType = CodeLinterTypes.parse(component.linter) def linterType = CodeLinterTypes.parse(component.linter)
def codeLintExecutor = new CodeLintExecutor(this, env.workroot + "/" + component.root + "/", component.linterConfig, linterType) def codeLintExecutor = new CodeLintExecutor(this, env.workroot + "/" + component.root + "/", component.linterConfig, linterType, component)
codeLintExecutor.execute() codeLintExecutor.execute()
} }
} }