From 6153a9f9764d78a5b6cf55d7eaf68af260340c37 Mon Sep 17 00:00:00 2001 From: nenadfilipovic <53398358+nenadfilipovic@users.noreply.github.com> Date: Mon, 21 Apr 2025 11:43:04 +0000 Subject: [PATCH] feat: add ls-lint linter --- README.md | 3 +- linters/ls-lint/helpers/json-to-sarif.js | 71 +++++++++++++++++++ linters/ls-lint/ls_lint.test.ts | 3 + linters/ls-lint/plugin.yaml | 40 +++++++++++ .../ls_lint_v2.3.0_snake_case.check.shot | 21 ++++++ linters/ls-lint/test_data/snake_case.in.ts | 1 + 6 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 linters/ls-lint/helpers/json-to-sarif.js create mode 100644 linters/ls-lint/ls_lint.test.ts create mode 100644 linters/ls-lint/plugin.yaml create mode 100644 linters/ls-lint/test_data/ls_lint_v2.3.0_snake_case.check.shot create mode 100644 linters/ls-lint/test_data/snake_case.in.ts diff --git a/README.md b/README.md index d0545e841..6456da785 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ trunk check enable {linter} | Technology | Linters | | --------------- | ------------------------------------------------------------------------------------------------------------------------ | -| All | [codespell], [cspell], [gitleaks], [git-diff-check], [pre-commit-hooks], [trunk-toolbox], [vale] | +| All | [codespell], [cspell], [gitleaks], [git-diff-check], [pre-commit-hooks], [trunk-toolbox], [vale], [ls-lint] | | Ansible | [ansible-lint] | | Apex | [pmd] | | Bash | [shellcheck], [shfmt] | @@ -136,6 +136,7 @@ trunk check enable {linter} [isort]: https://github.com/PyCQA/isort#readme [ktlint]: https://github.com/pinterest/ktlint#readme [kube-linter]: https://github.com/stackrox/kube-linter#readme +[ls-lint]: https://github.com/loeffel-io/ls-lint#readme [markdownlint]: https://github.com/DavidAnson/markdownlint#readme [markdownlint-cli2]: https://github.com/DavidAnson/markdownlint-cli2#readme [markdown-table-prettify]: https://github.com/darkriszty/MarkdownTablePrettify-VSCodeExt#readme diff --git a/linters/ls-lint/helpers/json-to-sarif.js b/linters/ls-lint/helpers/json-to-sarif.js new file mode 100644 index 000000000..fa11c04be --- /dev/null +++ b/linters/ls-lint/helpers/json-to-sarif.js @@ -0,0 +1,71 @@ +#!/usr/bin/env node +"use strict"; + +const fs = require("fs"); + +const directory = ".dir"; + +const toSarif = (path, rule, message) => ({ + level: "error", + locations: [ + { + physicalLocation: { + artifactLocation: { + uri: path, + }, + }, + }, + ], + message: { + text: message, + }, + ruleId: rule, +}); + +const loadJsonSafe = () => { + try { + const inputData = JSON.parse(fs.readFileSync(0, "utf-8")); + return inputData; + } catch { + return []; + } +}; + +const main = () => { + const results = []; + const inputData = loadJsonSafe(); + + for (const [path, descriptor] of Object.entries(inputData)) { + // src/directory { '.dir': [ 'kebabcase' ] } + for (const [key, rules] of Object.entries(descriptor)) { + // .dir [ 'kebabcase' ] + const type = key === directory ? "Directory" : "File"; + + for (const rule of rules) { + // kebabcase + const message = `${type} ${path} does not comply with rule ${rule}`; + results.push(toSarif(path, rule, message)); + } + } + } + + const output = { + $schema: + "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json", + version: "2.1.0", + runs: [ + { + results: results, + tool: { + driver: { + name: "ls-lint", + }, + }, + }, + ], + }; + + console.log(JSON.stringify(output, null, 2)); +}; + +main(); diff --git a/linters/ls-lint/ls_lint.test.ts b/linters/ls-lint/ls_lint.test.ts new file mode 100644 index 000000000..11adae659 --- /dev/null +++ b/linters/ls-lint/ls_lint.test.ts @@ -0,0 +1,3 @@ +import { linterCheckTest } from "tests"; + +linterCheckTest({ linterName: "ls-lint" }); diff --git a/linters/ls-lint/plugin.yaml b/linters/ls-lint/plugin.yaml new file mode 100644 index 000000000..6f9bfea38 --- /dev/null +++ b/linters/ls-lint/plugin.yaml @@ -0,0 +1,40 @@ +# yaml-language-server: $schema=https://static.trunk.io/pub/trunk-yaml-schema.json + +version: 0.1 +tools: + definitions: + - name: ls-lint + shims: + - ls-lint + runtime: node + known_good_version: 2.3.0 + package: "@ls-lint/ls-lint" +lint: + definitions: + - name: ls-lint + files: + - ALL + tools: + - ls-lint + direct_configs: + - .ls-lint.yml + hold_the_line: false + version_command: + parse_regex: ${semver} + run: ls-lint --version + known_good_version: 2.3.0 + suggest_if: config_present + description: Lints thousands of files and directories in milliseconds + commands: + - name: lint + target: . + batch: true + output: sarif + success_codes: + - 0 + - 1 + read_output_from: stderr + parser: + runtime: node + run: ${plugin}/helpers/json-to-sarif.js + run: ls-lint --workdir ${workspace} --error-output-format json diff --git a/linters/ls-lint/test_data/ls_lint_v2.3.0_snake_case.check.shot b/linters/ls-lint/test_data/ls_lint_v2.3.0_snake_case.check.shot new file mode 100644 index 000000000..060048a06 --- /dev/null +++ b/linters/ls-lint/test_data/ls_lint_v2.3.0_snake_case.check.shot @@ -0,0 +1,21 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Testing linter ls-lint test snake_case 1`] = ` +{ + "issues": [], + "lintActions": [], + "taskFailures": [ + { + "details": StringMatching /\\.\\*\\$/m, + "message": ".", + "name": "ls-lint", + }, + { + "details": StringMatching /\\.\\*\\$/m, + "message": ".", + "name": "ls-lint", + }, + ], + "unformattedFiles": [], +} +`; diff --git a/linters/ls-lint/test_data/snake_case.in.ts b/linters/ls-lint/test_data/snake_case.in.ts new file mode 100644 index 000000000..187ad13fe --- /dev/null +++ b/linters/ls-lint/test_data/snake_case.in.ts @@ -0,0 +1 @@ +// Hello