File tree 2 files changed +84
-1
lines changed
2 files changed +84
-1
lines changed Original file line number Diff line number Diff line change
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
+ / \* C o p y r i g h t \d { 4 } N e w R e l i c C o r p o r a t i o n \. A l l r i g h t s r e s e r v e d \. / . test ( headerLines [ 1 ] ) === true &&
69
+ / \* S P D X - L i c e n s e - I d e n t i f i e r : A p a c h e - 2 \. 0 / . test ( headerLines [ 2 ] ) === true &&
70
+ headerLines [ 3 ] === ' */'
71
+ }
Original file line number Diff line number Diff line change
1
+ /*
2
+ * Copyright 2024 New Relic Corporation. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
1
6
'use strict'
2
7
3
8
const neostandard = require ( 'neostandard' )
4
9
const jsdoc = require ( 'eslint-plugin-jsdoc' )
5
10
const sonarjs = require ( 'eslint-plugin-sonarjs' )
11
+ const header = require ( './eslint-plugin-newrelic-header.js' )
6
12
7
13
// The new eslint configuration format is a simple array of configuration
8
14
// objects. See https://eslint.org/docs/latest/use/configure/configuration-files#configuration-objects.
@@ -20,9 +26,15 @@ const globalIgnores = {
20
26
}
21
27
22
28
const localConfig = {
29
+ plugins : {
30
+ header
31
+ } ,
32
+
23
33
rules : {
24
34
'consistent-return' : 'off' ,
25
- 'header/header' : 'off' ,
35
+
36
+ // Enable file header checking and autocorrection.
37
+ 'header/header' : 'error' ,
26
38
27
39
// This one enforces `!!thing` syntax, which some folks find difficult
28
40
// to read:
You can’t perform that action at this time.
0 commit comments