From d7efdc6b91cf888d548b120a3beebd8a844ab89a Mon Sep 17 00:00:00 2001 From: Peter Downey Date: Wed, 25 Nov 2026 02:57:28 -0500 Subject: [PATCH] Keep the lint files Codacy reads to packages Codacy installs Codacy reads phpcs.xml, eslint.config.mjs, and .stylelintrc.json, but it installs no WordPress coding standards, no PHPCSExtra, and no WordPress ESLint or Stylelint packages, so every analysis failed for those three tools and Codacy checked nothing with them. Each tool now has a file Codacy can run and a full file that composer, npm, and CI run, so the plugin's own checks are unchanged: - PHPCS: phpcs-wordpress.xml is the former phpcs.xml. phpcs.xml holds the rules from PHPCS's own standards and VariableAnalysis that the WordPress standards apply, with their settings and the tab width, plus PHPCompatibilityWP. - Stylelint: stylelint-wordpress.json is the former .stylelintrc.json. .stylelintrc.json holds its 60 plain Stylelint rules. - ESLint: eslint.config.mjs exports the project's rules; eslint.wordpress.config.mjs adds the WordPress set and the browser-support check (its browser list extends @wordpress/browserslist-config), so the resolved configuration is unchanged. .github/scripts/codacy-configs.py regenerates phpcs.xml's rules and .stylelintrc.json from the full files, and CI fails when they are out of date. Co-Authored-By: Claude Opus 5.5 --- .codacy.yml | 5 + .distignore | 3 + .gitattributes | 3 + .github/ISSUE_TEMPLATE/lint-failure.md | 4 +- .github/labeler.yml | 3 + .github/scripts/codacy-configs.py | 144 ++++++ .github/scripts/phpcs-ruleset-dump.php | 56 +++ .github/workflows/wp-compatibility-test.yml | 12 +- .mypy.ini | 2 +- .stylelintrc.json | 236 ++++++++- CONTRIBUTING.md | 9 +- composer.json | 4 +- eslint.config.mjs | 48 +- eslint.wordpress.config.mjs | 25 + package.json | 4 +- phpcs-wordpress.xml | 213 ++++++++ phpcs.xml | 518 +++++++++++++++----- stylelint-wordpress.json | 7 + 18 files changed, 1132 insertions(+), 164 deletions(-) create mode 100755 .github/scripts/codacy-configs.py create mode 100644 .github/scripts/phpcs-ruleset-dump.php create mode 100644 eslint.wordpress.config.mjs create mode 100644 phpcs-wordpress.xml create mode 100644 stylelint-wordpress.json diff --git a/.codacy.yml b/.codacy.yml index dda7592..894fd0d 100644 --- a/.codacy.yml +++ b/.codacy.yml @@ -1,5 +1,10 @@ --- # Codacy configuration +# +# With each tool's "Configuration file" setting on, Codacy reads phpcs.xml, phpmd.xml, eslint.config.mjs, .stylelintrc.json, and +# .markdownlint.json. Codacy installs no WordPress standards or packages, so those files hold only +# what it can run; CI runs the full configurations (phpcs-wordpress.xml, phpmd.xml, eslint.wordpress.config.mjs, stylelint-wordpress.json). +# See CONTRIBUTING.md. engines: # Lizard reads the lock files as source code and reports their length. diff --git a/.distignore b/.distignore index c58d757..b82e565 100644 --- a/.distignore +++ b/.distignore @@ -60,13 +60,16 @@ /.mypy.ini /.ruff.toml /.stylelintrc.json +/stylelint-wordpress.json /.yamllint.yml /composer.json /composer.lock /eslint.config.mjs +/eslint.wordpress.config.mjs /package.json /package-lock.json /phpcs.xml +/phpcs-wordpress.xml /phpmd.xml /phpstan.neon /phpunit.xml diff --git a/.gitattributes b/.gitattributes index d3208c9..a95182f 100644 --- a/.gitattributes +++ b/.gitattributes @@ -12,15 +12,18 @@ /.mypy.ini export-ignore /.ruff.toml export-ignore /.stylelintrc.json export-ignore +/stylelint-wordpress.json export-ignore /.yamllint.yml export-ignore /composer.json export-ignore /composer.lock export-ignore /eslint.config.mjs export-ignore +/eslint.wordpress.config.mjs export-ignore /CHANGELOG.md export-ignore /CONTRIBUTING.md export-ignore /package.json export-ignore /package-lock.json export-ignore /phpcs.xml export-ignore +/phpcs-wordpress.xml export-ignore /phpmd.xml export-ignore /phpstan.neon export-ignore /phpunit.xml export-ignore diff --git a/.github/ISSUE_TEMPLATE/lint-failure.md b/.github/ISSUE_TEMPLATE/lint-failure.md index 53f50c2..9f4fc66 100644 --- a/.github/ISSUE_TEMPLATE/lint-failure.md +++ b/.github/ISSUE_TEMPLATE/lint-failure.md @@ -28,8 +28,8 @@ The Python tools are installed from `.github/lint-requirements.txt`, which pins - **Ruff** (`ruff check` and `ruff format --check`) on `.github/scripts`, with the rules in `.ruff.toml`. - **mypy** in strict mode on `.github/scripts`, configured in `.mypy.ini`. - **markdownlint** on every Markdown file that git does not ignore (`npm run lint:md`), with the rules in `.markdownlint.json`. -- **ESLint** with the WordPress JavaScript coding standards (`npm run lint:js`), configured in `eslint.config.mjs`. -- **Stylelint** on the plugin's CSS files (`npm run lint:css`), with the rules in `.stylelintrc.json`. +- **ESLint** with the WordPress JavaScript coding standards (`npm run lint:js`), configured in `eslint.wordpress.config.mjs` (the WordPress standards plus `eslint.config.mjs`, which Codacy reads). +- **Stylelint** on the plugin's CSS files (`npm run lint:css`), with the rules in `stylelint-wordpress.json` (`.stylelintrc.json`, which Codacy reads, is generated from it: `python .github/scripts/codacy-configs.py stylelint`). ### Next Steps diff --git a/.github/labeler.yml b/.github/labeler.yml index 95dcb4d..64124fc 100644 --- a/.github/labeler.yml +++ b/.github/labeler.yml @@ -53,6 +53,7 @@ testing: - any-glob-to-any-file: stubs/**/* - any-glob-to-any-file: phpunit.xml - any-glob-to-any-file: phpcs.xml + - any-glob-to-any-file: phpcs-wordpress.xml - any-glob-to-any-file: phpmd.xml - any-glob-to-any-file: phpstan.neon - any-glob-to-any-file: psalm.xml @@ -62,6 +63,8 @@ testing: - any-glob-to-any-file: .mypy.ini - any-glob-to-any-file: .ruff.toml - any-glob-to-any-file: eslint.config.mjs + - any-glob-to-any-file: eslint.wordpress.config.mjs - any-glob-to-any-file: .github/lint-requirements.txt - any-glob-to-any-file: .stylelintrc.json + - any-glob-to-any-file: stylelint-wordpress.json - any-glob-to-any-file: .yamllint.yml diff --git a/.github/scripts/codacy-configs.py b/.github/scripts/codacy-configs.py new file mode 100755 index 0000000..21f9d10 --- /dev/null +++ b/.github/scripts/codacy-configs.py @@ -0,0 +1,144 @@ +#!/usr/bin/env python3 +"""Regenerate the lint configuration files that Codacy reads from the full ones that CI runs. + +Codacy reads phpcs.xml and .stylelintrc.json, but it installs no WordPress coding standards, no +PHPCSExtra, and none of the WordPress Stylelint packages, so a file that names them makes the +tool fail. The full standards stay in phpcs-wordpress.xml and stylelint-wordpress.json, which +composer, npm, and CI run; this script copies into the Codacy files every rule of the full +standards that Codacy can run, with the settings the full standards give it. + + python .github/scripts/codacy-configs.py phpcs [--check] + python .github/scripts/codacy-configs.py stylelint [--check] + +With --check, nothing is written; the script fails when the file is out of date. phpcs needs +`composer install`; stylelint needs `npm ci`. +""" + +from __future__ import annotations + +import argparse +import json +import shutil +import subprocess # nosec B404 +import sys +import tempfile +from pathlib import Path +from typing import Any +from xml.sax.saxutils import escape, quoteattr # nosec B406 # nosemgrep + +# Bandit and Opengrep: every subprocess call in this file runs a fixed argument list without a +# shell, and xml.sax.saxutils only escapes the text this file writes; nothing here parses XML. + +ROOT = Path(__file__).resolve().parents[2] +SCRIPTS = Path(__file__).resolve().parent + +# PHPCS standards that Codacy installs. PHPCompatibilityWP is referenced as a whole standard in +# phpcs.xml's hand-written part, so its sniffs are not listed one by one. +CODACY_PHPCS_STANDARDS = ("Generic", "PEAR", "PSR1", "PSR2", "PSR12", "Squiz", "Zend", "VariableAnalysis") +BEGIN = " " +END = " " +# A message code (Standard.Category.Sniff.Code) has three dots; a sniff code has two. +MESSAGE_CODE_DOTS = 3 + + +def run(command: list[str]) -> str: + """Run a command from the repository root and return its standard output.""" + result = subprocess.run( # nosec B603 # nosemgrep + command, cwd=ROOT, capture_output=True, text=True, encoding="utf-8", check=False + ) + if not result.stdout.strip(): + sys.exit(f"{' '.join(command)} printed nothing:\n{result.stderr}") + return result.stdout + + +def property_xml(name: str, value: object, pad: str) -> list[str]: + """Write one PHPCS sniff property, a scalar or an array.""" + if isinstance(value, dict): + items = [f"{pad} " for k, v in value.items()] + elif isinstance(value, list): + items = [f"{pad} " for v in value] + else: + return [f"{pad}"] + return [f'{pad}', *items, f"{pad}"] + + +def rule_xml(code: str, settings: dict[str, Any]) -> list[str]: + """Write one PHPCS rule with its severity, type, message, and properties.""" + inner = [f" <{key}>{settings[key]}" for key in ("severity", "type") if key in settings] + if "message" in settings: + inner.append(f" {escape(settings['message'])}") + if settings.get("properties"): + inner.append(" ") + for name, value in settings["properties"].items(): + inner += property_xml(name, value, " ") + inner.append(" ") + if not inner: + return [f" "] + return [f" ", *inner, " "] + + +def phpcs_rules() -> str: + """Return the rules of phpcs-wordpress.xml that come from standards Codacy installs.""" + with tempfile.TemporaryDirectory() as tmp: + probe = Path(tmp) / "probe.php" + probe.write_text(" bool: + """Rewrite the generated part of phpcs.xml.""" + path = ROOT / "phpcs.xml" + text = path.read_text(encoding="utf-8") + head, rest = text.split(BEGIN + "\n", 1) + _, tail = rest.split(END, 1) + return write(path, text, f"{head}{BEGIN}\n{phpcs_rules()}\n{END}{tail}", check=check) + + +def update_stylelint(*, check: bool) -> bool: + """Rewrite .stylelintrc.json with the plain Stylelint rules of stylelint-wordpress.json.""" + npx = shutil.which("npx") or "npx" + sample = min(path for path in ROOT.glob("**/*.css") if not {"node_modules", "vendor"} & set(path.parts)) + command = [npx, "--no-install", "stylelint", "--config", "stylelint-wordpress.json", "--print-config"] + resolved = json.loads(run([*command, sample.relative_to(ROOT).as_posix()])) + # Rules with a "/" come from plugins, which Codacy does not install. + rules = {name: value for name, value in sorted(resolved["rules"].items()) if "/" not in name} + path = ROOT / ".stylelintrc.json" + text = path.read_text(encoding="utf-8") if path.exists() else "" + return write(path, text, json.dumps({"rules": rules}, indent=2) + "\n", check=check) + + +def write(path: Path, old: str, new: str, *, check: bool) -> bool: + """Write a regenerated file, or with check only report whether it is up to date.""" + if old == new: + return True + if check: + print(f"{path.name} is out of date: run python .github/scripts/codacy-configs.py", file=sys.stderr) + return False + path.write_text(new, encoding="utf-8", newline="\n") + print(f"updated {path.name}") + return True + + +def main() -> int: + """Regenerate or check one Codacy configuration file.""" + parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) + parser.add_argument("tool", choices=("phpcs", "stylelint")) + parser.add_argument("--check", action="store_true", help="fail when the file is out of date; write nothing") + args = parser.parse_args() + update = update_phpcs if args.tool == "phpcs" else update_stylelint + return 0 if update(check=args.check) else 1 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/.github/scripts/phpcs-ruleset-dump.php b/.github/scripts/phpcs-ruleset-dump.php new file mode 100644 index 0000000..a6d4222 --- /dev/null +++ b/.github/scripts/phpcs-ruleset-dump.php @@ -0,0 +1,56 @@ +ruleset; + $out = ['sniffs' => array_keys($ruleset->sniffCodes), 'rules' => []]; + foreach ($ruleset->ruleset as $code => $settings) { + $entry = []; + foreach ($settings['properties'] ?? [] as $name => $value) { + $entry['properties'][$name] = is_array($value) && array_key_exists('value', $value) ? $value['value'] : $value; + } + foreach (['severity', 'type', 'message'] as $key) { + if (isset($settings[$key])) { + $entry[$key] = $settings[$key]; + } + } + $out['rules'][$code] = $entry; + } + echo json_encode($out, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES), PHP_EOL; + return true; + } + + public function generate( + $cachedData, + $totalFiles, + $totalErrors, + $totalWarnings, + $totalFixable, + $showSources = false, + $width = 80, + $interactive = false, + $toScreen = true + ) { + echo $cachedData; + } +} diff --git a/.github/workflows/wp-compatibility-test.yml b/.github/workflows/wp-compatibility-test.yml index 4545d6f..2373d4b 100644 --- a/.github/workflows/wp-compatibility-test.yml +++ b/.github/workflows/wp-compatibility-test.yml @@ -255,7 +255,10 @@ jobs: - name: Run PHPCS id: run_phpcs run: | - vendor/bin/phpcs --standard=phpcs.xml + # phpcs-wordpress.xml is the full standard. phpcs.xml, which Codacy reads, copies the rules + # Codacy can run from it; fail when that copy is out of date. + vendor/bin/phpcs --standard=phpcs-wordpress.xml + python3 .github/scripts/codacy-configs.py phpcs --check # Static scanner controls; never execute these incompatible PHP fixtures. CONTROL_DIR="$RUNNER_TEMP/optimizations-ace-mc-compat-controls" @@ -1358,8 +1361,11 @@ jobs: - name: Run Stylelint id: run_stylelint - # Stylelint fails by itself when the pattern matches no file. - run: npm run --silent lint:css + # Stylelint fails by itself when the pattern matches no file. .stylelintrc.json, which Codacy + # reads, copies the plain Stylelint rules from stylelint-wordpress.json; fail when it is out of date. + run: | + npm run --silent lint:css + python3 .github/scripts/codacy-configs.py stylelint --check - name: Preserve diagnostic summary id: preserve_diagnostics diff --git a/.mypy.ini b/.mypy.ini index c3f94f4..b16fb11 100644 --- a/.mypy.ini +++ b/.mypy.ini @@ -3,4 +3,4 @@ strict = True warn_unreachable = True scripts_are_modules = True -files = .github/scripts/check-plugin-package.py, .github/scripts/check-wordpress-tested-up-to.py, .github/scripts/test-automation.py +files = .github/scripts/check-plugin-package.py, .github/scripts/codacy-configs.py, .github/scripts/check-wordpress-tested-up-to.py, .github/scripts/test-automation.py diff --git a/.stylelintrc.json b/.stylelintrc.json index e8eaf83..36ac7dd 100644 --- a/.stylelintrc.json +++ b/.stylelintrc.json @@ -1,7 +1,237 @@ { - "extends": "@wordpress/stylelint-config", - "plugins": ["stylelint-no-unsupported-browser-features"], "rules": { - "plugin/no-unsupported-browser-features": [true, { "severity": "error" }] + "annotation-no-unknown": null, + "at-rule-descriptor-no-unknown": [ + true + ], + "at-rule-descriptor-value-no-unknown": [ + true + ], + "at-rule-empty-line-before": [ + "always", + { + "except": [ + "blockless-after-blockless" + ], + "ignore": [ + "after-comment" + ] + } + ], + "at-rule-no-deprecated": [ + true + ], + "at-rule-no-unknown": [ + true + ], + "at-rule-prelude-no-invalid": [ + true, + { + "ignoreAtRules": [ + "media" + ] + } + ], + "block-no-empty": [ + true + ], + "color-hex-length": [ + "short" + ], + "color-named": [ + "never" + ], + "comment-empty-line-before": [ + "always", + { + "ignore": [ + "stylelint-commands" + ] + } + ], + "comment-no-empty": [ + true + ], + "custom-property-no-missing-var-function": [ + true + ], + "custom-property-pattern": [ + "^(?!_(?:gcd|wp)-).+", + {} + ], + "declaration-block-no-duplicate-custom-properties": [ + true + ], + "declaration-block-no-duplicate-properties": [ + true, + { + "ignore": [ + "consecutive-duplicates" + ] + } + ], + "declaration-block-no-shorthand-property-overrides": [ + true + ], + "declaration-property-unit-allowed-list": [ + { + "line-height": [ + "px" + ] + } + ], + "declaration-property-value-keyword-no-deprecated": [ + true + ], + "declaration-property-value-no-unknown": [ + true + ], + "font-family-name-quotes": [ + "always-where-recommended" + ], + "font-family-no-duplicate-names": [ + true + ], + "font-family-no-missing-generic-family-keyword": [ + true + ], + "font-weight-notation": [ + "numeric" + ], + "function-calc-no-unspaced-operator": [ + true + ], + "function-name-case": [ + "lower", + { + "ignoreFunctions": [ + "/^DXImageTransform.Microsoft.*$/" + ] + } + ], + "function-no-unknown": null, + "function-url-quotes": [ + "never" + ], + "keyframe-block-no-duplicate-selectors": null, + "keyframe-declaration-no-important": [ + true + ], + "length-zero-no-unit": [ + true, + { + "ignore": [ + "custom-properties" + ], + "ignoreFunctions": [ + "var" + ] + } + ], + "media-feature-name-no-unknown": [ + true + ], + "media-feature-name-value-no-unknown": [ + true + ], + "media-query-no-invalid": null, + "media-type-no-deprecated": [ + true + ], + "named-grid-areas-no-invalid": [ + true + ], + "nesting-selector-no-missing-scoping-root": [ + true + ], + "no-descending-specificity": [ + true + ], + "no-duplicate-at-import-rules": [ + true + ], + "no-duplicate-selectors": [ + true + ], + "no-empty-source": [ + true + ], + "no-invalid-double-slash-comments": [ + true + ], + "no-invalid-position-at-import-rule": [ + true + ], + "no-invalid-position-declaration": [ + true + ], + "no-irregular-whitespace": [ + true + ], + "property-no-deprecated": [ + true + ], + "property-no-unknown": [ + true + ], + "rule-empty-line-before": [ + "always", + { + "ignore": [ + "after-comment" + ] + } + ], + "selector-anb-no-unmatchable": null, + "selector-attribute-quotes": [ + "always" + ], + "selector-class-pattern": [ + "^([a-z][a-z0-9]*)(-[a-z0-9]+)*$", + { + "message": "Selector should use lowercase and separate words with hyphens (selector-class-pattern)" + } + ], + "selector-id-pattern": [ + "^([a-z][a-z0-9]*)(-[a-z0-9]+)*$", + { + "message": "Selector should use lowercase and separate words with hyphens (selector-id-pattern)" + } + ], + "selector-pseudo-class-no-unknown": [ + true + ], + "selector-pseudo-element-colon-notation": [ + "double" + ], + "selector-pseudo-element-no-unknown": [ + true + ], + "selector-type-case": [ + "lower" + ], + "selector-type-no-unknown": [ + true, + { + "ignore": [ + "custom-elements" + ] + } + ], + "string-no-newline": [ + true, + { + "ignore": [ + "at-rule-preludes", + "declaration-values" + ] + } + ], + "syntax-string-no-invalid": [ + true + ], + "value-keyword-case": [ + "lower" + ] } } diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 1fcf87a..939ec08 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -104,9 +104,14 @@ optimizations-ace-mc/ |-- LICENSE # GPL license |-- composer.json # PHP dependencies and check scripts |-- package.json # JavaScript test dependencies -|-- phpcs.xml # PHPCS configuration +|-- phpcs-wordpress.xml # PHPCS configuration (composer phpcs, CI) +|-- phpcs.xml # The part Codacy can run, generated from phpcs-wordpress.xml |-- phpstan.neon # PHPStan configuration |-- phpmd.xml # PHPMD configuration +|-- eslint.wordpress.config.mjs # ESLint configuration (npm run lint:js, CI) +|-- eslint.config.mjs # Its settings without the WordPress standards, for Codacy +|-- stylelint-wordpress.json # Stylelint configuration (npm run lint:css, CI) +|-- .stylelintrc.json # Its plain Stylelint rules, generated for Codacy |-- psalm.xml # Psalm configuration |-- phpunit.xml # PHPUnit configuration |-- .distignore # Files left out of the release package @@ -115,6 +120,8 @@ optimizations-ace-mc/ The release package holds the main plugin file, `readme.txt`, `LICENSE`, `includes/`, `assets/`, and `languages/`. When you add a top-level file or directory, add it to `.distignore` and `.gitattributes` unless it must ship; `.github/scripts/check-plugin-package.py` fails when the package lists disagree. +Codacy reads `phpcs.xml`, `phpmd.xml`, `eslint.config.mjs`, and `.stylelintrc.json`, but it installs no WordPress coding standards, no PHPCSExtra, no WordPress ESLint or Stylelint packages, and only PHPMD 2. Keep those files to what Codacy installs; the full configurations add the rest. After changing `phpcs-wordpress.xml` or `stylelint-wordpress.json`, or updating their packages, regenerate the Codacy copies with `python .github/scripts/codacy-configs.py phpcs` and `python .github/scripts/codacy-configs.py stylelint`. CI fails when they are out of date. + ## Making Changes ### Before You Start diff --git a/composer.json b/composer.json index 8fcc08a..162c50f 100644 --- a/composer.json +++ b/composer.json @@ -33,8 +33,8 @@ "rector/rector": "^2.6" }, "scripts": { - "phpcs": "phpcs --standard=phpcs.xml", - "phpcs:fix": "phpcbf --standard=phpcs.xml", + "phpcs": "phpcs --standard=phpcs-wordpress.xml", + "phpcs:fix": "phpcbf --standard=phpcs-wordpress.xml", "phpmd": "phpmd analyze . --no-progress --format text --ruleset phpmd.xml --exclude vendor --exclude tests --exclude stubs --exclude assets --exclude .private --exclude node_modules", "psalm": "psalm --config=psalm.xml --show-info=true", "psalm:taint": "psalm --config=psalm.xml --taint-analysis", diff --git a/eslint.config.mjs b/eslint.config.mjs index 745ba5d..01f67c3 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -1,16 +1,13 @@ /** - * ESLint configuration: the WordPress JavaScript coding standards for - * the tests of the store locator info window template, plus rules that - * keep them on modern JavaScript, and a check that any script the plugin - * ships uses only browser features that WordPress supports. The plugin - * ships no script today, so that check has no file to read yet. + * ESLint configuration, without the WordPress JavaScript coding standards: + * rules that keep the tests of the store locator info window template on + * modern JavaScript. * - * The "recommended-with-formatting" set checks formatting with ESLint's own - * rules. The plain "recommended" set hands formatting to Prettier, whose - * output does not match the WordPress style without the WordPress fork. + * Codacy reads this file, but it does not install the WordPress ESLint plugin + * (`@wordpress/eslint-plugin`) or the WordPress browser list that the + * browser-support check reads from package.json. eslint.wordpress.config.mjs + * adds both to these settings; npm run lint:js, and so CI, runs that file. */ -import wordpress from '@wordpress/eslint-plugin'; -import compat from 'eslint-plugin-compat'; import unicorn from 'eslint-plugin-unicorn'; import globals from 'globals'; @@ -71,11 +68,12 @@ const modernizationRules = Object.fromEntries( ].map( ( rule ) => [ `unicorn/${ rule }`, 'error' ] ), ); -export default [ - { - ignores: [ '.private/**', 'coverage/**', 'node_modules/**', 'vendor/**' ], - }, - ...wordpress.configs[ 'recommended-with-formatting' ], +export const ignores = { + ignores: [ '.private/**', 'coverage/**', 'node_modules/**', 'vendor/**' ], +}; + +// The settings that follow the WordPress standards in eslint.wordpress.config.mjs. +export const projectConfigs = [ { languageOptions: { globals: { ...globals.browser }, @@ -102,11 +100,6 @@ export default [ 'prefer-template': 'error', }, }, - { - // The browsers come from the "browserslist" entry in package.json. - ...compat.configs[ 'flat/recommended' ], - files: [ 'assets/js/**/*.js' ], - }, { files: [ 'tests/js/**' ], languageOptions: { @@ -114,3 +107,18 @@ export default [ }, }, ]; + +/* + * Without the WordPress rules, the code's disable comments for them (no-console, + * no-alert) would be reported as unused; the full configuration reports them. + */ +export default [ + ignores, + ...projectConfigs, + { + linterOptions: { + reportUnusedDisableDirectives: 'off', + reportUnusedInlineConfigs: 'off', + }, + }, +]; diff --git a/eslint.wordpress.config.mjs b/eslint.wordpress.config.mjs new file mode 100644 index 0000000..ea7164d --- /dev/null +++ b/eslint.wordpress.config.mjs @@ -0,0 +1,25 @@ +/** + * The full ESLint configuration: the WordPress JavaScript coding standards, + * this project's settings from eslint.config.mjs (modern JavaScript rules), + * and a check that any script the plugin ships uses only browser features that + * WordPress supports. The plugin ships no script today, so that check has no + * file to read yet. npm run lint:js, and so CI, runs this file. + * + * The "recommended-with-formatting" set checks formatting with ESLint's own + * rules. The plain "recommended" set hands formatting to Prettier, whose + * output does not match the WordPress style without the WordPress fork. + */ +import wordpress from '@wordpress/eslint-plugin'; +import compat from 'eslint-plugin-compat'; +import { ignores, projectConfigs } from './eslint.config.mjs'; + +export default [ + ignores, + ...wordpress.configs[ 'recommended-with-formatting' ], + ...projectConfigs, + { + // The browsers come from the "browserslist" entry in package.json. + ...compat.configs[ 'flat/recommended' ], + files: [ 'assets/js/**/*.js' ], + }, +]; diff --git a/package.json b/package.json index 7e869b0..f126ac7 100644 --- a/package.json +++ b/package.json @@ -7,8 +7,8 @@ "node": ">=22.22.2" }, "scripts": { - "lint:css": "stylelint \"assets/css/**/*.css\"", - "lint:js": "eslint --max-warnings 0 .", + "lint:css": "stylelint --config stylelint-wordpress.json \"assets/css/**/*.css\"", + "lint:js": "eslint --config eslint.wordpress.config.mjs --max-warnings 0 .", "lint:md": "markdownlint-cli2", "test": "node --test \"tests/js/**/*.test.mjs\"" }, diff --git a/phpcs-wordpress.xml b/phpcs-wordpress.xml new file mode 100644 index 0000000..57b0c2b --- /dev/null +++ b/phpcs-wordpress.xml @@ -0,0 +1,213 @@ + + + WordPress coding standards + + + . + + + */vendor/* + */.private/* + */node_modules/* + */tests/* + */.git/* + */.github/* + */css/* + */stubs/* + *.css + *.js + + + + + + + + + + + + + + + + + + + + + + + + 5 + + + 5 + + + 5 + + + 5 + + + 5 + + + 5 + + + 5 + + + 5 + + + 5 + + + 5 + + + 5 + + + 5 + + + 5 + + + 5 + + + 5 + + + 5 + + + 5 + + + 5 + + + 5 + + + 5 + + + 5 + + + 5 + + + 5 + + + 5 + + + 5 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + *\.php$ + + + + + + + + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/phpcs.xml b/phpcs.xml index 57b0c2b..fe0fa40 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -1,6 +1,17 @@ - - WordPress coding standards + + The parts of phpcs-wordpress.xml that Codacy can run. + + . @@ -17,186 +28,433 @@ *.css *.js - - - - - - - + + + *\.php$ + - - - - - - - - - - + + - + + + + + + 1 + + + + + + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + + + + + + + + + + + + + + + + + + + 0 + + + + + + + 5 - - 5 + + error + The "goto" language construct should not be used. - - 5 + + + + + + 1 + + + - - 5 + + + + + + - - 5 + + + + + + - - 5 + + + + + + + + + + + + + + + - - 5 + + + 0 - + 5 - + 5 + warning - + 5 + warning - - 5 + + + + + + - - 5 + + + + 0 - + 5 - + 5 - - 5 + + + + + + 0 - - 5 + + 0 - - 5 + + 0 - - 5 + + 0 - - 5 + + + 0 - - 5 + + 0 - + + 5 - - 5 + + + + 0 - - 5 + + 0 + + + 0 - + + + + + 5 - + + + 5 - - - - - - - - - - - - - - + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + 0 + + + 0 + + + 0 + + + + 0 + + + 0 + + + 0 + + + - + + + - - - - - - - - + + 0 + + + + + 0 + + + 0 + + + 0 + + + + - + - - - - - - + + + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + 5 + error + `eval()` is a security risk, please refrain from using it. + + + + - - - - + + - - - + + 0 + + + 5 + + + + + + + 5 + + + 5 + + - - - + + - - - - - - - - - *\.php$ - - - - - - - - 0 + + 3 - - - + + 5 - - - - - - - + diff --git a/stylelint-wordpress.json b/stylelint-wordpress.json new file mode 100644 index 0000000..e8eaf83 --- /dev/null +++ b/stylelint-wordpress.json @@ -0,0 +1,7 @@ +{ + "extends": "@wordpress/stylelint-config", + "plugins": ["stylelint-no-unsupported-browser-features"], + "rules": { + "plugin/no-unsupported-browser-features": [true, { "severity": "error" }] + } +}