Skip to content

feature request: enforce comments after last import #3226

@TrigamDev

Description

@TrigamDev

At least personally, I often use a comment separator between imports and the rest of the file, especially when the file has a large number of imports. Having a rule to enforce this would be very helpful.

This could potentially conflict with newline-after-import, though. I can think of two ways to resolve this.

Solution 1

The separator comment must be placed either before or after the new lines enforced by newline-after-import . Whether the comment is placed before or after could potentially be configured with an option in the rule, or one could be chosen to be hardcoded with it not being configurable. This is a little clunky, however.

Example

Options

{
	"import/comment-after-import": [
		"warn",

		/* Whether to style as line comments,
		or a block comment. */
		// "line" | "block"
		"block",

		/* The content of the comment.
		Can be one string using \n, or an array of
		strings, one element representing a line. */
		// string | string[]
		"---------------",

		/* Whether the comment should be placed
		before or after the new lines enforced by
		newline-after-import. */
		// "before" | "after"
		"before"
	],

	"import/newline-after-import": [
		"warn",
		{
			count: 2
		}
	]
}

❌ Invalid

import foo from "bar"
import type { baz } from "quux"


console.log("Hello world!")

✅ Valid

import foo from "bar"
import type { baz } from "quux"
/* --------------- */


console.log("Hello world!")

Solution 2

Instead of adding a new comment-after-import rule, newline-after-import could be modified to enforce lines with specific content, not just empty lines. The rule would also have to be renamed to reflect this change.

Example

Options

{
	"import/lines-after-import": [
		"warn",
		{
			/* The lines that should be present after
			the last import.
			Can be one string using \n, or an array of
			strings, one element representing a line. */
			// string | string[]
			"lines": [
				"",
				"",
				"/* --------------- */",
				"",
				""
			]
		}
	]
}

❌ Invalid

import foo from "bar"
import type { baz } from "quux"


console.log("Hello world!")

✅ Valid

import foo from "bar"
import type { baz } from "quux"


/* --------------- */


console.log("Hello world!")

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions