From adcd166692177df231df6433664c5c9b7d0b335b Mon Sep 17 00:00:00 2001 From: Han Yeong-woo Date: Wed, 3 Jan 2024 04:39:42 +0900 Subject: [PATCH] feat: support stylelint 16 (#296) * feat(peerdeps): support stylelint 16 only * feat(node): support node >=18.12.0 * chore(deps): upgrade dependencies releated test * fix(test): fix segment fault use jest-light-runner * test(fix): remove invalid settings tests Because stylelint auto report thats * feat: migrate to ESM * chore(deps): update dependencies * refactor: replace lodash --- .commitlintrc.js | 2 +- .eslintrc | 9 +++- .github/workflows/commitlint.yml | 2 +- .github/workflows/node.yml | 12 +++--- jest.config.js | 6 ++- lib/index.browser-env.test.js | 70 +++----------------------------- lib/index.js | 33 ++++++++++----- lib/index.test.js | 26 ++++++------ package.json | 44 ++++++++++---------- 9 files changed, 86 insertions(+), 118 deletions(-) diff --git a/.commitlintrc.js b/.commitlintrc.js index 84dcb12..0616fb9 100644 --- a/.commitlintrc.js +++ b/.commitlintrc.js @@ -1,3 +1,3 @@ -module.exports = { +export default { extends: ['@commitlint/config-conventional'], }; diff --git a/.eslintrc b/.eslintrc index 38a029a..703b3b4 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,6 +1,13 @@ { "extends": ["airbnb-base", "prettier"], + "parserOptions": { + "sourceType": "module" + }, "env": { - "node": true + "node": true, + "es2022": true + }, + "rules": { + "import/extensions": ["error", "always"] } } diff --git a/.github/workflows/commitlint.yml b/.github/workflows/commitlint.yml index acd0748..9b40733 100644 --- a/.github/workflows/commitlint.yml +++ b/.github/workflows/commitlint.yml @@ -6,7 +6,7 @@ jobs: lint: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: fetch-depth: 0 - uses: wagoid/commitlint-github-action@v5 diff --git a/.github/workflows/node.yml b/.github/workflows/node.yml index f2a8b8b..156c962 100644 --- a/.github/workflows/node.yml +++ b/.github/workflows/node.yml @@ -6,7 +6,7 @@ jobs: lint: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - run: npm i --ignore-scripts - run: npm run lint:js - run: npm run lint:prettier @@ -16,10 +16,10 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - node-version: [16.x, 18.x, 20.x] + node-version: [18.x, 20.x] steps: - - uses: actions/checkout@v3 - - uses: actions/setup-node@v3 + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 with: node-version: ${{ matrix.node-version }} - run: npm i --ignore-scripts @@ -29,7 +29,7 @@ jobs: needs: [test] runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - run: npm i --ignore-scripts - run: npm run test:coverage - uses: codecov/codecov-action@v3 @@ -39,7 +39,7 @@ jobs: if: github.ref == 'refs/heads/master' runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: fetch-depth: 0 persist-credentials: false diff --git a/jest.config.js b/jest.config.js index 4d02e12..84fd7db 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,3 +1,7 @@ -module.exports = { +/** @satisfies {import('jest').Config} */ +const config = { preset: 'jest-preset-stylelint', + runner: 'jest-light-runner', }; + +export default config; diff --git a/lib/index.browser-env.test.js b/lib/index.browser-env.test.js index abacaf3..4ec662f 100644 --- a/lib/index.browser-env.test.js +++ b/lib/index.browser-env.test.js @@ -1,6 +1,8 @@ /* global testRule */ -const { ruleName } = require('.'); +import rule from './index.js'; + +const { ruleName } = rule; /** * This test suite uses https://github.com/stylelint/jest-preset-stylelint, @@ -20,7 +22,7 @@ const { ruleName } = require('.'); */ testRule({ - plugins: ['.'], + plugins: ['./lib'], ruleName, config: true, @@ -67,7 +69,7 @@ testRule({ }); testRule({ - plugins: ['.'], + plugins: ['./lib'], ruleName, config: [ true, @@ -89,7 +91,7 @@ testRule({ }); testRule({ - plugins: ['.'], + plugins: ['./lib'], ruleName, config: [ true, @@ -109,63 +111,3 @@ testRule({ }, ], }); - -/** - * The section below tests option validation. It passes code that would be rejected - * with the current env settings. We're passing invalid options which cause the - * validation to pass (since the plugin returns early). - */ - -testRule({ - plugins: ['.'], - ruleName, - config: [ - true, - { - ignorePartialSupport: 'yes', - }, - ], - - accept: [ - { - code: 'div { width: 5rem; }', - description: 'return early if ignorePartialSupport is invalid', - }, - ], -}); - -testRule({ - plugins: ['.'], - ruleName, - config: [ - true, - { - ignore: 10, - }, - ], - - accept: [ - { - code: 'div { width: 5rem; }', - description: 'return early if ignore is invalid', - }, - ], -}); - -testRule({ - plugins: ['.'], - ruleName, - config: [ - true, - { - browsers: 100, - }, - ], - - accept: [ - { - code: 'div { width: 5rem; }', - description: 'return early if browsers is invalid', - }, - ], -}); diff --git a/lib/index.js b/lib/index.js index a0369ba..119c81f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,8 +1,8 @@ -const _ = require('lodash'); -const stylelint = require('stylelint'); +import stylelint from 'stylelint'; // eslint-disable-next-line import/no-unresolved -- https://github.com/import-js/eslint-plugin-import/issues/2703 -const doiuse = require('doiuse'); -const Result = require('postcss/lib/result'); +import doiuse from 'doiuse'; +import pick from 'lodash.pick'; +import { Result } from 'postcss'; /** * Plugin settings @@ -17,10 +17,18 @@ const messages = stylelint.utils.ruleMessages(ruleName, { * Options */ +function isString(value) { + return typeof value === 'string'; +} + +function isBoolean(value) { + return typeof value === 'boolean'; +} + const optionsSchema = { - browsers: [_.isString], - ignore: [_.isString], - ignorePartialSupport: _.isBoolean, + browsers: [isString], + ignore: [isString], + ignorePartialSupport: isBoolean, }; /** @@ -73,7 +81,7 @@ function ruleFunction(on, options) { } const ignorePartialSupport = options ? options.ignorePartialSupport : false; - const doiuseOptions = _.pick(options, Object.keys(optionsSchema)); + const doiuseOptions = pick(options, Object.keys(optionsSchema)); const doiuseResult = new Result(); const usedFeatures = {}; @@ -102,6 +110,9 @@ function ruleFunction(on, options) { }; } -module.exports = stylelint.createPlugin(ruleName, ruleFunction); -module.exports.ruleName = ruleName; -module.exports.messages = messages; +ruleFunction.ruleName = ruleName; +ruleFunction.messages = messages; + +const plugin = stylelint.createPlugin(ruleName, ruleFunction); + +export default plugin; diff --git a/lib/index.test.js b/lib/index.test.js index 010eb05..4db5ba3 100644 --- a/lib/index.test.js +++ b/lib/index.test.js @@ -1,6 +1,8 @@ /* global testRule */ -const { ruleName } = require('.'); +import rule from './index.js'; + +const { ruleName } = rule; /** * This test suite uses https://github.com/stylelint/jest-preset-stylelint, @@ -17,7 +19,7 @@ const { ruleName } = require('.'); // IE 6 testRule({ - plugins: ['.'], + plugins: ['./lib'], ruleName, config: [ true, @@ -39,7 +41,7 @@ testRule({ // IE 8 testRule({ - plugins: ['.'], + plugins: ['./lib'], ruleName, config: [ true, @@ -58,7 +60,7 @@ testRule({ // IE 9 testRule({ - plugins: ['.'], + plugins: ['./lib'], ruleName, config: [ true, @@ -84,7 +86,7 @@ testRule({ // IE 8 and IE 9 testRule({ - plugins: ['.'], + plugins: ['./lib'], ruleName, config: [ true, @@ -103,7 +105,7 @@ testRule({ // IE 6 and IE 7 testRule({ - plugins: ['.'], + plugins: ['./lib'], ruleName, config: [ true, @@ -129,7 +131,7 @@ testRule({ // IE 6 with flexbox ignored testRule({ - plugins: ['.'], + plugins: ['./lib'], ruleName, config: [ true, @@ -149,7 +151,7 @@ testRule({ // IE 6 with flexbox and table ignored testRule({ - plugins: ['.'], + plugins: ['./lib'], ruleName, config: [ true, @@ -173,7 +175,7 @@ testRule({ // IE 11 with partial support ignored testRule({ - plugins: ['.'], + plugins: ['./lib'], ruleName, config: [ true, @@ -193,7 +195,7 @@ testRule({ // IE 7 and IE 10 with partial support ignored testRule({ - plugins: ['.'], + plugins: ['./lib'], ruleName, config: [ true, @@ -216,7 +218,7 @@ testRule({ // IE 6 with partial support ignored testRule({ - plugins: ['.'], + plugins: ['./lib'], ruleName, config: [ true, @@ -239,7 +241,7 @@ testRule({ // IE 11 mix of not supported and partially supported testRule({ - plugins: ['.'], + plugins: ['./lib'], ruleName, config: [ true, diff --git a/package.json b/package.json index e53abde..2c6a4f7 100644 --- a/package.json +++ b/package.json @@ -13,42 +13,44 @@ "prepare": "husky install" }, "peerDependencies": { - "stylelint": "^14.0.0||^15.0.0" + "stylelint": "^16.0.2" }, "dependencies": { - "doiuse": "^6.0.1", - "lodash": "^4.17.15", - "postcss": "^8.4.16" + "doiuse": "^6.0.2", + "lodash.pick": "^4.4.0", + "postcss": "^8.4.32" }, "devDependencies": { - "@commitlint/cli": "17.6.6", - "@commitlint/config-conventional": "17.6.6", + "@commitlint/cli": "18.4.3", + "@commitlint/config-conventional": "18.4.3", "@semantic-release/changelog": "6.0.3", - "@semantic-release/commit-analyzer": "10.0.1", + "@semantic-release/commit-analyzer": "11.1.0", "@semantic-release/git": "10.0.1", - "@semantic-release/github": "9.0.3", - "@semantic-release/npm": "10.0.4", - "@semantic-release/release-notes-generator": "11.0.4", + "@semantic-release/github": "9.2.6", + "@semantic-release/npm": "11.0.2", + "@semantic-release/release-notes-generator": "12.1.0", "codecov": "3.8.3", "cross-env": "7.0.3", - "eslint": "8.44.0", + "eslint": "8.56.0", "eslint-config-airbnb-base": "15.0.0", - "eslint-config-prettier": "8.8.0", - "eslint-plugin-import": "2.27.5", + "eslint-config-prettier": "9.1.0", + "eslint-plugin-import": "2.29.1", "husky": "8.0.3", - "jest": "27.5.1", - "jest-preset-stylelint": "4.2.0", - "lint-staged": "13.2.3", - "prettier": "3.0.0", - "semantic-release": "21.0.7", - "stylelint": "15.10.1" + "jest": "29.7.0", + "jest-light-runner": "^0.6.0", + "jest-preset-stylelint": "7.0.0", + "lint-staged": "15.2.0", + "prettier": "3.1.1", + "semantic-release": "22.0.12", + "stylelint": "16.1.0" }, "author": "RJWadley", "license": "MIT", "engines": { - "node": ">=16" + "node": ">=18.12.0" }, - "main": "lib/index.js", + "type": "module", + "exports": "./lib/index.js", "files": [ "lib/index.js" ],