From 09038f696c8398f8223658f8e66f3e25f643832d Mon Sep 17 00:00:00 2001 From: Dmitry Zhifarsky Date: Sun, 9 Jan 2022 21:24:29 +0300 Subject: [PATCH] fix: support static getters for check unused l10n (#633) * fix: support static getters for check unused l10n * docs: move gtag in website config (#634) * fix: remove unreachable code --- CHANGELOG.md | 2 +- .../unused_l10n_analyzer/unused_l10n_visitor.dart | 14 ++++++++------ test/resources/unused_l10n_analyzer/test_i18n.dart | 3 +++ .../unused_l10n_analyzer_example.dart | 2 +- website/docusaurus.config.js | 6 +++--- 5 files changed, 16 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 41943d2be1..ec3fabed6a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,7 @@ * feat: facelift console reporters. * chore: restrict `analyzer` version to `>=2.4.0 <3.1.0`. * chore: restrict `analyzer_plugin` version to `>=0.8.0 <0.10.0`. -* feat: support extensions for check-unused-l10n. +* feat: support extensions and static getters for check-unused-l10n. * feat: improve `prefer-correct-type-name` rule. * build: activate check_unused_files on CI. diff --git a/lib/src/analyzers/unused_l10n_analyzer/unused_l10n_visitor.dart b/lib/src/analyzers/unused_l10n_analyzer/unused_l10n_visitor.dart index 2463302363..aac2d78412 100644 --- a/lib/src/analyzers/unused_l10n_analyzer/unused_l10n_visitor.dart +++ b/lib/src/analyzers/unused_l10n_analyzer/unused_l10n_visitor.dart @@ -76,12 +76,8 @@ class UnusedL10nVisitor extends RecursiveAstVisitor { final classTarget = (target as PrefixedIdentifier).identifier; _addMemberInvocationOnAccessor(classTarget, name); - } else if (_matchMethodOf(target)) { - final classTarget = (target as MethodInvocation).target; - - if (_matchIdentifier(classTarget)) { - _addMemberInvocation(classTarget as SimpleIdentifier, name); - } + } else if (_matchStaticGetter(target)) { + _addMemberInvocation((target as PrefixedIdentifier).prefix, name); } } @@ -102,6 +98,12 @@ class UnusedL10nVisitor extends RecursiveAstVisitor { target is PrefixedIdentifier && target.staticElement?.enclosingElement is ExtensionElement; + bool _matchStaticGetter(Expression? target) => + target is PrefixedIdentifier && + _classPattern.hasMatch( + target.staticType?.getDisplayString(withNullability: false) ?? '', + ); + void _addMemberInvocation(SimpleIdentifier target, String name) { final staticElement = target.staticElement; diff --git a/test/resources/unused_l10n_analyzer/test_i18n.dart b/test/resources/unused_l10n_analyzer/test_i18n.dart index 4a9e2a112b..2fb361d483 100644 --- a/test/resources/unused_l10n_analyzer/test_i18n.dart +++ b/test/resources/unused_l10n_analyzer/test_i18n.dart @@ -41,6 +41,9 @@ class S { return S(); } + + // ignore: prefer_constructors_over_static_methods + static S get current => S(); } class L10nClass { diff --git a/test/resources/unused_l10n_analyzer/unused_l10n_analyzer_example.dart b/test/resources/unused_l10n_analyzer/unused_l10n_analyzer_example.dart index f804060a1a..b35c5e3a36 100644 --- a/test/resources/unused_l10n_analyzer/unused_l10n_analyzer_example.dart +++ b/test/resources/unused_l10n_analyzer/unused_l10n_analyzer_example.dart @@ -23,6 +23,6 @@ class SomeClass { void method() { S.of('').regularMethod(''); - S.of('').regularGetter; + S.current.regularGetter; } } diff --git a/website/docusaurus.config.js b/website/docusaurus.config.js index 3c24180c40..e5d0989b73 100644 --- a/website/docusaurus.config.js +++ b/website/docusaurus.config.js @@ -11,9 +11,6 @@ module.exports = { projectName: 'dart-code-metrics', trailingSlash: false, themeConfig: { - gtag: { - trackingID: 'G-C2B16RHS34', - }, algolia: { apiKey: '77b6353dcfcae7da42fcb75524a90002', indexName: 'dartcodemetrics', @@ -146,6 +143,9 @@ module.exports = { theme: { customCss: require.resolve('./src/css/custom.css'), }, + gtag: { + trackingID: 'G-C2B16RHS34', + }, }, ], ],