Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: migrate linting workflow to use trunk check metalinter #17876

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Expand Up @@ -12,3 +12,4 @@
/tools/internal-rules/node_modules/**
test.js
!.eslintrc.js
**/svgo.config.*js
25 changes: 25 additions & 0 deletions .github/workflows/annotate_pr.yaml
@@ -0,0 +1,25 @@
name: Annotate PR with trunk issues

on:
workflow_run:
workflows: [Pull Request]
types: [completed]

permissions: read-all

jobs:
trunk_check:
name: Trunk Check Annotate
runs-on: ubuntu-latest

permissions:
checks: write

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Trunk Check
uses: trunk-io/trunk-action@v1
with:
post-annotations: true # only for fork PRs
10 changes: 8 additions & 2 deletions .github/workflows/ci.yml
Expand Up @@ -25,7 +25,10 @@ jobs:
run: npm install

- name: Lint Files
run: node Makefile lint
uses: trunk-io/trunk-action@v1
with:
# Run on everything except the docs folder.
arguments: --ignore=docs/**

- name: Check Rule Files
run: node Makefile checkRuleFiles
Expand All @@ -34,7 +37,10 @@ jobs:
run: node Makefile checkLicenses

- name: Lint Docs JS Files
run: node Makefile lintDocsJS
uses: trunk-io/trunk-action@v1
with:
# Run only on the docs folder.
arguments: --ignore=** --ignore=!docs/**

- name: Check Rule Examples
run: node Makefile checkRuleExamples
Expand Down
3 changes: 0 additions & 3 deletions .markdownlintignore

This file was deleted.

9 changes: 9 additions & 0 deletions .trunk/.gitignore
@@ -0,0 +1,9 @@
*out
*logs
*actions
*notifications
*tools
plugins
user_trunk.yaml
user.yaml
tmp
7 changes: 7 additions & 0 deletions .trunk/configs/.shellcheckrc
@@ -0,0 +1,7 @@
enable=all
source-path=SCRIPTDIR
disable=SC2154

# If you're having issues with shellcheck following source, disable the errors via:
# disable=SC1090
# disable=SC1091
10 changes: 10 additions & 0 deletions .trunk/configs/.yamllint.yaml
@@ -0,0 +1,10 @@
rules:
quoted-strings:
required: only-when-needed
extra-allowed: ["{|}"]
empty-values:
forbid-in-block-mappings: true
forbid-in-flow-mappings: true
key-duplicates: {}
octal-values:
forbid-implicit-octal: true
14 changes: 14 additions & 0 deletions .trunk/configs/svgo.config.js
@@ -0,0 +1,14 @@
module.exports = {
plugins: [
{
name: "preset-default",
params: {
overrides: {
removeViewBox: false, // https://github.com/svg/svgo/issues/1128
sortAttrs: true,
removeOffCanvasPaths: true,
},
},
},
],
};
62 changes: 62 additions & 0 deletions .trunk/trunk.yaml
@@ -0,0 +1,62 @@
# This file controls the behavior of Trunk: https://docs.trunk.io/cli
# To learn more about the format of this file, see https://docs.trunk.io/reference/trunk-yaml
version: 0.1
cli:
version: 1.21.0
# Trunk provides extensibility via plugins. (https://docs.trunk.io/plugins)
plugins:
sources:
- id: trunk
uri: https://github.com/trunk-io/plugins
ref: v1.4.3

# Many linters and tools depend on runtimes - configure them here. (https://docs.trunk.io/runtimes)
runtimes:
enabled:
- [email protected]
- [email protected]
- [email protected]
tools:
enabled:
- [email protected]
runtimes: [node]

# This is the section where you manage your linters. (https://docs.trunk.io/check/configuration)
lint:
definitions:
- name: eslint
commands:
- name: lint
run: ${workspace}/bin/eslint.js --output-file ${tmpfile} --format json ${target}
enabled:
- eslint
- [email protected]
- [email protected]
- git-diff-check
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
ignore:
- linters: [markdownlint]
paths:
- CHANGELOG.md
- linters: [prettier]
paths:
# Ignore all files
- "**"
# Except for json files in conf/
- "!conf/**/*.json"
actions:
disabled:
- trunk-announce
- trunk-check-pre-push
- trunk-fmt-pre-commit
enabled:
- trunk-upgrade-available
102 changes: 1 addition & 101 deletions Makefile.js
Expand Up @@ -21,8 +21,6 @@ const checker = require("npm-license"),
semver = require("semver"),
ejs = require("ejs"),
loadPerf = require("load-perf"),
yaml = require("js-yaml"),
ignore = require("ignore"),
{ CLIEngine } = require("./lib/cli-engine"),
builtinRules = require("./lib/rules/index");

