Skip to content

Commit

Permalink
[stylelint-plugin] feat: Improve copyright comment detection (#5655)
Browse files Browse the repository at this point in the history
  • Loading branch information
kmark authored Oct 10, 2022
1 parent 0dd1c3a commit 3742f57
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
30 changes: 24 additions & 6 deletions packages/stylelint-plugin/src/utils/insertImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,34 @@ function getLastImport(cssSyntaxType: CssSyntax.SASS | CssSyntax.LESS, root: Roo
}

/**
* Returns the first copyright header in the file, or undefined if one does not exist
* Returns the first copyright header in the file, or undefined if one does not exist.
* If the first copyright header spans multiple lines, the last line is returned.
*/
function getCopyrightHeader(root: Root): Comment | undefined {
let copyrightComment: Comment | undefined;
let lastCopyrightComment: Comment | undefined;
root.walkComments(comment => {
if (comment.text.toLowerCase().includes("copyright")) {
copyrightComment = comment;
return false; // Stop the iteration
if (lastCopyrightComment) {
if (comment.source?.start === undefined || lastCopyrightComment.source?.end === undefined) {
return false;
}
if (comment.source.start.line === lastCopyrightComment.source.end.line + 1) {
// Copyright continues in next comment via //
lastCopyrightComment = comment;
} else {
// The next comment is not directly under the prior comment
return false;
}
} else if (comment.text.toLowerCase().includes("copyright")) {
lastCopyrightComment = comment;
if (comment.source?.start === undefined || comment.source?.end === undefined) {
return false;
}
if (comment.source.start.line !== comment.source.end.line) {
// A multi-line copyright comment such as /* */
return false; // Stop the iteration
}
}
return;
});
return copyrightComment;
return lastCopyrightComment;
}
18 changes: 18 additions & 0 deletions packages/stylelint-plugin/test/insertImport.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,24 @@ describe("insertImport", () => {
@import "some_path";
.some-class { width: 10px }`);
});

it("Inserts an import below a multi-line copyright header if no other imports exist", () => {
const root = postcss.parse(`
/* copyright 2021
* and some additional licensing info here
*/
.some-class { width: 10px }`);
insertImport(CssSyntax.SASS, root, { newline: "\n" }, "some_path");
expect(root.toString()).to.be.eq(`
/* copyright 2021
* and some additional licensing info here
*/
@use "some_path";
.some-class { width: 10px }`);
});

Expand Down

1 comment on commit 3742f57

@blueprint-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[stylelint-plugin] feat: Improve copyright comment detection (#5655)

Previews: documentation | landing | table | demo

Please sign in to comment.