|  | 
|  | 1 | +// Copyright 2021 The Chromium Authors. All rights reserved. | 
|  | 2 | +// Use of this source code is governed by a BSD-style license that can be | 
|  | 3 | +// found in the LICENSE file. | 
|  | 4 | + | 
|  | 5 | +import type {TSESTree} from '@typescript-eslint/utils'; | 
|  | 6 | +import * as path from 'path'; | 
|  | 7 | + | 
|  | 8 | +import {isModuleScope} from './l10n-helper.ts'; | 
|  | 9 | +import {createRule} from './tsUtils.ts'; | 
|  | 10 | + | 
|  | 11 | +type CallExpression = TSESTree.CallExpression; | 
|  | 12 | +// True iff the callExpression is `i18n.i18n.registerUIStrings`. | 
|  | 13 | +function isRegisterUIStringsCall(callExpression: CallExpression): boolean { | 
|  | 14 | +  if (callExpression.callee?.type !== 'MemberExpression') { | 
|  | 15 | +    return false; | 
|  | 16 | +  } | 
|  | 17 | +  const callee = callExpression.callee; | 
|  | 18 | + | 
|  | 19 | +  if (callee.property?.type !== 'Identifier' || callee.property?.name !== 'registerUIStrings') { | 
|  | 20 | +    return false; | 
|  | 21 | +  } | 
|  | 22 | + | 
|  | 23 | +  if (callee.object?.type !== 'MemberExpression') { | 
|  | 24 | +    return false; | 
|  | 25 | +  } | 
|  | 26 | +  const calleeObject = callee.object; | 
|  | 27 | + | 
|  | 28 | +  if (calleeObject.property?.type !== 'Identifier' || calleeObject.property?.name !== 'i18n') { | 
|  | 29 | +    return false; | 
|  | 30 | +  } | 
|  | 31 | + | 
|  | 32 | +  if (calleeObject.object?.type !== 'Identifier' || calleeObject.object?.name !== 'i18n') { | 
|  | 33 | +    return false; | 
|  | 34 | +  } | 
|  | 35 | +  return true; | 
|  | 36 | +} | 
|  | 37 | + | 
|  | 38 | +type Options = [ | 
|  | 39 | +  { | 
|  | 40 | +    rootFrontendDirectory?: string, | 
|  | 41 | +  }, | 
|  | 42 | +]; | 
|  | 43 | + | 
|  | 44 | +type MessageIds = 'pathMismatch'; | 
|  | 45 | + | 
|  | 46 | +export default createRule<Options, MessageIds>({ | 
|  | 47 | +  name: 'l10n-filename-matches', | 
|  | 48 | +  meta: { | 
|  | 49 | +    type: 'problem', | 
|  | 50 | +    docs: { | 
|  | 51 | +      description: 'i18n.i18n.registerUIStrings must receive the current file\'s path as the first argument', | 
|  | 52 | +      category: 'Possible Errors', | 
|  | 53 | +    }, | 
|  | 54 | +    fixable: 'code', | 
|  | 55 | +    schema: [ | 
|  | 56 | +      { | 
|  | 57 | +        type: 'object', | 
|  | 58 | +        properties: { | 
|  | 59 | +          rootFrontendDirectory: { | 
|  | 60 | +            type: 'string', | 
|  | 61 | +          }, | 
|  | 62 | +        }, | 
|  | 63 | +        additionalProperties: false, | 
|  | 64 | +      }, | 
|  | 65 | +    ], | 
|  | 66 | +    messages: { | 
|  | 67 | +      // Using a generic message ID as the message itself is dynamic | 
|  | 68 | +      pathMismatch: | 
|  | 69 | +          'First argument to \'registerUIStrings\' call must be \'{{expectedPath}}\' or the ModuleUIStrings.(js|ts)', | 
|  | 70 | +    }, | 
|  | 71 | +  }, | 
|  | 72 | +  defaultOptions: [{}], | 
|  | 73 | +  create: function(context) { | 
|  | 74 | +    const filename = context.filename; | 
|  | 75 | + | 
|  | 76 | +    let frontEndDirectory = ''; | 
|  | 77 | +    if (context.options?.[0]?.rootFrontendDirectory) { | 
|  | 78 | +      frontEndDirectory = context.options[0].rootFrontendDirectory; | 
|  | 79 | +    } | 
|  | 80 | +    if (!frontEndDirectory) { | 
|  | 81 | +      throw new Error( | 
|  | 82 | +          '\'rootFrontendDirectory\' option must be provided for the l10n-filename-matches rule.', | 
|  | 83 | +      ); | 
|  | 84 | +    } | 
|  | 85 | +    return { | 
|  | 86 | +      CallExpression(node) { | 
|  | 87 | +        if (!isModuleScope(context, node) || !isRegisterUIStringsCall(node)) { | 
|  | 88 | +          return; | 
|  | 89 | +        } | 
|  | 90 | + | 
|  | 91 | +        // Do nothing if there are no arguments or the first argument is not a string literal we | 
|  | 92 | +        // can check. | 
|  | 93 | +        const firstArgument = node.arguments[0]; | 
|  | 94 | +        if (node.arguments.length === 0 || !firstArgument || firstArgument.type !== 'Literal' || | 
|  | 95 | +            typeof firstArgument.value !== 'string') { | 
|  | 96 | +          return; | 
|  | 97 | +        } | 
|  | 98 | + | 
|  | 99 | +        const currentSourceFile = path.resolve(filename); | 
|  | 100 | +        const currentFileRelativeToFrontEnd = path.relative( | 
|  | 101 | +            frontEndDirectory, | 
|  | 102 | +            currentSourceFile, | 
|  | 103 | +        ); | 
|  | 104 | + | 
|  | 105 | +        const currentModuleDirectory = path.dirname(currentSourceFile); | 
|  | 106 | +        const allowedPathArguments = [ | 
|  | 107 | +          currentSourceFile, | 
|  | 108 | +          path.join(currentModuleDirectory, 'ModuleUIStrings.js'), | 
|  | 109 | +          path.join(currentModuleDirectory, 'ModuleUIStrings.ts'), | 
|  | 110 | +        ]; | 
|  | 111 | + | 
|  | 112 | +        const actualPath = path.join( | 
|  | 113 | +            frontEndDirectory, | 
|  | 114 | +            `${firstArgument.value}`, | 
|  | 115 | +        ); | 
|  | 116 | + | 
|  | 117 | +        if (!allowedPathArguments.includes(actualPath)) { | 
|  | 118 | +          const newFileName = currentFileRelativeToFrontEnd.replace(/\\/g, '/'); | 
|  | 119 | +          context.report({ | 
|  | 120 | +            node, | 
|  | 121 | +            messageId: 'pathMismatch', | 
|  | 122 | +            data: { | 
|  | 123 | +              expectedPath: newFileName, | 
|  | 124 | +            }, | 
|  | 125 | +            fix(fixer) { | 
|  | 126 | +              return fixer.replaceText( | 
|  | 127 | +                  firstArgument, | 
|  | 128 | +                  `'${newFileName}'`, | 
|  | 129 | +              ); | 
|  | 130 | +            }, | 
|  | 131 | +          }); | 
|  | 132 | +        } | 
|  | 133 | +      }, | 
|  | 134 | +    }; | 
|  | 135 | +  }, | 
|  | 136 | +}); | 
0 commit comments