protected override void ExecuteCore(RazorCodeDocument codeDocument) { var syntaxTree = codeDocument.GetSyntaxTree(); ThrowForMissingDocumentDependency(syntaxTree); var descriptors = codeDocument.GetTagHelpers(); if (descriptors == null) { var feature = Engine.Features.OfType <ITagHelperFeature>().FirstOrDefault(); if (feature == null) { // No feature, nothing to do. return; } descriptors = feature.GetDescriptors(); } // We need to find directives in all of the *imports* as well as in the main razor file // // The imports come logically before the main razor file and are in the order they // should be processed. DirectiveVisitor visitor = null; var parserOptions = codeDocument.GetParserOptions(); if (FileKinds.IsComponent(codeDocument.GetFileKind()) && (parserOptions == null || parserOptions.FeatureFlags.AllowComponentFileKind)) { codeDocument.TryComputeNamespace(fallbackToRootNamespace: true, out var currentNamespace); visitor = new ComponentDirectiveVisitor(codeDocument.Source.FilePath, descriptors, currentNamespace); } else { visitor = new TagHelperDirectiveVisitor(descriptors); } var imports = codeDocument.GetImportSyntaxTrees(); if (imports != null) { for (var i = 0; i < imports.Count; i++) { var import = imports[i]; visitor.Visit(import); } } visitor.Visit(syntaxTree); // This will always be null for a component document. var tagHelperPrefix = visitor.TagHelperPrefix; descriptors = visitor.Matches.ToArray(); var context = TagHelperDocumentContext.Create(tagHelperPrefix, descriptors); codeDocument.SetTagHelperContext(context); if (descriptors.Count == 0) { // No descriptors, no-op. return; } var rewrittenSyntaxTree = TagHelperParseTreeRewriter.Rewrite(syntaxTree, tagHelperPrefix, descriptors); codeDocument.SetSyntaxTree(rewrittenSyntaxTree); }
protected override void ExecuteCore(RazorCodeDocument codeDocument) { var syntaxTree = codeDocument.GetSyntaxTree(); ThrowForMissingDocumentDependency(syntaxTree); var feature = Engine.Features.OfType <ITagHelperFeature>().FirstOrDefault(); if (feature == null) { // No feature, nothing to do. return; } // We need to find directives in all of the *imports* as well as in the main razor file // // The imports come logically before the main razor file and are in the order they // should be processed. var descriptors = feature.GetDescriptors(); var visitor = new DirectiveVisitor(descriptors); var imports = codeDocument.GetImportSyntaxTrees(); if (imports != null) { for (var i = 0; i < imports.Count; i++) { var import = imports[i]; visitor.VisitBlock(import.Root); } } visitor.VisitBlock(syntaxTree.Root); var tagHelperPrefix = visitor.TagHelperPrefix; descriptors = visitor.Matches.ToArray(); var context = TagHelperDocumentContext.Create(tagHelperPrefix, descriptors); codeDocument.SetTagHelperContext(context); if (descriptors.Count == 0) { // No descriptors, no-op. return; } var errorSink = new ErrorSink(); var rewriter = new TagHelperParseTreeRewriter(tagHelperPrefix, descriptors, syntaxTree.Options.FeatureFlags); var root = syntaxTree.Root; root = rewriter.Rewrite(root, errorSink); // Temporary code while we're still using legacy diagnostics in the SyntaxTree. var errorList = new List <RazorDiagnostic>(); errorList.AddRange(errorSink.Errors.Select(error => RazorDiagnostic.Create(error))); errorList.AddRange(descriptors.SelectMany(d => d.GetAllDiagnostics())); var diagnostics = CombineErrors(syntaxTree.Diagnostics, errorList); var newSyntaxTree = RazorSyntaxTree.Create(root, syntaxTree.Source, diagnostics, syntaxTree.Options); codeDocument.SetSyntaxTree(newSyntaxTree); }