Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare release v3.0.0 #40

Merged
merged 17 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*!
* Gulp Stylelint (v2.2.0): .eslintrc.cjs
* Gulp Stylelint (v3.0.0): .eslintrc.cjs
* Copyright (c) 2023-24 Adorade (https://github.com/adorade/gulp-stylelint-esm)
* License under MIT
* ========================================================================== */

module.exports = {
parser: "@babel/eslint-parser",
parser: '@babel/eslint-parser',
env: {
node: true,
es6: true
Expand All @@ -23,10 +23,24 @@ module.exports = {
overrides: [
{
files: [
"test/*.test.js"
'src/*.mjs',
'test/*.test.js'
],
rules: {
'@stylistic/js/semi': ['error', 'always'],
}
},
{
files: [
'test/*.test.js',
'test/*.spec.js',
'bin/*.spec.js'
],
env: {
jest: true
},
rules: {
'@stylistic/js/arrow-parens': ['error', 'always'],
}
}
]
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Folders to ignore
node_modules
archive
bin

# To do Tasks
.todo
Expand Down
80 changes: 42 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,17 @@ import gStylelintEsm from 'gulp-stylelint-esm';

function lintCssTask() {
return src('src/**/*.css')
.pipe(gStylelintEsm({
reporters: [
{ formatter: 'string', console: true }
]
}));
.pipe(gStylelintEsm());
}
```

## Formatters

Below is the list of currently available **stylelint formatters**. Some of them are bundled with stylelint by default and exposed on `gStylelintEsm.formatters` object. Others need to be installed. You can [write a custom formatter](http://stylelint.io/developer-guide/formatters/) to tailor the reporting to your needs.

Formatters bundled with stylelint: `"compact"`, `"github"`, `"json"`, `"string (default)"`, `"tap"`, `"unix"`, `"verbose"`.
Formatters bundled with stylelint: `"compact", "github", "json", "string", "tap", "unix", "verbose"`.

The plugin comes with a built-in formatter called `"stylish"`, which is set as the **default**.

## Options

Expand All @@ -66,69 +64,75 @@ import { myStylelintFormatter } from 'my-stylelint-formatter';
function lintCssTask() {
return src('src/**/*.css')
.pipe(gStylelintEsm({
failAfterError: true,
reportOutputDir: 'reports/lint',
failAfterError: true, // true (default) | false
fix: false, // false (default) | true
reporters: [
{ formatter: 'verbose', console: true },
{ formatter: 'stylish', console: true }, // default
{ formatter: 'json', save: 'report.json' },
{ formatter: myStylelintFormatter, save: 'my-custom-report.txt' }
],
debug: true
debug: false // false (default) | true
}));
}
```

### `failAfterError`

When set to `true`, the process will end with non-zero error code if any error-level warnings were raised. Defaults to `true`.
When set to `true`, the process will end with non-zero error code if any error-level warnings were raised. Files are pushed back to the their pipes only if there are no errors. Defaults to `true`.

### `fix`

The `fix: true` (autofix) option instructs stylelint to try to fix as many issues as possible. Defaults to `false`.

### `reportOutputDir`
> NOTE:
> - fixed files will automatically overwrite the original files; proceed with caution.
> - the fixes are applied to the gulp stream only if there are no errors, to allow usage of other plugins.
> - not all stylelint rules can be automatically fixed, so it's advisable to manually resolve errors.

Base directory for lint results written to filesystem. Defaults to **current working directory** `process.cwd()`.
```js
import { src, dest } from 'gulp';
import gStylelintEsm from 'gulp-stylelint-esm';

function fixCssTask() {
return src('src/**/*.css')
.pipe(gStylelintEsm({
fix: true
}))
.pipe(dest('src'));
}
```

### `reporters`

List of reporter configuration objects (see below). Defaults to **an empty array** `[]`.
List of reporter configuration objects (see below). Defaults to:

```js
reporters: [
{ formatter: 'stylish', console: true }
]
```

```js
{
// stylelint results formatter (required):
// - pass a built-in formatter
// - pass a function for imported, custom or exposed formatters
// - pass a string for formatters bundled with stylelint
// "string (default)", "compact", "github", "json", "tap", "unix", "verbose"
formatter: myFormatter,

// save the formatted result to a file (optional):
save: 'text-report.txt',
// "stylish (default)", "string", "compact", "github", "json", "tap", "unix", "verbose"
formatter: stylish,

// log the formatted result to console (optional):
console: true

// save the formatted result to a file (optional):
save: 'text-report.txt',
}
```

### `debug`

When set to `true`, the error handler will print an error stack trace. Defaults to `false`.

## Autofix

The `fix: true` option instructs stylelint to try to fix as many issues as possible. The fixes are applied to the gulp stream. The fixed content can be saved to file using gulp `dest`.

> NOTE: Not all stylelint rules can be automatically fixed, so it's advisable to manually resolve errors.

```js
import { src, dest } from 'gulp';
import gStylelintEsm from 'gulp-stylelint-esm';

function fixCssTask() {
return src('src/**/*.css')
.pipe(gStylelintEsm({
fix: true
}))
.pipe(dest('src'));
}
```

## License

See the [LICENSE](LICENSE) file for license rights and limitations (MIT).
2 changes: 1 addition & 1 deletion .babelrc.cjs → babel.config.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Gulp Stylelint (v2.2.0): .babelrc.cjs
* Gulp Stylelint (v3.0.0): babel.config.cjs
* Copyright (c) 2023-24 Adorade (https://github.com/adorade/gulp-stylelint-esm)
* License under MIT
* ========================================================================== */
Expand Down
11 changes: 8 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
/*!
* Gulp Stylelint (v2.2.0): jest.config.js
* Gulp Stylelint (v3.0.0): jest.config.js
* Copyright (c) 2023-24 Adorade (https://github.com/adorade/gulp-stylelint-esm)
* License under MIT
* ========================================================================== */

/** @type {import('jest').Config} */
const config = {
displayName: {
name: "Gulp Stylelint Plugin",
color: "cyan",
},
clearMocks: true,
// collectCoverage: true,
collectCoverageFrom: [
"src/*.mjs"
],
coverageDirectory: "./.coverage/",
coverageReporters: [
"html",
"lcov",
"text"
],
coverageThreshold: {
Expand All @@ -25,6 +27,9 @@ const config = {
statements: 75
}
},
// fakeTimers: {
// enableGlobally: true,
// },
transform: {
"\\.mjs?$": "babel-jest"
},
Expand Down
30 changes: 21 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gulp-stylelint-esm",
"version": "2.2.0",
"version": "3.0.0",
"description": "Gulp plugin for stylelint with ES module support.",
"author": {
"name": "Adorade",
Expand All @@ -10,13 +10,21 @@
"homepage": "https://github.com/adorade/gulp-stylelint-esm",
"repository": {
"type": "git",
"url": "https://github.com/adorade/gulp-stylelint-esm.git"
"url": "git+https://github.com/adorade/gulp-stylelint-esm.git"
},
"type": "module",
"exports": "./src/index.mjs",
"exports": {
".": {
"types": "./types/index.d.mts",
"default": "./src/index.mjs"
},
"./package.json": "./package.json"
},
"files": [
"/src/*.mjs"
"src/*.mjs",
"types/*.d.mts"
],
"types": "types",
"keywords": [
"gulpplugin",
"gulpfriendly",
Expand All @@ -28,6 +36,7 @@
],
"scripts": {
"lint": "eslint {src,test}/**/*.{mjs,js}",
"lint:fix": "eslint {src,test}/**/*.{mjs,js} --fix",
"jest": "node --experimental-vm-modules --no-warnings node_modules/jest/bin/jest.js -i",
"jest:coverage": "yarn run jest --coverage",
"test": "yarn run lint && yarn run jest",
Expand All @@ -37,24 +46,27 @@
"stylelint": "16"
},
"dependencies": {
"@adorade/plugin-error": "1.0.0",
"@adorade/plugin-error": "2.0.1",
"@jridgewell/trace-mapping": "0.3.25",
"ansi-colors": "4.1.3",
"fancy-log": "2.0.0"
"fancy-log": "2.0.0",
"text-table": "0.2.0",
"write-file-atomic": "6.0.0"
},
"devDependencies": {
"@babel/core": "7.25.2",
"@babel/eslint-parser": "7.25.1",
"@babel/preset-env": "7.25.3",
"@babel/preset-env": "7.25.4",
"babel-jest": "29.7.0",
"eslint": "8.57.0",
"eslint-config-stylelint": "22.0.0",
"gulp": "5.0.0",
"gulp-clean-css": "4.3.0",
"gulp-concat": "2.6.1",
"jest": "29.7.0",
"sinon": "18.0.0",
"stylelint": "16.8.1"
"sinon": "19.0.2",
"stylelint": "16.9.0",
"vinyl": "3.0.0"
},
"engines": {
"node": ">=18.12.0"
Expand Down
47 changes: 22 additions & 25 deletions src/apply-sourcemap.mjs
Original file line number Diff line number Diff line change
@@ -1,48 +1,46 @@
/*!
* Gulp Stylelint (v2.2.0): src/apply-sourcemap.mjs
* Gulp Stylelint (v3.0.0): src/apply-sourcemap.mjs
* Copyright (c) 2023-24 Adorade (https://github.com/adorade/gulp-stylelint-esm)
* License under MIT
* ========================================================================== */

import {TraceMap, originalPositionFor} from '@jridgewell/trace-mapping';

/**
* Applies a sourcemap to stylelint lint results, updating warning positions.
* @param {Object} lintResult - Stylelint lint results.
* @param {Object} sourceMap - Source map object.
* @returns {Promise<Object>} - Promise resolving to updated lint results with sourcemap applied.
* @typedef {Object} Warning
* @property {number} line
* @property {number} column
*
* @typedef {Object} Result
* @property {string} source
* @property {Warning[]} warnings
*
* @typedef {Object} LintResult
* @property {Result[]} results
*/

/**
* Applies source map to the lint result.
*
* @param {LintResult} lintResult - The result of the linting process
* @param {Object} sourceMap - The source map object
* @returns {Promise<LintResult>} The lint result with applied source map
*/
export default async function applySourcemap(lintResult, sourceMap) {
/**
* Source map consumer for the provided source map.
* @type {TraceMap}
*/
/** @type {TraceMap} */
const sourceMapConsumer = new TraceMap(sourceMap);

/**
* Applies sourcemap to lint results, updating warning positions.
* @type {Object[]}
*/
lintResult.results = lintResult.results.reduce((memo, result) => {
if (result.warnings.length) {
result.warnings.forEach(warning => {
/**
* Original position information for the warning from the sourcemap.
* @type {Object}
*/
/** @type {import('@jridgewell/trace-mapping').OriginalMapping} */
const origPos = originalPositionFor(sourceMapConsumer, warning);

/**
* Index of a result with the same source in the memo array.
* @type {number}
*/
const sameSourceResultIndex = memo.findIndex(r => r.source === origPos.source);

// Update warning properties with original position information
warning.line = origPos.line;
warning.column = origPos.column;

// Add or update result in the memo array based on the source file
if (sameSourceResultIndex === -1) {
memo.push(Object.assign({}, result, {
source: origPos.source,
Expand All @@ -57,8 +55,7 @@ export default async function applySourcemap(lintResult, sourceMap) {
}

return memo;
}, []);
}, /** @type {Result[]} */ ([]));

// Return the updated lint results after applying the source map
return lintResult;
}
Loading