-
-
Notifications
You must be signed in to change notification settings - Fork 669
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
535 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Define a style for the attributes casing in templates. (html-attributes-casing) | ||
|
||
Define a style for the attributes casing in templates. | ||
|
||
:+1: Examples of **correct** code for `PascalCase`: | ||
|
||
```html | ||
<template> | ||
<component MyProp="prop"></component> | ||
</template> | ||
``` | ||
|
||
:+1: Examples of **correct** code for `kebab-case`: | ||
|
||
```html | ||
<template> | ||
<component my-prop="prop"></component> | ||
</template> | ||
``` | ||
|
||
:+1: Examples of **correct** code for `camelCase`: | ||
|
||
```html | ||
<template> | ||
<component myProp="prop"></component> | ||
</template> | ||
``` | ||
|
||
## :wrench: Options | ||
|
||
Default casing is set to `kebab-case` | ||
|
||
``` | ||
'vue/html-attributes-casing': [2, 'camelCase'|'kebab-case'|'PascalCase'] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* @fileoverview Define a style for the props casing in templates. | ||
* @author Armano | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
const casing = require('../utils/casing') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
function create (context) { | ||
const sourceCode = context.getSourceCode() | ||
const options = context.options[0] | ||
const caseType = casing.allowedCaseOptions.indexOf(options) !== -1 ? options : 'kebab-case' | ||
|
||
function reportIssue (node, name, newName) { | ||
context.report({ | ||
node: node.key, | ||
loc: node.loc, | ||
message: "Attribute '{{name}}' is not {{caseType}}.", | ||
data: { | ||
name, | ||
caseType, | ||
newName | ||
}, | ||
fix: fixer => fixer.replaceText(node.key, newName) | ||
}) | ||
} | ||
|
||
// ---------------------------------------------------------------------- | ||
// Public | ||
// ---------------------------------------------------------------------- | ||
|
||
utils.registerTemplateBodyVisitor(context, { | ||
'VStartTag' (obj) { | ||
if (!utils.isSvgElementName(obj.id.name) && !utils.isMathMLElementName(obj.id.name)) { | ||
obj.attributes.forEach((node) => { | ||
if (!node.directive) { | ||
const oldValue = node.key.name | ||
if (oldValue.indexOf('data-') !== -1) { | ||
return | ||
} | ||
const value = casing.getConverter(caseType)(oldValue) | ||
if (value !== oldValue) { | ||
reportIssue(node, oldValue, value) | ||
} | ||
} else if (node.key.name === 'bind') { | ||
const oldValue = node.key.argument | ||
if (oldValue.indexOf('data-') !== -1) { | ||
return | ||
} | ||
const text = sourceCode.getText(node.key) | ||
const value = casing.getConverter(caseType)(oldValue) | ||
if (value !== oldValue) { | ||
reportIssue(node, text, text.replace(oldValue, value)) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
}) | ||
|
||
return {} | ||
} | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Define a style for the props casing in templates.', | ||
category: 'Stylistic Issues', | ||
recommended: false | ||
}, | ||
fixable: 'code', | ||
schema: [ | ||
{ | ||
enum: casing.allowedCaseOptions | ||
} | ||
] | ||
}, | ||
|
||
create | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.