diff --git a/tools/dgeni/processors/categorizer.js b/tools/dgeni/processors/categorizer.js index 3d6da71a05a..12e6c61a67e 100644 --- a/tools/dgeni/processors/categorizer.js +++ b/tools/dgeni/processors/categorizer.js @@ -46,7 +46,7 @@ module.exports = function categorizer() { // Categorize the current visited classDoc into its Angular type. if (isDirective(classDoc)) { classDoc.isDirective = true; - classDoc.directiveExportAs = getDirectiveExportAs(classDoc); + classDoc.directiveExportAs = getMetadataProperty(classDoc, 'exportAs'); classDoc.directiveSelectors = getDirectiveSelectors(classDoc); } else if (isService(classDoc)) { classDoc.isService = true; @@ -186,26 +186,25 @@ function getDirectiveOutputAlias(doc) { } function getDirectiveSelectors(classDoc) { - let metadata = classDoc.decorators - .find(d => d.name === 'Component' || d.name === 'Directive').arguments[0]; + const directiveSelectors = getMetadataProperty(classDoc, 'selector'); - let selectorMatches = /selector\s*:\s*(?:"|')([^']*?)(?:"|')/g.exec(metadata); - selectorMatches = selectorMatches && selectorMatches[1]; - - return selectorMatches ? selectorMatches.split(/\s*,\s*/) - .filter(s => s !== '' && !s.includes('mat') && !SELECTOR_BLACKLIST.has(s)) - : selectorMatches; + if (directiveSelectors) { + // Filter blacklisted selectors and remove line-breaks in resolved selectors. + return directiveSelectors.replace(/[\r\n]/g, '').split(/\s*,\s*/) + .filter(s => s !== '' && !s.includes('mat') && !SELECTOR_BLACKLIST.has(s)); + } } -function getDirectiveExportAs(doc) { - let metadata = doc.decorators - .find(d => d.name === 'Component' || d.name === 'Directive').arguments[0]; +function getMetadataProperty(doc, property) { + const metadata = doc.decorators + .find(d => d.name === 'Component' || d.name === 'Directive').arguments[0]; - // Use a Regex to determine the exportAs metadata because we can't parse the JSON due to - // environment variables inside of the JSON. - let exportMatches = /exportAs\s*:\s*(?:"|')(\w+)(?:"|')/g.exec(metadata); + // Use a Regex to determine the given metadata property. This is necessary, because we can't + // parse the JSON due to environment variables inside of the JSON (e.g module.id) + let matches = new RegExp(`${property}s*:\\s*(?:"|'|\`)((?:.|\\n|\\r)+?)(?:"|'|\`)`) + .exec(metadata); - return exportMatches && exportMatches[1]; + return matches && matches[1].trim(); } function hasMemberDecorator(doc, decoratorName) {