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

Fix for .vue files to inject header comment in script tag. #32

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
64 changes: 54 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ When you use a regular expression `pattern`, you can also provide a `template` p
],
"rules": {
"header/header": [2, "block", [
{"pattern": " Copyright \\d{4}", "template": " Copyright 2019"},
{"pattern": " Copyright \\d{4}", "template": " Copyright 2019"},
"My Company"
]]
}
Expand All @@ -154,35 +154,56 @@ The rule works with both unix and windows line endings. For ESLint `--fix`, the
```
Possible values are `unix` for `\n` and `windows` for `\r\n` line endings.

### Vue support

`eslint-plugin-header` now supports `.vue` files. Please see installation instructions for [eslint-plugin-vue](https://eslint.vuejs.org/user-guide/) before trying to integrate with `eslint-plugin-header`. There are some caveats to be aware of:
* `<script>` tag must be defined within a `.vue` file, otherwise the linter will crash.
* `<script>` tag must be defined at the top of a `.vue` file, otherwise the linter won't realize the comment is in the file and will inject it multiple times.
* `<script>` tag must contain at least `export default {
name: 'component-name'
}` otherwise the linter won't realize the comment is in the file and will inject it multiple times.

It is suggested to use `vue/component-tags-order` rule in parallel to ensure `<script>` tags are at the top of each `.vue` file (part of `eslint-plugin-vue`, see [eslint-plugin-vue/component-tags-order](https://github.com/vuejs/eslint-plugin-vue/blob/master/docs/rules/component-tags-order.md) for more information):
```json
"vue/component-tags-order": [
2,
{
"order": ["script", "template", "style"]
}
]
```

## Examples

The following examples are all valid.
The following rules are all valid within the `.eslintrc.*` file:

`"block", "Copyright 2015, My Company"`:
---
`"header/header": [2, "block", "Copyright 2015, My Company"]`

```js
/*Copyright 2015, My Company*/
console.log(1);
```

`"line", ["Copyright 2015", "My Company"]]`:
---
`"header/header": [2, "line", ["Copyright 2015", "My Company"]]`

```js
//Copyright 2015
//My Company
console.log(1)
console.log(1);
```

`"line", [{pattern: "^Copyright \\d{4}$"}, {pattern: "^My Company$"}]]`:
---
`"header/header": [2, "line", [{pattern: "^Copyright \\d{4}$"}, {pattern: "^My Company$"}]]`

```js
//Copyright 2017
//My Company
console.log(1)
console.log(1);
```

---
### With more decoration

---
```json
"header/header": [2, "block", [
"************************",
Expand All @@ -199,6 +220,29 @@ console.log(1)
*************************/
console.log(1);
```
---
### With Vue support

---
`"header/header": [2, "block", "Copyright 2015, My Company"]`

```vue
<script>
/*Copyright 2015, My Company*/
console.log(1);
</script>
```
---
`"header/header": [2, "line", ["Copyright 2015", "My Company"]]`

```vue
<script>
//Copyright 2015
//My Company
console.log(1);
</script>
```
---

## License

Expand Down
3 changes: 3 additions & 0 deletions lib/rules/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ function getEOL(options) {
}

function hasHeader(src) {
if (src.substr(0, 8) === "<script>") {
src = src.substr(9);
}
if (src.substr(0, 2) === "#!") {
var m = src.match(/(\r\n|\r|\n)/);
if (m) {
Expand Down