2
2
// Use of this source code is governed by a BSD-style license that can be
3
3
// found in the LICENSE file.
4
4
5
- /**
6
- * @fileoverview Rule to check license headers
7
- * @author Tim van der Lippe
8
- */
9
- 'use strict' ;
5
+ import type { TSESTree } from '@typescript-eslint/utils' ;
6
+ import * as path from 'path' ;
10
7
11
- const path = require ( 'path' ) ;
8
+ import { createRule } from './tsUtils.ts' ;
12
9
13
10
const FRONT_END_FOLDER = path . join (
14
- __filename ,
11
+ // If we want to fix this we need to change to .mts or use a package.json
12
+ // with type : module
13
+ // @ts -expect-error we don't build the file to CJS
14
+ import . meta. filename ,
15
15
'..' ,
16
16
'..' ,
17
17
'..' ,
@@ -111,14 +111,10 @@ const OTHER_LICENSE_HEADERS = [
111
111
'ui/Widget.js' ,
112
112
] ;
113
113
114
- // ------------------------------------------------------------------------------
115
- // Rule Definition
116
- // ------------------------------------------------------------------------------
117
-
118
114
/**
119
- * Check each linecomment that should (combined) result in the LINE_LICENSE_HEADER.
115
+ * Check each line comment that should (combined) result in the LINE_LICENSE_HEADER.
120
116
*/
121
- function isMissingLineCommentLicense ( comments ) {
117
+ function isMissingLineCommentLicense ( comments : TSESTree . Comment [ ] ) {
122
118
for ( let i = 0 ; i < LINE_REGEXES . length ; i ++ ) {
123
119
if ( ! comments [ i ] || ! LINE_REGEXES [ i ] . test ( comments [ i ] . value ) ) {
124
120
return true ;
@@ -131,39 +127,36 @@ function isMissingLineCommentLicense(comments) {
131
127
/**
132
128
* We match the whole block comment, including potential leading asterisks of the jsdoc.
133
129
*/
134
- function isMissingBlockLineCommentLicense ( licenseText ) {
130
+ function isMissingBlockLineCommentLicense ( licenseText : string ) {
135
131
return ! BLOCK_REGEX . test ( licenseText ) ;
136
132
}
137
133
138
- /**
139
- * @type {import('eslint').Rule.RuleModule }
140
- */
141
- module . exports = {
134
+ export default createRule ( {
135
+ name : 'check-license-header' ,
142
136
meta : {
143
137
type : 'problem' ,
144
-
145
138
docs : {
146
139
description : 'check license headers' ,
147
140
category : 'Possible Errors' ,
148
141
} ,
149
142
fixable : 'code' ,
150
- schema : [ ] , // no options
143
+ schema : [ ] , // no options
144
+ messages : {
145
+ missingLicense : 'Missing license header' ,
146
+ incorrectLineLicense : 'Incorrect line license header' ,
147
+ incorrectBlockLicense : 'Incorrect block license header' ,
148
+ } ,
151
149
} ,
152
- create : function ( context ) {
150
+ defaultOptions : [ ] ,
151
+ create : function ( context ) {
153
152
const sourceCode = context . sourceCode ?? context . getSourceCode ( ) ;
154
153
const filename = context . filename ?? context . getFilename ( ) ;
155
154
const fileName = filename ;
156
155
// Fix windows paths for exemptions
157
- const relativePath = path
158
- . relative ( FRONT_END_FOLDER , fileName )
159
- . replace ( / \\ / g, '/' ) ;
160
-
161
- if (
162
- relativePath . startsWith ( 'third_party' ) ||
163
- fileName . endsWith ( 'TestRunner.js' ) ||
164
- EXCLUDED_FILES . includes ( relativePath ) ||
165
- OTHER_LICENSE_HEADERS . includes ( relativePath )
166
- ) {
156
+ const relativePath = path . relative ( FRONT_END_FOLDER , fileName ) . replace ( / \\ / g, '/' ) ;
157
+
158
+ if ( relativePath . startsWith ( 'third_party' ) || fileName . endsWith ( 'TestRunner.js' ) ||
159
+ EXCLUDED_FILES . includes ( relativePath ) || OTHER_LICENSE_HEADERS . includes ( relativePath ) ) {
167
160
return { } ;
168
161
}
169
162
@@ -180,7 +173,7 @@ module.exports = {
180
173
( comments . length === 1 && comments [ 0 ] . type === 'Shebang' ) ) {
181
174
context . report ( {
182
175
node,
183
- message : 'Missing license header ' ,
176
+ messageId : 'missingLicense ' ,
184
177
fix ( fixer ) {
185
178
return fixer . insertTextBefore ( node , LICENSE_HEADER_ADDITION ) ;
186
179
} ,
@@ -202,30 +195,28 @@ module.exports = {
202
195
if ( isMissingLineCommentLicense ( commentsToCheck ) ) {
203
196
context . report ( {
204
197
node,
205
- message : 'Incorrect line license header ' ,
198
+ messageId : 'incorrectLineLicense ' ,
206
199
fix ( fixer ) {
207
200
return fixer . insertTextBefore (
208
- firstCommentToCheck ,
209
- LICENSE_HEADER_ADDITION ,
201
+ firstCommentToCheck ,
202
+ LICENSE_HEADER_ADDITION ,
210
203
) ;
211
204
} ,
212
205
} ) ;
213
206
}
214
- } else if (
215
- isMissingBlockLineCommentLicense ( firstCommentToCheck . value )
216
- ) {
207
+ } else if ( isMissingBlockLineCommentLicense ( firstCommentToCheck . value ) ) {
217
208
context . report ( {
218
209
node,
219
- message : 'Incorrect block license header ' ,
210
+ messageId : 'incorrectBlockLicense ' ,
220
211
fix ( fixer ) {
221
212
return fixer . insertTextBefore (
222
- firstCommentToCheck ,
223
- LICENSE_HEADER_ADDITION ,
213
+ firstCommentToCheck ,
214
+ LICENSE_HEADER_ADDITION ,
224
215
) ;
225
216
} ,
226
217
} ) ;
227
218
}
228
219
} ,
229
220
} ;
230
221
} ,
231
- } ;
222
+ } ) ;
0 commit comments