Skip to content

Commit 3acf2a2

Browse files
devversionandrewseguin
authored andcommitted
build: fix multi-line selectors in dgeni (#4336)
* Fixes multi-line selectors not being detected properly (https://github.com/angular/material2/blob/master/src/lib/button/button.ts#L185-L185) * Template strings not being detected properly (https://github.com/angular/material2/blob/master/src/lib/input/input-container.ts#L103-L103) * No longer repeats RegExp logic multiple times. More generic function.
1 parent 244aece commit 3acf2a2

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

tools/dgeni/processors/categorizer.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ module.exports = function categorizer() {
4646
// Categorize the current visited classDoc into its Angular type.
4747
if (isDirective(classDoc)) {
4848
classDoc.isDirective = true;
49-
classDoc.directiveExportAs = getDirectiveExportAs(classDoc);
49+
classDoc.directiveExportAs = getMetadataProperty(classDoc, 'exportAs');
5050
classDoc.directiveSelectors = getDirectiveSelectors(classDoc);
5151
} else if (isService(classDoc)) {
5252
classDoc.isService = true;
@@ -186,26 +186,25 @@ function getDirectiveOutputAlias(doc) {
186186
}
187187

188188
function getDirectiveSelectors(classDoc) {
189-
let metadata = classDoc.decorators
190-
.find(d => d.name === 'Component' || d.name === 'Directive').arguments[0];
191-
192-
let selectorMatches = /selector\s*:\s*(?:"|')([^']*?)(?:"|')/g.exec(metadata);
193-
selectorMatches = selectorMatches && selectorMatches[1];
189+
const directiveSelectors = getMetadataProperty(classDoc, 'selector');
194190

195-
return selectorMatches ? selectorMatches.split(/\s*,\s*/)
196-
.filter(s => s !== '' && !s.includes('mat') && !SELECTOR_BLACKLIST.has(s))
197-
: selectorMatches;
191+
if (directiveSelectors) {
192+
// Filter blacklisted selectors and remove line-breaks in resolved selectors.
193+
return directiveSelectors.replace(/[\r\n]/g, '').split(/\s*,\s*/)
194+
.filter(s => s !== '' && !s.includes('mat') && !SELECTOR_BLACKLIST.has(s));
195+
}
198196
}
199197

200-
function getDirectiveExportAs(doc) {
201-
let metadata = doc.decorators
202-
.find(d => d.name === 'Component' || d.name === 'Directive').arguments[0];
198+
function getMetadataProperty(doc, property) {
199+
const metadata = doc.decorators
200+
.find(d => d.name === 'Component' || d.name === 'Directive').arguments[0];
203201

204-
// Use a Regex to determine the exportAs metadata because we can't parse the JSON due to
205-
// environment variables inside of the JSON.
206-
let exportMatches = /exportAs\s*:\s*(?:"|')(\w+)(?:"|')/g.exec(metadata);
202+
// Use a Regex to determine the given metadata property. This is necessary, because we can't
203+
// parse the JSON due to environment variables inside of the JSON (e.g module.id)
204+
let matches = new RegExp(`${property}s*:\\s*(?:"|'|\`)((?:.|\\n|\\r)+?)(?:"|'|\`)`)
205+
.exec(metadata);
207206

208-
return exportMatches && exportMatches[1];
207+
return matches && matches[1].trim();
209208
}
210209

211210
function hasMemberDecorator(doc, decoratorName) {

0 commit comments

Comments
 (0)