Skip to content

Commit e7d8868

Browse files
committed
fix header plugin
1 parent af3771d commit e7d8868

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

eslint-plugin-newrelic-header.js

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2024 New Relic Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
'use strict'
7+
8+
const headerTmpl = `
9+
/*
10+
* Copyright {{year}} New Relic Corporation. All rights reserved.
11+
* SPDX-License-Identifier: Apache-2.0
12+
*/
13+
`.trim()
14+
15+
const rule = {
16+
meta: {
17+
type: 'layout',
18+
fixable: 'whitespace',
19+
schema: false
20+
},
21+
22+
create(context) {
23+
return {
24+
Program(node) {
25+
const src = context.sourceCode.getText()
26+
if (hasHeader(src) === true) {
27+
return
28+
}
29+
context.report({
30+
loc: node.loc,
31+
message: 'missing or invalid header',
32+
fix(fixer) {
33+
const rendered = headerTmpl.replace('{{year}}', new Date().getFullYear() + '') + '\n\n'
34+
if (hasShebang(src) === true) {
35+
return fixer.insertTextAfterRange([0, src.indexOf('\n')], '\n' + rendered)
36+
}
37+
return fixer.insertTextBefore(
38+
node,
39+
rendered
40+
)
41+
}
42+
})
43+
}
44+
}
45+
}
46+
}
47+
48+
module.exports = {
49+
meta: {
50+
name: 'eslint-plugin-newrelic-header',
51+
version: '1.0.0'
52+
},
53+
rules: {
54+
header: rule
55+
}
56+
}
57+
58+
function hasShebang(src) {
59+
return /^#!\s?\//.test(src)
60+
}
61+
62+
function hasHeader(src) {
63+
const headerLines = src.split('\n').slice(0, 5)
64+
if (hasShebang(src) === true) {
65+
headerLines.shift()
66+
}
67+
return headerLines[0] === '/*' &&
68+
/ \* Copyright \d{4} New Relic Corporation\. All rights reserved\./.test(headerLines[1]) === true &&
69+
/ \* SPDX-License-Identifier: Apache-2\.0/.test(headerLines[2]) === true &&
70+
headerLines[3] === ' */'
71+
}

eslint.config.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
/*
2+
* Copyright 2024 New Relic Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
16
'use strict'
27

38
const neostandard = require('neostandard')
49
const jsdoc = require('eslint-plugin-jsdoc')
510
const sonarjs = require('eslint-plugin-sonarjs')
11+
const header = require('./eslint-plugin-newrelic-header.js')
612

713
// The new eslint configuration format is a simple array of configuration
814
// objects. See https://eslint.org/docs/latest/use/configure/configuration-files#configuration-objects.
@@ -20,9 +26,15 @@ const globalIgnores = {
2026
}
2127

2228
const localConfig = {
29+
plugins: {
30+
header
31+
},
32+
2333
rules: {
2434
'consistent-return': 'off',
25-
'header/header': 'off',
35+
36+
// Enable file header checking and autocorrection.
37+
'header/header': 'error',
2638

2739
// This one enforces `!!thing` syntax, which some folks find difficult
2840
// to read:

0 commit comments

Comments
 (0)