Skip to content

Commit

Permalink
Merge pull request #21 from nickdeis/eslint-9
Browse files Browse the repository at this point in the history
Add support and testing for eslint 9
  • Loading branch information
nickdeis authored Jun 9, 2024
2 parents 2c6d315 + 08e8f95 commit bab9d6b
Show file tree
Hide file tree
Showing 14 changed files with 2,890 additions and 1,343 deletions.
5 changes: 4 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
tests/*
/node_modules/
*.log
/tmp/
/tmp/
staging/
.travis.yml
.eslintrc
130 changes: 82 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,72 +1,110 @@
[![Build Status](https://travis-ci.org/nickdeis/eslint-plugin-notice.svg)](https://travis-ci.org/nickdeis/eslint-plugin-notice)

# eslint-plugin-notice

An eslint rule that checks the top of files and `--fix` them too!

## Usage

`npm i eslint-plugin-notice`
`npm i -D eslint-plugin-notice`

### Flag config

_eslint.config.js_

```js
import notice from "eslint-plugin-notice";

export default [
{
files: ["**/*.js"],
plugins: {
notice,
},
rules: {
"notice/notice": [
"error",
{ mustMatch: "Copyright \\(c\\) [0-9]{0,4}, Nick Deis" },
],
},
},
];
```

### eslintrc

Throw an error when a file doesn't have copyright notice

```json
{
"plugins":["notice"],
"rules":{
"notice/notice":["error",{"mustMatch":"Copyright \\(c\\) [0-9]{0,4}, Nick Deis"}]
}
"plugins": ["notice"],
"rules": {
"notice/notice": [
"error",
{ "mustMatch": "Copyright \\(c\\) [0-9]{0,4}, Nick Deis" }
]
}
}
```

Add a template to `--fix` it

```json
{
"notice/notice":["error",
{
"mustMatch":"Copyright \\(c\\) [0-9]{0,4}, Nick Deis",
"template":"/** Copyright (c) <%= YEAR %>, Nick Deis **/"
}
]
"notice/notice": [
"error",
{
"mustMatch": "Copyright \\(c\\) [0-9]{0,4}, Nick Deis",
"template": "/** Copyright (c) <%= YEAR %>, Nick Deis **/"
}
]
}
```

or use a file

*config/copyright.js*
_config/copyright.js_

```js
/**
* Copyright (c) <%= YEAR %>, Nick Deis
*/
```

```json
{
"notice/notice":["error",
{
"mustMatch":"Copyright \\(c\\) [0-9]{0,4}, Nick Deis",
"templateFile":"config/copyright.js"
}
]
"notice/notice": [
"error",
{
"mustMatch": "Copyright \\(c\\) [0-9]{0,4}, Nick Deis",
"templateFile": "config/copyright.js"
}
]
}
```

or just use your template, eslint-plugin-notice will reverse into a pattern for `mustMatch`

```json
{
"notice/notice":["error",
{
"templateFile":"config/copyright.js"
}
]
"notice/notice": [
"error",
{
"templateFile": "config/copyright.js"
}
]
}
```

Want a more expressive template? Add `templateVars` and `varRegexps`
*config/copyright.js*
_config/copyright.js_

```js
/**
* Copyright (c) <%= YEAR %>, <%= NAME %>
*/
```

```js
{
"notice/notice":["error",
Expand All @@ -81,33 +119,30 @@ Want a more expressive template? Add `templateVars` and `varRegexps`
}
```


## Options

|Option|Description|Default/Required/Optional|Type|
|------|-----------|----------------|----|
|mustMatch|A pattern that must be present in the notice|**Required** unless `template` is set|RegExp/string|
|template|A lodash template that will be used to fix files that do not match `mustMatch` or are less than `nonMatchingTolerance`|**Optional** unless `mustMatch` is not set|string|
|templateFile|`template` will override this setting. A file which contains the `template`|**Optional**|string|
|chars|The number of characters to check for the `mustMatch` pattern|`1000`|number|
|templateVars|The variables to be used with the lodash template, always contains the variable YEAR|`{YEAR:new Date().getFullYear()}`|object|
|[onNonMatchingHeader](#onnonmatchingheader)|Action that should be taken when there is a header comment, but it does not match `mustMatch` or is less than `nonMatchingTolerance`|`"prepend"`|string|
|nonMatchingTolerance|Optional fallback for `mustMatch`. Compares a non-matching header comment (if it exists) to the resolved template using [Metric Longest Common Subsequence](http://heim.ifi.uio.no/~danielry/StringMetric.pdf). `1` means the strings must be exactly the same, where anything less is varying degrees of dissimiliar. `.70` seems like a good choice|**Optional**|number between 0 and 1|
|varRegexps|If `mustMatch` is not set and `template` is set, a regexp that will be replaced in the `template` to create a regexp for `mustMatch`|`{YEAR:/20\d{2}/}`|object|
|messages|Allows you to change the error messages. See [messages](#messages)|**Optional**|object|


| Option | Description | Default/Required/Optional | Type |
| ------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------ | ---------------------- |
| mustMatch | A pattern that must be present in the notice | **Required** unless `template` is set | RegExp/string |
| template | A lodash template that will be used to fix files that do not match `mustMatch` or are less than `nonMatchingTolerance` | **Optional** unless `mustMatch` is not set | string |
| templateFile | `template` will override this setting. A file which contains the `template` | **Optional** | string |
| chars | The number of characters to check for the `mustMatch` pattern | `1000` | number |
| templateVars | The variables to be used with the lodash template, always contains the variable YEAR | `{YEAR:new Date().getFullYear()}` | object |
| [onNonMatchingHeader](#onnonmatchingheader) | Action that should be taken when there is a header comment, but it does not match `mustMatch` or is less than `nonMatchingTolerance` | `"prepend"` | string |
| nonMatchingTolerance | Optional fallback for `mustMatch`. Compares a non-matching header comment (if it exists) to the resolved template using [Metric Longest Common Subsequence](http://heim.ifi.uio.no/~danielry/StringMetric.pdf). `1` means the strings must be exactly the same, where anything less is varying degrees of dissimiliar. `.70` seems like a good choice | **Optional** | number between 0 and 1 |
| varRegexps | If `mustMatch` is not set and `template` is set, a regexp that will be replaced in the `template` to create a regexp for `mustMatch` | `{YEAR:/20\d{2}/}` | object |
| messages | Allows you to change the error messages. See [messages](#messages) | **Optional** | object |

### onNonMatchingHeader

* **prepend**: Prepends the fix template, if it exists, leaving the former header comment intact.
* **replace**: Replaces the former header comment with the fix template if it exists
* **report**: Does not apply fix, simply reports it based on the level assigned to the rule ("error" or "warn")
- **prepend**: Prepends the fix template, if it exists, leaving the former header comment intact.
- **replace**: Replaces the former header comment with the fix template if it exists
- **report**: Does not apply fix, simply reports it based on the level assigned to the rule ("error" or "warn")

### messages

The `messages` option allows you to change the default error messages.
There are three messages you can change by passing in an object with the pairs you wish to change.
The `messages` option allows you to change the default error messages.
There are three messages you can change by passing in an object with the pairs you wish to change.
For example, if you want to change the default message for when a header does not match `mustMatch`:

```js
Expand All @@ -124,9 +159,8 @@ For example, if you want to change the default message for when a header does no
}
```

The three configurable messages are:

* **whenFailedToMatch**: When the header fails to match the `mustMatch` pattern.
* **reportAndSkip**: When using `"onNonMatchingHeader":"report"` and a non-matching notice is found.
* **whenOutsideTolerance**: When you using `nonMatchingTolerance` to check for notice similarity and it fails to be similar enough. Passes in `similarity` as a template variable (eg `"The similarity is {{ similarity }}"`)
The three configurable messages are:

- **whenFailedToMatch**: When the header fails to match the `mustMatch` pattern.
- **reportAndSkip**: When using `"onNonMatchingHeader":"report"` and a non-matching notice is found.
- **whenOutsideTolerance**: When you using `nonMatchingTolerance` to check for notice similarity and it fails to be similar enough. Passes in `similarity` as a template variable (eg `"The similarity is {{ similarity }}"`)
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,22 @@ const fs = require("fs"),
utils = require("./utils"),
metriclcs = require("metric-lcs");

const { regexpizeTemplate, resolveOptions, createFixer } = utils;
const { resolveOptions, createFixer } = utils;

module.exports = {
meta: {
name: "eslint-plugin-notice",
version: "1.0.0-eslint9"
},
rules: {
notice: {
meta: {
docs: {
description: "An eslint rule that checks the top of files and --fix them too!",
category: "Stylistic Issues"
},
fixable: "code"
fixable: "code",
schema: false
},
create(context) {
const {
Expand Down
Loading

0 comments on commit bab9d6b

Please sign in to comment.