|
| 1 | +// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +library linter.src.plugin.linter_plugin; |
| 6 | + |
| 7 | +import 'package:linter/plugin/linter.dart'; |
| 8 | +import 'package:linter/src/linter.dart'; |
| 9 | +import 'package:linter/src/rules/camel_case_types.dart'; |
| 10 | +import 'package:linter/src/rules/constant_identifier_names.dart'; |
| 11 | +import 'package:linter/src/rules/empty_constructor_bodies.dart'; |
| 12 | +import 'package:linter/src/rules/library_names.dart'; |
| 13 | +import 'package:linter/src/rules/library_prefixes.dart'; |
| 14 | +import 'package:linter/src/rules/non_constant_identifier_names.dart'; |
| 15 | +import 'package:linter/src/rules/one_member_abstracts.dart'; |
| 16 | +import 'package:linter/src/rules/slash_for_doc_comments.dart'; |
| 17 | +import 'package:linter/src/rules/super_goes_last.dart'; |
| 18 | +import 'package:linter/src/rules/type_init_formals.dart'; |
| 19 | +import 'package:linter/src/rules/unnecessary_brace_in_string_interp.dart'; |
| 20 | +import 'package:plugin/plugin.dart'; |
| 21 | + |
| 22 | +/// The shared linter plugin instance. |
| 23 | +final LinterPlugin linterPlugin = new LinterPlugin(); |
| 24 | + |
| 25 | +/// A plugin that defines the extension points and extensions that are |
| 26 | +/// inherently defined by the linter. |
| 27 | +class LinterPlugin implements Plugin { |
| 28 | + |
| 29 | + /// A subset of rules that we are considering enabled by "default". |
| 30 | + static final List<LintRule> _rules = [ |
| 31 | + new CamelCaseTypes(), |
| 32 | + new ConstantIdentifierNames(), |
| 33 | + new EmptyConstructorBodies(), |
| 34 | + new LibraryNames(), |
| 35 | + new LibraryPrefixes(), |
| 36 | + new NonConstantIdentifierNames(), |
| 37 | + new OneMemberAbstracts(), |
| 38 | + new SlashForDocComments(), |
| 39 | + new SuperGoesLast(), |
| 40 | + new TypeInitFormals(), |
| 41 | + new UnnecessaryBraceInStringInterp() |
| 42 | + ]; |
| 43 | + |
| 44 | + /// The unique identifier of this plugin. |
| 45 | + static const String UNIQUE_IDENTIFIER = 'linter.core'; |
| 46 | + |
| 47 | + /// The simple identifier of the extension point that allows plugins to |
| 48 | + /// register new lint rules. |
| 49 | + static const String LINT_RULE_EXTENSION_POINT = 'rule'; |
| 50 | + |
| 51 | + /// The extension point that allows plugins to register new lint rules. |
| 52 | + ExtensionPoint lintRuleExtensionPoint; |
| 53 | + |
| 54 | + /// Return a list containing all contributed lint rules. |
| 55 | + List<LintRule> get lintRules => lintRuleExtensionPoint.extensions; |
| 56 | + |
| 57 | + @override |
| 58 | + String get uniqueIdentifier => UNIQUE_IDENTIFIER; |
| 59 | + |
| 60 | + @override |
| 61 | + void registerExtensionPoints(RegisterExtensionPoint registerExtensionPoint) { |
| 62 | + lintRuleExtensionPoint = registerExtensionPoint( |
| 63 | + LINT_RULE_EXTENSION_POINT, _validateTaskExtension); |
| 64 | + } |
| 65 | + |
| 66 | + @override |
| 67 | + void registerExtensions(RegisterExtension registerExtension) { |
| 68 | + _rules.forEach((LintRule rule) => |
| 69 | + registerExtension(LINT_RULE_EXTENSION_POINT_ID, rule)); |
| 70 | + } |
| 71 | + |
| 72 | + void _validateTaskExtension(Object extension) { |
| 73 | + if (extension is! LintRule) { |
| 74 | + String id = lintRuleExtensionPoint.uniqueIdentifier; |
| 75 | + throw new ExtensionError('Extensions to $id must implement LintRule'); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments