22// Use of this source code is governed by a BSD-style license that can be
33// found in the LICENSE file.
44
5- 'use strict' ;
5+ import fs from 'node:fs' ;
6+ import path from 'node:path' ;
7+ import { fileURLToPath } from 'node:url' ;
68
7- const fs = require ( 'fs' ) ;
8- const path = require ( 'path' ) ;
9+ import { createRule } from './tsUtils.ts' ;
910
11+ // @ts -expect-error
12+ const filename = fileURLToPath ( import . meta. url ) ;
1013const FILE = 'front_end/ui/visual_logging/KnownContextValues.ts' ;
11- const FRONT_END_PARENT_FOLDER = path . join ( __filename , '..' , '..' , '..' , '..' ) ;
14+ const FRONT_END_PARENT_FOLDER = path . join ( filename , '..' , '..' , '..' , '..' ) ;
1215const ABSOLUTE_FILE_PATH = path . join ( FRONT_END_PARENT_FOLDER , FILE ) ;
1316const LICENSE_HEADER = `// Copyright 2024 The Chromium Authors. All rights reserved.
1417// Use of this source code is governed by a BSD-style license that can be
@@ -20,49 +23,47 @@ const formattedValues = new Set(
2023 fs . readFileSync ( ABSOLUTE_FILE_PATH , 'utf-8' ) . split ( '\n' ) . filter ( l => l . startsWith ( ' \'' ) ) ,
2124) ;
2225
23- /**
24- * @type {import('eslint').Rule.RuleModule }
25- */
26- module . exports = {
26+ export default createRule ( {
27+ name : 'jslog-context-list' ,
2728 meta : {
2829 type : 'problem' ,
2930
3031 docs : {
3132 description : 'Puts jslog context values into KnownContextValues.ts file' ,
3233 category : 'Possible Errors' ,
3334 } ,
35+ messages : {
36+ unknownJslogContextValue : 'Found jslog context value \'{{ value }}\' that is not listed in ' + FILE ,
37+ } ,
3438 fixable : 'code' ,
35- schema : [ ] , // no options
39+ schema : [ ] , // no options
3640 } ,
3741 defaultOptions : [ ] ,
38- create : function ( context ) {
42+ create : function ( context ) {
3943 const checkValue = ( value , node ) => {
4044 if ( typeof value !== 'string' ) {
4145 return ;
4246 }
4347 if ( ! value . length ) {
4448 return ;
4549 }
46- const formattedValue =
47- ' ' + JSON . stringify ( value ) . replaceAll ( '"' , '\'' ) + ',' ;
50+ const formattedValue = ' ' + JSON . stringify ( value ) . replaceAll ( '"' , '\'' ) + ',' ;
4851 if ( formattedValues . has ( formattedValue ) ) {
4952 return ;
5053 }
5154 formattedValues . add ( formattedValue ) ;
5255 if ( process . env . ESLINT_FAIL_ON_UNKNOWN_JSLOG_CONTEXT_VALUE ) {
5356 context . report ( {
5457 node,
55- message : `Found jslog context value '${ value } ' that is not listed in ${ FILE } ` ,
58+ messageId : 'unknownJslogContextValue' ,
59+ data : { value} ,
5660 } ) ;
5761 }
5862 } ;
5963
6064 const checkPropertyValue = ( propertyName , node ) => {
6165 for ( const property of node ?. properties || [ ] ) {
62- if (
63- property . key ?. name === propertyName ||
64- property . key ?. value === propertyName
65- ) {
66+ if ( property . key ?. name === propertyName || property . key ?. value === propertyName ) {
6667 checkValue ( property . value ?. value , node ) ;
6768 }
6869 }
@@ -74,18 +75,12 @@ module.exports = {
7475 return ;
7576 }
7677
77- if (
78- node . callee . type === 'MemberExpression' &&
79- node . callee . object . type === 'Identifier' &&
80- node . callee . object . name === 'VisualLogging'
81- ) {
78+ if ( node . callee . type === 'MemberExpression' && node . callee . object . type === 'Identifier' &&
79+ node . callee . object . name === 'VisualLogging' ) {
8280 if ( firstArg . type === 'Literal' ) {
8381 checkValue ( firstArg . value , node ) ;
8482 }
85- } else if (
86- node . callee . type === 'MemberExpression' &&
87- node . callee . property . type === 'Identifier'
88- ) {
83+ } else if ( node . callee . type === 'MemberExpression' && node . callee . property . type === 'Identifier' ) {
8984 const propertyName = node . callee . property . name ;
9085 if ( propertyName === 'registerActionExtension' ) {
9186 checkPropertyValue ( 'actionId' , firstArg ) ;
@@ -104,29 +99,19 @@ module.exports = {
10499 checkPropertyValue ( 'jslogContext' , node ) ;
105100 } ,
106101 VariableDeclarator ( node ) {
107- if (
108- node . id . type === 'Identifier' &&
109- node . id . name === 'generatedProperties' &&
110- node . init ?. type === 'ArrayExpression'
111- ) {
102+ if ( node . id . type === 'Identifier' && node . id . name === 'generatedProperties' &&
103+ node . init ?. type === 'ArrayExpression' ) {
112104 for ( const element of node . init . elements ) {
113105 checkPropertyValue ( 'name' , element ) ;
114106 }
115107 }
116- if (
117- node . id . type === 'Identifier' &&
118- node . id . name === 'generatedAliasesFor' &&
119- node . init ?. type === 'NewExpression'
120- ) {
108+ if ( node . id . type === 'Identifier' && node . id . name === 'generatedAliasesFor' &&
109+ node . init ?. type === 'NewExpression' ) {
121110 const firstArg = node . init ?. arguments ?. [ 0 ] ;
122- const elements =
123- firstArg . type === 'ArrayExpression' ? firstArg . elements : [ ] ;
111+ const elements = firstArg . type === 'ArrayExpression' ? firstArg . elements : [ ] ;
124112
125113 for ( const outerElement of elements ) {
126- const innerElements =
127- outerElement ?. type === 'ArrayExpression'
128- ? outerElement . elements
129- : [ ] ;
114+ const innerElements = outerElement ?. type === 'ArrayExpression' ? outerElement . elements : [ ] ;
130115 for ( const innerElement of innerElements ) {
131116 if ( innerElement && 'value' in innerElement ) {
132117 checkValue ( innerElement . value , innerElement ) ;
@@ -139,13 +124,10 @@ module.exports = {
139124 if ( process . env . ESLINT_FAIL_ON_UNKNOWN_JSLOG_CONTEXT_VALUE ) {
140125 return ;
141126 }
142- const finalContents =
143- LICENSE_HEADER +
144- 'export const knownContextValues = new Set([\n' +
145- [ ...formattedValues ] . sort ( ) . join ( '\n' ) +
146- '\n]);\n' ;
127+ const finalContents = LICENSE_HEADER + 'export const knownContextValues = new Set([\n' +
128+ [ ...formattedValues ] . sort ( ) . join ( '\n' ) + '\n]);\n' ;
147129 fs . writeFileSync ( ABSOLUTE_FILE_PATH , finalContents , 'utf-8' ) ;
148130 } ,
149131 } ;
150132 } ,
151- } ;
133+ } ) ;
0 commit comments