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

New: vue/html-indent rule (fixes #46) #145

Merged
merged 5 commits into from
Oct 15, 2017
Merged

New: vue/html-indent rule (fixes #46) #145

merged 5 commits into from
Oct 15, 2017

Conversation

mysticatea
Copy link
Member

@mysticatea mysticatea commented Aug 10, 2017

Fixes #46.

This PR adds vue/html-indent rule.

The strategy of the vue/html-indent rule is the same as core indent rule. I.e., it makes the base token and offset for every token in the first traversing, then at leaving the root element, it calculate and validate expected indentation for each line.

Currently, about inside of directives and mustaches, this vue/html-indent rule supports only the syntax ECMAScript 2017 standard includes. It just ignores other syntaxes. The indentation rule is one of most complex rules. I think this is a good start point.

@mysticatea mysticatea changed the title [WIP] New: vue/html-indent rule (fixes #46) New: vue/html-indent rule (fixes #46) Sep 7, 2017
@mysticatea
Copy link
Member Author

Thank you for your patience. I think I have done. Please review and suggest lacking tests!

@@ -1,3 +1,4 @@
{
"editor.tabSize": 2
"editor.tabSize": 2,
Copy link
Contributor

Choose a reason for hiding this comment

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

seems not related to the vue/html-indent. I think it can be in another PR?

package.json Outdated
@@ -10,7 +10,8 @@
"lint": "eslint .",
"pretest": "npm run lint",
"preversion": "npm run update && npm test",
"update": "node ./tools/update-rules.js"
"update": "node ./tools/update-rules.js",
"watch": "warun lib/rules/html-indent.js tests/lib/rules/html-indent.js -- nyc --reporter lcov -- mocha tests/lib/rules/html-indent.js"
Copy link
Contributor

Choose a reason for hiding this comment

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

what is the warun? I didn't see it in deps.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oops, it was added unintentionally. I was using it to test.
Thank you.

* @returns {{firstToken:Token,lastToken:Token}} The gotten tokens.
*/
function getFirstAndLastTokens (node, borderOffset) {
borderOffset |= 0
Copy link
Contributor

Choose a reason for hiding this comment

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

could you explain it a little? :)

Copy link
Collaborator

Choose a reason for hiding this comment

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

its js magic assign 0 if value is non numeric

Copy link
Contributor

Choose a reason for hiding this comment

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

hah.. got it! amazing! lol~

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, it is "casting to signed integer".

Bitwise operators (&, |, "^", ~, <<, >>, and >>>) casts both operands to 32bits signed integer, then the logical OR with 0 does nothing. As the result, the x | 0 expression casts x to 32bits signed integer.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@mysticatea like i said magic

return
}

// Debug log
Copy link
Contributor

Choose a reason for hiding this comment

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

this debug log seems no longer required. 😄

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah!
But I'd like to leave it to investigate for bug reports, since the debug code is long...

<div
aaa
bbb
`,
Copy link
Contributor

Choose a reason for hiding this comment

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

not a valid template, is is intentional?

and why the parser not reporting an error?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, it's intentional. The HTML spec includes recovering process for invalid syntax, so vue-eslint-parser reports syntax errors with the programNode.templateBody.errors property instead of throwing the error. Then vue/no-parsing-error rule would report those syntax errors.

The purpose of this test case is to check whether vue/html-indent rule works properly on an incomplete code. Especially, the expected indentation of closing brackets (>) does not apply to the last attribute even if the closing bracket is lacking.

"vue/html-indent": ["error", type, {
"attribute": 1,
"closeBracket": 0,
"switchCase": 0,
Copy link
Contributor

Choose a reason for hiding this comment

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

could you please add an example for the switchCase option?

Copy link
Member Author

Choose a reason for hiding this comment

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

Or maybe I should remove the option from this rule since switch is a prohibited keyword.

bar();
"
>
{{
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe we need some more tests for {{}}.

and I didn't see tests for comma, e.g:

<template>
{{
  foo,
  bar,
  qux
}}
</template>

Copy link
Member Author

Choose a reason for hiding this comment

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

In technically, mustaches ({{foo}}) and directive values ("foo" of directives) are completely same for indentation. Both are VExpressionContainer. So I don't think we need many tests for mustaches. I added some tests.

@Snugug Snugug mentioned this pull request Oct 2, 2017
30 tasks
Copy link
Member

@michalsnik michalsnik left a comment

Choose a reason for hiding this comment

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

Wow, impressive work @mysticatea ! 🚀 I wasn't expecting this rule to be that complex, now I see how wrong I was.. I think the best way to properly check this rule is to merge it and beta test on real projects as we still have some time before official release. Feel free to merge it :)


// Collect related tokens.
// Commas between this and the previous, and the first token of this node.
if (lastToken != null) {
Copy link
Member

Choose a reason for hiding this comment

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

Do you actually need to check for != null everywhere? From what I can tell it would make sense if the value could be also 0 and it would've been expected. But since those tokens are either Objects or null, simple if(lastToken) would be enough, wouldn't it?

Copy link
Member Author

Choose a reason for hiding this comment

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

I apology that I'm inactive during a long time.

It, ensuring boolean type for conditional expression, is just my favorite style 😄

@michalsnik michalsnik added this to the v4.0.0 - Official release milestone Oct 9, 2017
@michalsnik michalsnik merged commit 5fdb848 into master Oct 15, 2017
@michalsnik michalsnik deleted the issue46 branch October 15, 2017 21:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants