Skip to content

Commit

Permalink
Merge pull request #1223 from bryceosterhaus/eslintFlatConfig
Browse files Browse the repository at this point in the history
fix(stylelint-plugin): provide plugin via cjs with no build step
  • Loading branch information
bryceosterhaus authored Jun 11, 2024
2 parents ceeedfc + 0540cfb commit e1da82d
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 459 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ function getSupportedTasks(task) {
}

function doctor({
themeConfig = null,
haltOnMissingDeps = false,
tasks = [],
themeConfig = null,
} = {}) {
themeConfig = themeConfig || lfrThemeConfig.getConfig(true);

Expand Down
2 changes: 1 addition & 1 deletion projects/npm-tools/packages/npm-scripts/src/sass/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ function convertAndWriteRTL(cssContent, filePath, outputDir) {

function main(
baseDir,
{appendImportTimestamps = true, excludes, imports = [], rtl, outputDir = ''}
{appendImportTimestamps = true, excludes, imports = [], outputDir = '', rtl}
) {
const {entryFiles, partials} = collectSassFiles(baseDir, excludes);
const cssFiles = expandGlobs(['**/*.css'], excludes, {baseDir});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
* SPDX-License-Identifier: MIT
*/

import plugin from './plugin.mjs';
/**
* SPDX-FileCopyrightText: © 2020 Liferay, Inc. <https://liferay.com>
* SPDX-License-Identifier: MIT
*/

export const config = {
plugins: [plugin],
const plugins = require('./plugins.js');

const defaultConfig = {
rules: {
'at-rule-no-unknown': [
true,
Expand Down Expand Up @@ -76,3 +80,7 @@ export const config = {
'value-keyword-case': 'lower',
},
};

plugins.defaultConfig = defaultConfig;

module.exports = plugins;
10 changes: 0 additions & 10 deletions projects/stylelint-plugin/index.mjs

This file was deleted.

13 changes: 3 additions & 10 deletions projects/stylelint-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
{
"name": "@liferay/stylelint-plugin",
"files": [
"dist"
],
"main": "./dist/index.js",
"main": "./index.js",
"scripts": {
"build": "esbuild index.mjs --bundle --outdir=dist --platform=node \"--external:stylelint\"",
"build": "true",
"format": "liferay-workspace-scripts format",
"format:check": "liferay-workspace-scripts format:check",
"postversion": "liferay-workspace-scripts publish",
"preversion": "liferay-workspace-scripts ci"
},
"version": "1.0.0",
"devDependencies": {
"esbuild": "^0.21.2",
"stylelint": "^16.6.1"
}
"version": "1.0.0"
}
22 changes: 0 additions & 22 deletions projects/stylelint-plugin/plugin.mjs

This file was deleted.

22 changes: 22 additions & 0 deletions projects/stylelint-plugin/plugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* SPDX-FileCopyrightText: © 2020 Liferay, Inc. <https://liferay.com>
* SPDX-License-Identifier: MIT
*/

const stylelint = require('stylelint');

const noBlockComments = require('./rules/no-block-comments.js');
const noImportExtension = require('./rules/no-import-extension.js');
const singleImports = require('./rules/single-imports.js');
const sortImports = require('./rules/sort-imports.js');
const trimComments = require('./rules/trim-comments.js');

const rulesPlugins = [
noBlockComments,
noImportExtension,
singleImports,
sortImports,
trimComments,
].map((rule) => stylelint.createPlugin(rule.ruleName, rule));

module.exports = rulesPlugins;
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
* SPDX-License-Identifier: MIT
*/

import stylelint from 'stylelint';
const stylelint = require('stylelint');

const ruleName = 'liferay/no-block-comments';

const messages = stylelint.utils.ruleMessages(ruleName, {
expected:
'No block-based comments (/* ... */); use line-based (//) comment syntax instead',
Expand All @@ -17,11 +16,9 @@ const rule = (actual) => {
const validOptions = stylelint.utils.validateOptions(result, ruleName, {
actual,
});

if (!validOptions) {
return;
}

root.walkComments((comment) => {
if (!comment.raws.inline) {
stylelint.utils.report({
Expand All @@ -38,4 +35,4 @@ const rule = (actual) => {
rule.ruleName = ruleName;
rule.messages = messages;

export default rule;
module.exports = rule;
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: MIT
*/

import stylelint from 'stylelint';
const stylelint = require('stylelint');

const ruleName = 'liferay/no-import-extension';

Expand All @@ -12,7 +12,6 @@ const messages = stylelint.utils.ruleMessages(ruleName, {
});

const PARAMS_REGEXP = /^(['"]?)(.+?)(['"]?)$/;

const SCSS_EXT = '.scss';

const rule = (options, secondaryOptions, context) => {
Expand All @@ -32,29 +31,27 @@ const rule = (options, secondaryOptions, context) => {
},
}
);

if (!validOptions || !options) {
return;
}

const disableFix = secondaryOptions && secondaryOptions.disableFix;

const fix = context ? context.fix && !disableFix : false;

root.walkAtRules('import', (rule) => {
if (rule.params.startsWith('url(')) {
return;
}

const [, left, params, right] = rule.params.match(PARAMS_REGEXP);

if (params.endsWith(SCSS_EXT)) {
const desired =
left + params.slice(0, -SCSS_EXT.length) + right;

if (fix) {
rule.replaceWith(rule.clone({params: desired}));
} else {
rule.replaceWith(
rule.clone({
params: desired,
})
);
}
else {
stylelint.utils.report({
message: messages.extension,
node: rule,
Expand All @@ -70,4 +67,4 @@ const rule = (options, secondaryOptions, context) => {
rule.ruleName = ruleName;
rule.messages = messages;

export default rule;
module.exports = rule;
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: MIT
*/

import stylelint from 'stylelint';
const stylelint = require('stylelint');

const ruleName = 'liferay/single-imports';

Expand All @@ -25,27 +25,21 @@ function tokenize(params) {
// Slow but simple iteration through string.

let i = 0;

for (; i < params.length; ) {
const remaining = params.slice(i);

const matched = MATCHERS.some(([kind, matcher]) => {
const match = matcher.exec(remaining);

if (match) {
const text = match[0];

tokens.push({
kind,
text: match[0],
});

i += text.length;

return true;
}
});

if (!matched) {
throw new Error(
`Unable to tokenize ${JSON.stringify(params)} at index ${i}`
Expand All @@ -55,7 +49,6 @@ function tokenize(params) {

return tokens;
}

const rule = (options, secondaryOptions, context) => {
return function (root, result) {
const validOptions = stylelint.utils.validateOptions(
Expand All @@ -73,32 +66,27 @@ const rule = (options, secondaryOptions, context) => {
},
}
);

if (!validOptions || !options) {
return;
}

const disableFix = secondaryOptions && secondaryOptions.disableFix;

const fix = context ? context.fix && !disableFix : false;

root.walkAtRules('import', (rule) => {
const tokens = tokenize(rule.params);

const resources = tokens.filter((token) => {
return (
token.kind === 'DOUBLE_QUOTED_STRING' ||
token.kind === 'SINGLE_QUOTED_STRING' ||
token.kind === 'URL'
);
});

if (resources.length > 1) {
if (fix) {
const replacements = resources.map((resource) =>
rule.clone({params: resource.text})
rule.clone({
params: resource.text,
})
);

rule.replaceWith(...replacements);

// If the original rule had a blank line before it, we
Expand All @@ -112,7 +100,6 @@ const rule = (options, secondaryOptions, context) => {
const trailing = /(.*?)(\n+)([ \t]*)$/.exec(
rule.raws.before
);

replacements.forEach((resource, i) => {
if (i) {
if (trailing && trailing[2].length > 1) {
Expand All @@ -123,7 +110,8 @@ const rule = (options, secondaryOptions, context) => {
}
}
});
} else {
}
else {
stylelint.utils.report({
message: messages.single,
node: rule,
Expand All @@ -139,4 +127,4 @@ const rule = (options, secondaryOptions, context) => {
rule.ruleName = ruleName;
rule.messages = messages;

export default rule;
module.exports = rule;
Loading

0 comments on commit e1da82d

Please sign in to comment.