nikku / eslint-plugin-license-header

Rules to validate the presence of license headers in source files.
MIT License
26 stars 9 forks source link

Incorrect behavior when using // #13

Open stramel opened 2 years ago

stramel commented 2 years ago

Describe the Bug

When using this header,

// Copyright Foobar. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

the fix results in incorrect behavior

Steps to Reproduce

  1. Setup eslint plugin rule to error
  2. Set license-header.js file to contain the content above.
  3. Run eslint with --fix flag

Actual Behavior

First run,

// Copyright Foobar. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: Apache-2.0

second consecutive run,

// Copyright Foobar. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: Apache-2.0

Expected Behavior

I would expect the license header to be properly applied.

Environment

nikku commented 2 years ago

Happy to take a PR to support multiple headers

// A
// B 
// C

Until we have that consider using a single docblock header:

/**
 * A
 * B
 */
suterma commented 5 months ago

I can confirm this issue, ran into it today. May I suggest to document the (sole?) supported header type in the readme, until a respective PR is taken?

nikku commented 5 months ago

@suterma Happy to take a PR documenting the limitation in a lightweight manner. Preferred of course is a PR that adds support for // license headers.

dblock commented 3 months ago

I took a quick look, turns out it's a bit tricky. When you say /* you get a single block, but with // you get multiple lines.

[
    [
        {
            "type": "Block",
            "value": "*\n * Copyright (c) Foo Corp.\n *\n * This source code is licensed under the WTFPL license found in the\n * LICENSE file in the root of this projects source tree.\n ",
            "range": [
                0,
                164
            ],
            "loc": {
                "start": {
                    "line": 1,
                    "column": 0
                },
                "end": {
                    "line": 6,
                    "column": 3
                }
            }
        },
        {
            "type": "Block",
            "value": "* do this ",
            "range": [
                166,
                180
            ],
            "loc": {
                "start": {
                    "line": 8,
                    "column": 0
                },
                "end": {
                    "line": 8,
                    "column": 14
                }
            }
        }
    ]
]
[
    [
        {
            "type": "Line",
            "value": "",
            "range": [
                0,
                2
            ],
            "loc": {
                "start": {
                    "line": 1,
                    "column": 0
                },
                "end": {
                    "line": 1,
                    "column": 2
                }
            }
        },
        {
            "type": "Line",
            "value": " Copyright (c) Foo Corp.",
            "range": [
                3,
                29
            ],
            "loc": {
                "start": {
                    "line": 2,
                    "column": 0
                },
                "end": {
                    "line": 2,
                    "column": 26
                }
            }
        },
        {
            "type": "Line",
            "value": "",
            "range": [
                30,
                32
            ],
            "loc": {
                "start": {
                    "line": 3,
                    "column": 0
                },
                "end": {
                    "line": 3,
                    "column": 2
                }
            }
        },
        {
            "type": "Line",
            "value": " This source code is licensed under the WTFPL license found in the",
            "range": [
                33,
                101
            ],
            "loc": {
                "start": {
                    "line": 4,
                    "column": 0
                },
                "end": {
                    "line": 4,
                    "column": 68
                }
            }
        },
        {
            "type": "Line",
            "value": " LICENSE file in the root of this projects source tree.",
            "range": [
                102,
                159
            ],
            "loc": {
                "start": {
                    "line": 5,
                    "column": 0
                },
                "end": {
                    "line": 5,
                    "column": 57
                }
            }
        },
        {
            "type": "Line",
            "value": "",
            "range": [
                160,
                162
            ],
            "loc": {
                "start": {
                    "line": 6,
                    "column": 0
                },
                "end": {
                    "line": 6,
                    "column": 2
                }
            }
        },
        {
            "type": "Block",
            "value": "* do this ",
            "range": [
                167,
                181
            ],
            "loc": {
                "start": {
                    "line": 9,
                    "column": 0
                },
                "end": {
                    "line": 9,
                    "column": 14
                }
            }
        }
    ]
]

I attempted to merge these, but couldn't get it to work, maybe that's not the right strategy? Here's a test if someone wants to beat me to iterating on it.