Coder Social home page Coder Social logo

eslint-plugin-notice's Introduction

Build Status

eslint-plugin-notice

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

Usage

npm i eslint-plugin-notice

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

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

Add a template to --fix it

{
    "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

/**
 * Copyright (c) <%= YEAR %>, Nick Deis
 */
{
    "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

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

Want a more expressive template? Add templateVars and varRegexps config/copyright.js

/**
 * Copyright (c) <%= YEAR %>, <%= NAME %>
 */
{
    "notice/notice":["error",
        {
        templateFile:"config/copyright.js",
        //YEAR will still be added unless you add your own value
        templateVars:{NAME:"Nick Deis"},
        //The regexp for YEAR is /20\d{2}/ and is automatically added
        varRegexps:{NAME:/(Nick|Nicholas) Deis/}
        }
    ]
}

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 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. 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 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")

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. For example, if you want to change the default message for when a header does not match mustMatch:

{
    "notice/notice":["error",
        {
            "mustMatch":"Apache License",
            "templateFile":"config/apache.js",
            "messages":{
                "whenFailedToMatch":"Couldn't find 'Apache License', are you sure you added it?"
            }
        }
    ]
}

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 }}")

eslint-plugin-notice's People

Contributors

grahamdyson avatar ivansanchez avatar nickdeis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

eslint-plugin-notice's Issues

Either template or templateFile must be set

When I use the first example from the readme:

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

I get an error:

Either template or templateFile must be set

no such file or directory

"notice/notice": [
      "error",
      {
        "mustMatch":
          "[0-9]{0,4} All rights reserved.",
        "templateFile": "config/copyright.js",
        "nonMatchingTolerance": ".70"
      }
    ]

Error while loading rule 'notice/notice'/mnt/SSD/Projects/.../src/modules/searchModule.js:: ENOENT/mnt/SSD/Projects/.../src/modules/searchModule.js:: no such file or directory, open 'config/copyright.js'
And yes - the file is under the root folder in config directory

Problem with matching notice headers because of line endings mismatch on Windows

Hi @nickdeis ! Thank you for creating the tool, it's very useful! โค๏ธ

Friends at Spotify are using this plugin to lint for notice headers in their Open Source projects. See spotify/eslint-config-oss. There is a notice template file in the repository containing the template for Spotify copyright notice.

When working on spotify/backstage, users noticed that the project would not build on Windows OS and the problem is "missing notice header". Note that there are no errors on the unix platform. All the project source files do have the notice in place, and we should not be seeing this error.
Another key fact is that our build broke when we moved the notice header to a separate text file.

One of the causes we think is that the line endings are different in the backstage source code files and the notice-template.txt. And thus, the matching logic in eslint-plugin-notice is having issues, even though the notice text is exactly the same.

I am referring this comment from @Rugvip on backstage/backstage#613. The issue has more details!

A possible solution is to improve the logic in this plugin to ignore line endings when matching the notice headers. Or it could allow to suppress errors raised due to line endings.

Let us know your thoughts! ๐Ÿ™‡

cc @fastfrwrd

Gulp ends automatically

Cloned repos in the same way as suggested in the README. However, the app doesn't fire up and gulp ends in the following way :

> [15:41:07] Finished 'lint' after 5 s
> [15:41:07] Starting 'watch'...
> [15:41:07] Starting 'watchify'...
> [15:41:15] 11409263 bytes written (8.35 seconds)
> [15:41:16] main.js.map 5.18 MB
> [15:41:16] main.js 4.5 MB
> [15:41:16] all files 9.68 MB
> [15:41:16] main.js.map 996 kB (gzipped)
> [15:41:17] main.js 911 kB (gzipped)
> [15:41:17] all files 1.91 MB (gzipped)
> [15:41:17] Finished 'watchify' after 10 s
> [15:41:17] Starting '<anonymous>'...
> [15:41:17] Finished '<anonymous>' after 12 ms
> [15:41:17] Finished 'watch' after 10 s
> [15:41:17] Starting 'serve'...
> [15:41:17] Webserver started at https://0.0.0.0:3000
> [15:41:17] Finished 'serve' after 204 ms
> [15:41:17] Finished 'default' after 15 s

Template variable for file created date vs modified date?

Our copyright notices need to be different based on when the file was originally created vs. modified. Examples:

/* Copyright (c) 2019, 2024; All Rights Reserved. */
/* Copyright (c) 2019 - 2024; All Rights Reserved. */
/* Copyright (c) 2024; All Rights Reserved. */

I see there's a template variable that exists for YEAR but that's the current date. Is there a way to obtain the original create date of the file? Is there a way to do conditionals if the created date doesn't match YEAR? Or, even more accurately, something like the earliest year from the git file commit.

Check for an existing notice that is close, but incorrect

I've run into a case where some files had a notice, but with incorrect capitalisation, missing punctuation, or typo. This leads to duplicate notices when using --fix

It would be great to have a "closeness" metric/threshold that would allow for fixing notices with typos or other problems.

Example:

/* Copyright (c) 2014-present, Foo bar Inc. */

vs

/* Copyright (c) 2014-present, FooBar, Inc. */

For some users there would be some edge cases like not wanting to change the initial year.

Make error text more helpful?

Would be better if it were configurable, or if it included a suggested fix:

  • Could not find a match for the mustMatch pattern. Are you missing the header text? notice/notice
  • Alternatively, make it configurable
notice/notice:
  - error
  - errorMessage: 'Missing license header'
  - templateFile: config/copyright.js

Insert notice before any existing header comments?

It seems that when using --fix this will insert the header below already existing block level comments.

For example before fixing:

/**
 * This file contains important variables used throughout the project
 */

Then after fixing it would look like this:

/**
 * This file contains important variables used throughout the project
 */

/**
 * Copyright (c) 2017 Acme Corp.
 */

whereas I would expect it to look like this:

/**
 * Copyright (c) 2017 Acme Corp.
 */

/**
 * This file contains important variables used throughout the project
 */

Use `templateFile` only

I think it would make sense to use only the templateFile parameter and supply for each argument such as YEAR a matching regex pattern.

Support for blank line after block comment when fixing a file

The problem is that files without extra line space fix incorrectly and duplicate the licence comment:

The original file:

/**
 * Copyright 2017 Moshe Simantov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
console.error("Don't run `mocha` directly. Use `make test`.");
process.exit(0);

The fix:

/**
 * Copyright 2017 Moshe Simantov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Copyright 2017 Moshe Simantov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
console.error("Don't run `mocha` directly. Use `make test`.");
process.exit(0);

Please see issue #2 for my configuration.

My template file is as follow (notice the blank line on the end):

/**
 * Copyright <%= YEAR %> Moshe Simantov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.