Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Directives class name selectors #87

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/directive.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
Declaration, defineMetadata, getAttributeName, getMetadata, isAttributeSelector, kebabToCamel,
metadataKeys
metadataKeys, tryGetDirectiveSelector
} from './utils';
import { IHostListeners } from './hostListener';
import { IViewChildren } from './viewChild';
Expand All @@ -26,9 +26,10 @@ export function Directive({selector, ...options}: DirectiveOptionsDecorated) {
options.require = require;
if (!options.bindToController) options.bindToController = true;
}
options.restrict = options.restrict || 'A';

const selectorName = isAttributeSelector(selector) ? getAttributeName(selector) : selector;
const selectorTyped = tryGetDirectiveSelector(selector);
const selectorName = selectorTyped.selector || selector;
options.restrict = options.restrict || selectorTyped.restrict || 'A';
defineMetadata(metadataKeys.name, kebabToCamel(selectorName), ctrl);
defineMetadata(metadataKeys.declaration, Declaration.Directive, ctrl);
defineMetadata(metadataKeys.options, options, ctrl);
Expand Down
18 changes: 18 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ import 'reflect-metadata';

export enum Declaration { Component = 'Component', Directive = 'Directive', Pipe = 'Pipe' }

export type DirectiveRestriction = 'A' | 'E' | 'C';

/** @internal */
export const knownDirectiveTypes: Record<DirectiveRestriction, RegExp> = {
A: /^\[([a-z][a-zA-Z\-_]*?)\]$/,
E: /^([a-z\-]*)$/,
C: /^\.([a-z][a-zA-Z\-_]*?)$/
};

/** @internal */
export const metadataKeys = {
declaration: 'custom:declaration',
Expand Down Expand Up @@ -31,6 +40,15 @@ export function isAttributeSelector(selector: string) {
return /^[\[].*[\]]$/g.test(selector);
}

/** @internal */
export function tryGetDirectiveSelector(selector: string): Partial<{ selector: string, restrict: DirectiveRestriction }> {
for (const restrictBy of Object.keys(knownDirectiveTypes)) {
const match = selector.match(knownDirectiveTypes[restrictBy]);
if (match && match[1]) return { selector: match[1], restrict: restrictBy as DirectiveRestriction };
}
return {};
}

/** @internal */
export function getMetadata(metadataKey: any, target: any): any {
return Reflect.getMetadata(metadataKey, target);
Expand Down
3 changes: 2 additions & 1 deletion test/ng-module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,10 @@ describe('NgModule', () => {
directive('camel-case-name'),
directive('[camelCaseName]'),
directive('[camel-case-name]'),
directive('.camel-case-name'),
]);
const invokeQueue = angular.module(moduleName)['_invokeQueue'];
expect(invokeQueue.length).toEqual(4);
expect(invokeQueue.length).toEqual(5);
invokeQueue.forEach((value: any) => {
expect(value[0]).toEqual('$compileProvider');
expect(value[1]).toEqual('directive');
Expand Down