Skip to content

Commit

Permalink
Convert to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
hudochenkov committed Apr 17, 2024
1 parent 3ffad4b commit 3a6ece9
Show file tree
Hide file tree
Showing 63 changed files with 271 additions and 277 deletions.
12 changes: 12 additions & 0 deletions .eslintrc.json
Expand Up @@ -9,8 +9,20 @@
"es6": true,
"jest": true
},
"parserOptions": {
"sourceType": "module",
"ecmaVersion": "latest"
},
"globals": {
"testConfig": true,
"testRule": true
},
"rules": {
"import/extensions": ["error", "always", { "ignorePackages": true } ],
"import/no-default-export": "error",
"import/no-useless-path-segments": ["warn", { "commonjs": false, "noUselessIndex": false }],
"import/prefer-default-export": "off",
"unicorn/prefer-module": "error",
"unicorn/prefer-node-protocol": "error"
}
}
11 changes: 6 additions & 5 deletions index.js
@@ -1,9 +1,10 @@
const { createPlugin } = require('stylelint');
const { namespace } = require('./utils');
const rules = require('./rules');
import stylelint from 'stylelint';
import { rules } from './rules/index.js';
import { namespace } from './utils/namespace.js';

const rulesPlugins = Object.keys(rules).map((ruleName) => {
return createPlugin(namespace(ruleName), rules[ruleName]);
return stylelint.createPlugin(namespace(ruleName), rules[ruleName]);
});

module.exports = rulesPlugins;
// eslint-disable-next-line import/no-default-export
export default rulesPlugins;
5 changes: 2 additions & 3 deletions jest-setup.js
@@ -1,6 +1,6 @@
const stylelint = require('stylelint');
import stylelint from 'stylelint';
// eslint-disable-next-line import/no-extraneous-dependencies
const getTestRule = require('jest-preset-stylelint/getTestRule');
import { getTestRule } from 'jest-preset-stylelint';

global.testRule = getTestRule({ plugins: ['./'] });

Expand All @@ -27,7 +27,6 @@ global.testConfig = (input) => {
.lint({
code: '',
config,
quietDeprecationWarnings: true,
})
.then((data) => {
const { invalidOptionWarnings } = data.results[0];
Expand Down
14 changes: 9 additions & 5 deletions package.json
Expand Up @@ -19,13 +19,17 @@
"index.js",
"!.DS_Store"
],
"main": "index.js",
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=18.12.0"
},
"dependencies": {
"postcss": "^8.4.38",
"postcss-sorting": "^8.0.2"
},
"peerDependencies": {
"stylelint": "^14.0.0 || ^15.0.0 || ^16.0.1"
"stylelint": "^16.3.1"
},
"devDependencies": {
"eslint": "^8.55.0",
Expand All @@ -46,9 +50,9 @@
},
"scripts": {
"lint": "eslint . --max-warnings=0 && prettier '**/*.js' --check",
"test": "jest",
"watch": "jest --watch",
"coverage": "jest --coverage",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
"watch": "npm run test -- --watch",
"coverage": "npm run test -- --coverage",
"fix": "eslint . --fix --max-warnings=0 && prettier '**/*.js' --write",
"prepare": "husky"
},
Expand Down
8 changes: 4 additions & 4 deletions rules/checkAlphabeticalOrder.js
@@ -1,13 +1,13 @@
const shorthandData = require('./shorthandData');
const { vendor } = require('../utils');
import { shorthandData } from './shorthandData.js';
import * as vendor from '../utils/vendor.js';

function isShorthand(a, b) {
const longhands = shorthandData[a] || [];

return longhands.includes(b);
}

module.exports = function checkAlphabeticalOrder(firstPropData, secondPropData) {
export function checkAlphabeticalOrder(firstPropData, secondPropData) {
// OK if the first is shorthand for the second:
if (isShorthand(firstPropData.unprefixedName, secondPropData.unprefixedName)) {
return true;
Expand All @@ -32,4 +32,4 @@ module.exports = function checkAlphabeticalOrder(firstPropData, secondPropData)
}

return firstPropData.unprefixedName < secondPropData.unprefixedName;
};
}
12 changes: 8 additions & 4 deletions rules/index.js
@@ -1,5 +1,9 @@
module.exports = {
order: require('./order'),
'properties-order': require('./properties-order'),
'properties-alphabetical-order': require('./properties-alphabetical-order'),
import { rule as order } from './order/index.js';
import { rule as propertiesOrder } from './properties-order/index.js';
import { rule as propertiesAlphabeticalOrder } from './properties-alphabetical-order/index.js';

export const rules = {
order,
'properties-order': propertiesOrder,
'properties-alphabetical-order': propertiesAlphabeticalOrder,
};
4 changes: 2 additions & 2 deletions rules/order/calcAtRulePatternPriority.js
@@ -1,4 +1,4 @@
module.exports = function calcAtRulePatternPriority(pattern, node) {
export function calcAtRulePatternPriority(pattern, node) {
// 0 — it pattern doesn't match
// 1 — pattern without `name` and `hasBlock`
// 10010 — pattern match `hasBlock`
Expand Down Expand Up @@ -57,4 +57,4 @@ module.exports = function calcAtRulePatternPriority(pattern, node) {
}

return priority;
};
}
4 changes: 2 additions & 2 deletions rules/order/calcRulePatternPriority.js
@@ -1,4 +1,4 @@
module.exports = function calcRulePatternPriority(pattern, node) {
export function calcRulePatternPriority(pattern, node) {
// 0 — it pattern doesn't match
// 1 — pattern without `selector`
// 2 — pattern match `selector`
Expand All @@ -16,4 +16,4 @@ module.exports = function calcRulePatternPriority(pattern, node) {
}

return priority;
};
}
25 changes: 9 additions & 16 deletions rules/order/checkNode.js
@@ -1,18 +1,11 @@
const stylelint = require('stylelint');
const sortNode = require('postcss-sorting/lib/order/sortNode');
const checkOrder = require('./checkOrder');
const getOrderData = require('./getOrderData');
const ruleName = require('./ruleName');
const messages = require('./messages');

module.exports = function checkNode({
node,
isFixEnabled,
orderInfo,
primaryOption,
result,
unspecified,
}) {
import stylelint from 'stylelint';
import sortNode from 'postcss-sorting/lib/order/sortNode.js';
import { checkOrder } from './checkOrder.js';
import { getOrderData } from './getOrderData.js';
import { ruleName } from './ruleName.js';
import { messages } from './messages.js';

export function checkNode({ node, isFixEnabled, orderInfo, primaryOption, result, unspecified }) {
if (isFixEnabled) {
let shouldFix = false;
let allNodesData = [];
Expand Down Expand Up @@ -104,4 +97,4 @@ module.exports = function checkNode({
previousNodeData,
};
}
};
}
10 changes: 5 additions & 5 deletions rules/order/checkOrder.js
@@ -1,9 +1,9 @@
let stylelint = require('stylelint');
let ruleName = require('./ruleName');
let messages = require('./messages');
import stylelint from 'stylelint';
import { ruleName } from './ruleName.js';
import { messages } from './messages.js';

// eslint-disable-next-line max-params, consistent-return
module.exports = function checkOrder({
export function checkOrder({
firstNodeData,
secondNodeData,
allNodesData,
Expand Down Expand Up @@ -75,4 +75,4 @@ module.exports = function checkOrder({
if (unspecified === 'bottom' && !firstNodeIsSpecified) {
return false;
}
};
}
8 changes: 4 additions & 4 deletions rules/order/createOrderInfo.js
@@ -1,7 +1,7 @@
const getDescription = require('./getDescription');
const { isString } = require('../../utils/validateType');
import { getDescription } from './getDescription.js';
import { isString } from '../../utils/validateType.js';

module.exports = function createOrderInfo(input) {
export function createOrderInfo(input) {
let order = {};
let expectedPosition = 0;

Expand Down Expand Up @@ -85,4 +85,4 @@ module.exports = function createOrderInfo(input) {
});

return order;
};
}
6 changes: 3 additions & 3 deletions rules/order/getDescription.js
@@ -1,6 +1,6 @@
const { isObject } = require('../../utils/validateType');
import { isObject } from '../../utils/validateType.js';

module.exports = function getDescription(item) {
export function getDescription(item) {
const descriptions = {
'custom-properties': 'custom property',
'dollar-variables': '$-variable',
Expand Down Expand Up @@ -48,4 +48,4 @@ module.exports = function getDescription(item) {

// Return description for keyword patterns
return descriptions[item];
};
}
28 changes: 16 additions & 12 deletions rules/order/getOrderData.js
@@ -1,21 +1,25 @@
const utils = require('../../utils');
const calcAtRulePatternPriority = require('./calcAtRulePatternPriority');
const calcRulePatternPriority = require('./calcRulePatternPriority');
const getDescription = require('./getDescription');

module.exports = function getOrderData(orderInfo, node) {
import { calcAtRulePatternPriority } from './calcAtRulePatternPriority.js';
import { calcRulePatternPriority } from './calcRulePatternPriority.js';
import { getDescription } from './getDescription.js';
import { isAtVariable } from '../../utils/isAtVariable.js';
import { isCustomProperty } from '../../utils/isCustomProperty.js';
import { isDollarVariable } from '../../utils/isDollarVariable.js';
import { isLessMixin } from '../../utils/isLessMixin.js';
import { isStandardSyntaxProperty } from '../../utils/isStandardSyntaxProperty.js';

export function getOrderData(orderInfo, node) {
let nodeType;

if (utils.isAtVariable(node)) {
if (isAtVariable(node)) {
nodeType = 'at-variables';
} else if (utils.isLessMixin(node)) {
} else if (isLessMixin(node)) {
nodeType = 'less-mixins';
} else if (node.type === 'decl') {
if (utils.isCustomProperty(node.prop)) {
if (isCustomProperty(node.prop)) {
nodeType = 'custom-properties';
} else if (utils.isDollarVariable(node.prop)) {
} else if (isDollarVariable(node.prop)) {
nodeType = 'dollar-variables';
} else if (utils.isStandardSyntaxProperty(node.prop)) {
} else if (isStandardSyntaxProperty(node.prop)) {
nodeType = 'declarations';
}
} else if (node.type === 'rule') {
Expand Down Expand Up @@ -89,4 +93,4 @@ module.exports = function getOrderData(orderInfo, node) {
return {
description: getDescription(nodeType),
};
};
}
19 changes: 9 additions & 10 deletions rules/order/index.js
@@ -1,12 +1,13 @@
const stylelint = require('stylelint');
const { getContainingNode, isRuleWithNodes } = require('../../utils');
const checkNode = require('./checkNode');
const createOrderInfo = require('./createOrderInfo');
const validatePrimaryOption = require('./validatePrimaryOption');
const ruleName = require('./ruleName');
const messages = require('./messages');
import stylelint from 'stylelint';
import { getContainingNode } from '../../utils/getContainingNode.js';
import { isRuleWithNodes } from '../../utils/isRuleWithNodes.js';
import { checkNode } from './checkNode.js';
import { createOrderInfo } from './createOrderInfo.js';
import { validatePrimaryOption } from './validatePrimaryOption.js';
import { ruleName } from './ruleName.js';
import { messages } from './messages.js';

function rule(primaryOption, options = {}, context = {}) {
export function rule(primaryOption, options = {}, context = {}) {
return function ruleBody(root, result) {
let validOptions = stylelint.utils.validateOptions(
result,
Expand Down Expand Up @@ -61,5 +62,3 @@ function rule(primaryOption, options = {}, context = {}) {
rule.ruleName = ruleName;
rule.messages = messages;
rule.primaryOptionArray = true;

module.exports = rule;
6 changes: 3 additions & 3 deletions rules/order/messages.js
@@ -1,6 +1,6 @@
const stylelint = require('stylelint');
const ruleName = require('./ruleName');
import stylelint from 'stylelint';
import { ruleName } from './ruleName.js';

module.exports = stylelint.utils.ruleMessages(ruleName, {
export const messages = stylelint.utils.ruleMessages(ruleName, {
expected: (first, second) => `Expected ${first} to come before ${second}`,
});
4 changes: 2 additions & 2 deletions rules/order/ruleName.js
@@ -1,3 +1,3 @@
const { namespace } = require('../../utils');
import { namespace } from '../../utils/namespace.js';

module.exports = namespace('order');
export const ruleName = namespace('order');
2 changes: 1 addition & 1 deletion rules/order/tests/index.js
@@ -1,4 +1,4 @@
const rule = require('..');
import { rule } from '../index.js';

const { ruleName, messages } = rule;

Expand Down
2 changes: 1 addition & 1 deletion rules/order/tests/validate-options.js
@@ -1,4 +1,4 @@
const { ruleName } = require('..');
import { ruleName } from '../ruleName.js';

testConfig({
ruleName,
Expand Down
6 changes: 3 additions & 3 deletions rules/order/validatePrimaryOption.js
@@ -1,6 +1,6 @@
const { isObject, isString } = require('../../utils/validateType');
import { isObject, isString } from '../../utils/validateType.js';

module.exports = function validatePrimaryOption(actualOptions) {
export function validatePrimaryOption(actualOptions) {
// Otherwise, begin checking array options
if (!Array.isArray(actualOptions)) {
return false;
Expand Down Expand Up @@ -78,7 +78,7 @@ module.exports = function validatePrimaryOption(actualOptions) {
}

return true;
};
}

function isRegExp(value) {
return Object.prototype.toString.call(value) === '[object RegExp]';
Expand Down
12 changes: 7 additions & 5 deletions rules/properties-alphabetical-order/checkNode.js
@@ -1,9 +1,11 @@
let stylelint = require('stylelint');
let checkAlphabeticalOrder = require('../checkAlphabeticalOrder');
let { isStandardSyntaxProperty, isCustomProperty, vendor } = require('../../utils');
import stylelint from 'stylelint';
import { checkAlphabeticalOrder } from '../checkAlphabeticalOrder.js';
import { isCustomProperty } from '../../utils/isCustomProperty.js';
import { isStandardSyntaxProperty } from '../../utils/isStandardSyntaxProperty.js';
import * as vendor from '../../utils/vendor.js';

// eslint-disable-next-line max-params
module.exports = function checkNode(node, result, ruleName, messages) {
export function checkNode(node, result, ruleName, messages) {
let allPropData = [];

node.each(function processEveryNode(child) {
Expand Down Expand Up @@ -58,4 +60,4 @@ module.exports = function checkNode(node, result, ruleName, messages) {
ruleName,
});
});
};
}

0 comments on commit 3a6ece9

Please sign in to comment.