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

Add template-indent rule #1478

Merged
merged 49 commits into from
Oct 11, 2021
Merged

Conversation

mmkal
Copy link
Contributor

@mmkal mmkal commented Aug 11, 2021

Fixes #580

Todo:

  • Fill out markdown docs
  • Support comments

@mmkal mmkal marked this pull request as draft August 11, 2021 05:27
@fisker
Copy link
Collaborator

fisker commented Aug 11, 2021

I don't see why it couldn't go in separately.

Because your template literal can be anywhere, use something like /* HTML */ is easier than write a selector.

Imaging

foo.innerHTML = /* HTML */ `
// ...
`;
$(foo).html(/* HTML */ `
// ...
`)
div.insertAdjacentHTML(
	'beforebegin',
	  /* HTML */ `
	  // ...
	  `
);
Object.assign(div, {
	innerHTML: /* HTML */ `
	`,
});

You don't have to list all those cases in selectors, and it can work with Prettier .

It's much more complex

It's very simple, use sourceCode.getTokenBefore(..., {includeComments: true}), and check that comment.

rules/template-indent.js Outdated Show resolved Hide resolved
rules/template-indent.js Outdated Show resolved Hide resolved
@mmkal mmkal marked this pull request as ready for review August 11, 2021 19:47
@fisker fisker self-requested a review August 12, 2021 00:33
@mmkal mmkal changed the title Template indent Add template-indent rule Sep 21, 2021
@mmkal
Copy link
Contributor Author

mmkal commented Sep 21, 2021

@fisker anything else you feel should be added here?

@fisker
Copy link
Collaborator

fisker commented Sep 21, 2021

I'll take a look ASAP.

rules/template-indent.js Outdated Show resolved Hide resolved
rules/template-indent.js Outdated Show resolved Hide resolved
rules/template-indent.js Show resolved Hide resolved
test/template-indent.mjs Show resolved Hide resolved
@fisker
Copy link
Collaborator

fisker commented Sep 28, 2021

We need test for

foo = outdent`
before
before${
expression
}after
after
`;
foo = outdent`
  before
  before${
expression
}after
  after
`;
foo = outdent`
  before
  before${
	outdent`
inner
	`
}after
  after
`;
foo = outdent`
  before
  before${
	normalTemplate`
inner
	`
}after
  after
`;

@mmkal
Copy link
Contributor Author

mmkal commented Sep 28, 2021

@fisker added tests in 84ada62. Note that this rule is unopinionated about how parent variables are indented, and only cares about the template literals themselves, so

outdent`
  before
  before${
	outdent`
inner
	`
}after
  after
`;

becomes

outdent`
  before
  before${
	outdent`
		inner
	`
}after
  after
`;

etc. even though the outer template is indented with spaces and the inner with tabs. It should work nicely with other rules that do care about the indentation level of the parent, like prettier or whatever. Good call to make sure this behaviour doesn't get regressed though. 👍

@mmkal mmkal requested a review from fisker September 30, 2021 15:33
@fisker

This comment has been minimized.

rules/template-indent.js Outdated Show resolved Hide resolved
Copy link
Collaborator

@fisker fisker left a comment

Choose a reason for hiding this comment

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

LGTM

@sindresorhus sindresorhus merged commit 5f4c440 into sindresorhus:main Oct 11, 2021
@mmkal mmkal deleted the template-indent branch October 11, 2021 10:33
@mmkal mmkal restored the template-indent branch October 11, 2021 12:16
@mmkal mmkal deleted the template-indent branch July 1, 2022 15:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

indent for template literals
4 participants