Expand All @@ -33,7 +31,7 @@ require("shelljs/make");
* @see https://github.com/shelljs/shelljs/blob/124d3349af42cb794ae8f78fc9b0b538109f7ca7/make.js#L4
* @see https://github.com/DefinitelyTyped/DefinitelyTyped/blob/3aa2d09b6408380598cfb802743b07e1edb725f3/types/shelljs/make.d.ts#L8-L11
*/
const { cat, cd, echo, exec, exit, find, ls, mkdir, pwd, test } = require("shelljs");
const { cat, cd, echo, exec, exit, find, mkdir, pwd, test } = require("shelljs");

//------------------------------------------------------------------------------
// Settings
Expand Down Expand Up @@ -73,9 +71,6 @@ const NODE = "node ", // intentional extra space

// Files
RULE_FILES = glob.sync("lib/rules/*.js").filter(filePath => path.basename(filePath) !== "index.js"),
JSON_FILES = find("conf/").filter(fileType("json")),
MARKDOWNLINT_IGNORE_INSTANCE = ignore().add(fs.readFileSync(path.join(__dirname, ".markdownlintignore"), "utf-8")),
MARKDOWN_FILES_ARRAY = MARKDOWNLINT_IGNORE_INSTANCE.filter(find("docs/").concat(ls(".")).filter(fileType("md"))),
TEST_FILES = "\"tests/{bin,conf,lib,tools}/**/*.js\"",
PERF_ESLINTRC = path.join(PERF_TMP_DIR, "eslint.config.js"),
PERF_MULTIFILES_TARGET_DIR = path.join(PERF_TMP_DIR, "eslint"),
Expand All @@ -93,30 +88,6 @@ const NODE = "node ", // intentional extra space
// Helpers
//------------------------------------------------------------------------------

/**
* Simple JSON file validation that relies on ES JSON parser.
* @param {string} filePath Path to JSON.
* @throws Error If file contents is invalid JSON.
* @returns {undefined}
*/
function validateJsonFile(filePath) {
const contents = fs.readFileSync(filePath, "utf8");

JSON.parse(contents);
}

/**
* Generates a function that matches files with a particular extension.
* @param {string} extension The file extension (i.e. "js")
* @returns {Function} The function to pass into a filter method.
* @private
*/
function fileType(extension) {
return function(filename) {
return filename.slice(filename.lastIndexOf(".") + 1) === extension;
};
}

/**
* Executes a command and returns the output instead of printing it to stdout.
* @param {string} cmd The command string to execute.
Expand Down Expand Up @@ -390,29 +361,6 @@ function getFirstVersionOfDeletion(filePath) {
.sort(semver.compare)[0];
}

/**
* Lints Markdown files.
* @param {Array} files Array of file names to lint.
* @returns {Object} exec-style exit code object.
* @private
*/
function lintMarkdown(files) {
const markdownlint = require("markdownlint");
const config = yaml.load(fs.readFileSync(path.join(__dirname, "./.markdownlint.yml"), "utf8")),
result = markdownlint.sync({
files,
config,
resultVersion: 1
}),
resultString = result.toString(),
returnCode = resultString ? 1 : 0;

if (resultString) {
console.error(resultString);
}
return { code: returnCode };
}

/**
* Gets linting results from every formatter, based on a hard-coded snippet and config
* @returns {Object} Output from each formatter
Expand Down Expand Up @@ -483,54 +431,6 @@ function getBinFile(command) {
// Tasks
//------------------------------------------------------------------------------

target.lint = function([fix = false] = []) {
let errors = 0,
lastReturn;

/*
* In order to successfully lint JavaScript files in the `docs` directory, dependencies declared in `docs/package.json`
* would have to be installed in `docs/node_modules`. In particular, eslint-plugin-node rules examine `docs/node_modules`
* when analyzing `require()` calls from CJS modules in the `docs` directory. Since our release process does not run `npm install`
* in the `docs` directory, linting would fail and break the release. Also, working on the main `eslint` package does not require
* installing dependencies declared in `docs/package.json`, so most contributors will not have `docs/node_modules` locally.
* Therefore, we add `--ignore-pattern "docs/**"` to exclude linting the `docs` directory from this command.
* There is a separate command `target.lintDocsJS` for linting JavaScript files in the `docs` directory.
*/
echo("Validating JavaScript files");
lastReturn = exec(`${ESLINT}${fix ? "--fix" : ""} . --ignore-pattern "docs/**"`);
if (lastReturn.code !== 0) {
errors++;
}

echo("Validating JSON Files");
JSON_FILES.forEach(validateJsonFile);

echo("Validating Markdown Files");
lastReturn = lintMarkdown(MARKDOWN_FILES_ARRAY);
if (lastReturn.code !== 0) {
errors++;
}

if (errors) {
exit(1);
}
};

target.lintDocsJS = function([fix = false] = []) {
let errors = 0;

echo("Validating JavaScript files in the docs directory");
const lastReturn = exec(`${ESLINT}${fix ? "--fix" : ""} docs`);

if (lastReturn.code !== 0) {
errors++;
}

if (errors) {
exit(1);
}
};

target.fuzz = function({ amount = 1000, fuzzBrokenAutofixes = false } = {}) {
const { run } = require("./tools/fuzzer-runner");
const fuzzResults = run({ amount, fuzzBrokenAutofixes });
Expand Down
3 changes: 2 additions & 1 deletion eslint.config.js
Expand Up @@ -89,7 +89,8 @@ module.exports = [
"tests/performance/**",
"tmp/**",
"tools/internal-rules/node_modules/**",
"**/test.js"
"**/test.js",
"**/svgo.config.*js"
]
},
{
Expand Down
12 changes: 6 additions & 6 deletions package.json
Expand Up @@ -18,12 +18,11 @@
"build:webpack": "node Makefile.js webpack",
"build:readme": "node tools/update-readme.js",
"build:rules-index": "node Makefile.js generateRuleIndexPage",
"lint": "node Makefile.js lint",
"lint:docs:js": "node Makefile.js lintDocsJS",
"lint": "trunk check --ignore=docs/**",
"lint:docs:js": "trunk check --filter=eslint --ignore=** --ignore=!docs/**",
"lint:docs:rule-examples": "node Makefile.js checkRuleExamples",
"lint:fix": "node Makefile.js lint -- fix",
"lint:fix:docs:js": "node Makefile.js lintDocsJS -- fix",
"lint:unused": "knip",
"lint:fix": "trunk check -y --ignore=docs/**",
"lint:fix:docs:js": "trunk check -y --filter=eslint --ignore=** --ignore=!docs/**",
"release:generate:alpha": "node Makefile.js generatePrerelease -- alpha",
"release:generate:beta": "node Makefile.js generatePrerelease -- beta",
"release:generate:latest": "node Makefile.js generateRelease",
Expand Down Expand Up @@ -102,6 +101,7 @@
"devDependencies": {
"@babel/core": "^7.4.3",
"@babel/preset-env": "^7.4.3",
"@trunkio/launcher": "^1.3.0",
"@types/estree": "^1.0.5",
"@types/node": "^20.11.5",
"@wdio/browser-runner": "^8.14.6",
Expand Down Expand Up @@ -177,4 +177,4 @@
"engines": {
"node": "^18.18.0 || ^20.9.0 || >=21.1.0"
}
}
}