/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ import * as eslint from 'eslint'; import { TSESTree } from '@typescript-eslint/utils'; /** * Prevents theme icon syntax `$(iconName)` from appearing inside localized * string arguments. Localizers may translate or corrupt the icon syntax, * breaking rendering. Icon references should be kept outside the localized * string - either prepended via concatenation or passed as a placeholder * argument. * * Examples: * ❌ localize('key', "$(gear) Settings") * ✅ '$(gear) ' + localize('key', "Settings") * ✅ localize('key', "Like {0}", '$(gear)') * * ❌ nls.localize('key', "$(loading~spin) Loading...") * ✅ '$(loading~spin) ' + nls.localize('key', "Loading...") */ export default new class NoIconsInLocalizedStrings implements eslint.Rule.RuleModule { readonly meta: eslint.Rule.RuleMetaData = { messages: { noIconInLocalizedString: 'Theme icon syntax $(…) should not appear inside localized strings. Move it outside the localize call or pass it as a placeholder argument.' }, docs: { description: 'Prevents $(icon) theme icon syntax inside localize() string arguments', }, type: 'problem', schema: false, }; create(context: eslint.Rule.RuleContext): eslint.Rule.RuleListener { // Matches $(iconName) or $(iconName~modifier) but not escaped \$(...) const iconPattern = /(? checkCallExpression(node as TSESTree.CallExpression) }; } };