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

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

Merged
merged 2 commits into from
Oct 10, 2022
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
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