Skip to content

Commit

Permalink
feat: support stylelint 16 (#296)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
nix6839 committed Jan 2, 2024
1 parent f398e0b commit adcd166
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 118 deletions.
2 changes: 1 addition & 1 deletion .commitlintrc.js
@@ -1,3 +1,3 @@
module.exports = {
export default {
extends: ['@commitlint/config-conventional'],
};
9 changes: 8 additions & 1 deletion .eslintrc
@@ -1,6 +1,13 @@
{
"extends": ["airbnb-base", "prettier"],
"parserOptions": {
"sourceType": "module"
},
"env": {
"node": true
"node": true,
"es2022": true
},
"rules": {
"import/extensions": ["error", "always"]
}
}
2 changes: 1 addition & 1 deletion .github/workflows/commitlint.yml
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/node.yml
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion 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;
70 changes: 6 additions & 64 deletions 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,
Expand All @@ -20,7 +22,7 @@ const { ruleName } = require('.');
*/

testRule({
plugins: ['.'],
plugins: ['./lib'],
ruleName,
config: true,

Expand Down Expand Up @@ -67,7 +69,7 @@ testRule({
});

testRule({
plugins: ['.'],
plugins: ['./lib'],
ruleName,
config: [
true,
Expand All @@ -89,7 +91,7 @@ testRule({
});

testRule({
plugins: ['.'],
plugins: ['./lib'],
ruleName,
config: [
true,
Expand All @@ -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',
},
],
});
33 changes: 22 additions & 11 deletions 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
Expand All @@ -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,
};

/**
Expand Down Expand Up @@ -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 = {};
Expand Down Expand Up @@ -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;
26 changes: 14 additions & 12 deletions 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,
Expand All @@ -17,7 +19,7 @@ const { ruleName } = require('.');

// IE 6
testRule({
plugins: ['.'],
plugins: ['./lib'],
ruleName,
config: [
true,
Expand All @@ -39,7 +41,7 @@ testRule({

// IE 8
testRule({
plugins: ['.'],
plugins: ['./lib'],
ruleName,
config: [
true,
Expand All @@ -58,7 +60,7 @@ testRule({

// IE 9
testRule({
plugins: ['.'],
plugins: ['./lib'],
ruleName,
config: [
true,
Expand All @@ -84,7 +86,7 @@ testRule({

// IE 8 and IE 9
testRule({
plugins: ['.'],
plugins: ['./lib'],
ruleName,
config: [
true,
Expand All @@ -103,7 +105,7 @@ testRule({

// IE 6 and IE 7
testRule({
plugins: ['.'],
plugins: ['./lib'],
ruleName,
config: [
true,
Expand All @@ -129,7 +131,7 @@ testRule({

// IE 6 with flexbox ignored
testRule({
plugins: ['.'],
plugins: ['./lib'],
ruleName,
config: [
true,
Expand All @@ -149,7 +151,7 @@ testRule({

// IE 6 with flexbox and table ignored
testRule({
plugins: ['.'],
plugins: ['./lib'],
ruleName,
config: [
true,
Expand All @@ -173,7 +175,7 @@ testRule({

// IE 11 with partial support ignored
testRule({
plugins: ['.'],
plugins: ['./lib'],
ruleName,
config: [
true,
Expand All @@ -193,7 +195,7 @@ testRule({

// IE 7 and IE 10 with partial support ignored
testRule({
plugins: ['.'],
plugins: ['./lib'],
ruleName,
config: [
true,
Expand All @@ -216,7 +218,7 @@ testRule({

// IE 6 with partial support ignored
testRule({
plugins: ['.'],
plugins: ['./lib'],
ruleName,
config: [
true,
Expand All @@ -239,7 +241,7 @@ testRule({

// IE 11 mix of not supported and partially supported
testRule({
plugins: ['.'],
plugins: ['./lib'],
ruleName,
config: [
true,
Expand Down

0 comments on commit adcd166

Please sign in to comment.