Skip to content

Commit

Permalink
feat(compiler): warn about event name conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat committed Jul 7, 2019
1 parent feea58c commit 015ea32
Showing 1 changed file with 274 additions and 6 deletions.
280 changes: 274 additions & 6 deletions src/compiler/transformers/decorators-to-static/event-decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function parseEventDecorator(config: d.Config, diagnostics: d.Diagnostic[], type
const symbol = typeChecker.getSymbolAtLocation(prop.name);
const name = getEventName(opts, memberName);

validateEventEmitterMemberName(config, diagnostics, prop.name, memberName);
validateEventName(config, diagnostics, prop.name, name);

const eventMeta = {
method: memberName,
Expand Down Expand Up @@ -77,16 +77,284 @@ function getEventType(type: ts.TypeNode): ts.TypeNode | null {
return null;
}

function validateEventEmitterMemberName(config: d.Config, diagnostics: d.Diagnostic[], node: ts.Node, memberName: string) {
const firstChar = memberName.charAt(0);
function validateEventName(config: d.Config, diagnostics: d.Diagnostic[], node: ts.Node, eventName: string) {

if (/[A-Z]/.test(firstChar)) {
if (/^[A-Z]/.test(eventName)) {
const diagnostic = buildWarn(diagnostics);
diagnostic.messageText = [
`In order to be compatible with all event listeners on elements, the `,
`@Event() "${memberName}" cannot start with a capital letter. `,
`In order to be compatible with all event listeners on elements, the event name `,
`cannot start with a capital letter. `,
`Please lowercase the first character for the event to best work with all listeners.`
].join('');
augmentDiagnosticWithNode(config, diagnostic, node);
}

if (DOM_EVENT_NAMES.has(eventName)) {
const diagnostic = buildWarn(diagnostics);
diagnostic.messageText = `The event name conflicts with the "${eventName}" native DOM event name.`;
augmentDiagnosticWithNode(config, diagnostic, node);
}
}

const DOM_EVENT_NAMES: Set<string> = new Set([
'CheckboxStateChange',
'DOMContentLoaded',
'DOMMenuItemActive',
'DOMMenuItemInactive',
'DOMMouseScroll',
'MSManipulationStateChanged',
'MSPointerHover',
'MozAudioAvailable',
'MozGamepadButtonDown',
'MozGamepadButtonUp',
'MozMousePixelScroll',
'MozOrientation',
'MozScrolledAreaChanged',
'RadioStateChange',
'SVGAbort',
'SVGError',
'SVGLoad',
'SVGResize',
'SVGScroll',
'SVGUnload',
'SVGZoom',
'ValueChange',
'abort',
'afterprint',
'afterscriptexecute',
'alerting',
'animationcancel',
'animationend',
'animationiteration',
'animationstart',
'appinstalled',
'audioend',
'audioprocess',
'audiostart',
'auxclick',
'beforeinstallprompt',
'beforeprint',
'beforescriptexecute',
'beforeunload',
'beginEvent',
'blur',
'boundary',
'broadcast',
'busy',
'callschanged',
'canplay',
'canplaythrough',
'cardstatechange',
'cfstatechange',
'change',
'chargingchange',
'chargingtimechange',
'checking',
'click',
'command',
'commandupdate',
'compassneedscalibration',
'complete',
'compositionend',
'compositionstart',
'compositionupdate',
'connected',
'connecting',
'connectionInfoUpdate',
'contextmenu',
'copy',
'cut',
'datachange',
'dataerror',
'dblclick',
'delivered',
'devicechange',
'devicemotion',
'deviceorientation',
'dialing',
'disabled',
'dischargingtimechange',
'disconnected',
'disconnecting',
'downloading',
'drag',
'dragend',
'dragenter',
'dragleave',
'dragover',
'dragstart',
'drop',
'durationchange',
'emptied',
'enabled',
'end',
'endEvent',
'ended',
'error',
'focus',
'focusin',
'focusout',
'fullscreenchange',
'fullscreenerror',
'gamepadconnected',
'gamepaddisconnected',
'gotpointercapture',
'hashchange',
'held',
'holding',
'icccardlockerror',
'iccinfochange',
'incoming',
'input',
'invalid',
'keydown',
'keypress',
'keyup',
'languagechange',
'levelchange',
'load',
'loadeddata',
'loadedmetadata',
'loadend',
'loadstart',
'localized',
'lostpointercapture',
'mark',
'message',
'messageerror',
'mousedown',
'mouseenter',
'mouseleave',
'mousemove',
'mouseout',
'mouseover',
'mouseup',
'mousewheel',
'mozbrowseractivitydone',
'mozbrowserasyncscroll',
'mozbrowseraudioplaybackchange',
'mozbrowsercaretstatechanged',
'mozbrowserclose',
'mozbrowsercontextmenu',
'mozbrowserdocumentfirstpaint',
'mozbrowsererror',
'mozbrowserfindchange',
'mozbrowserfirstpaint',
'mozbrowsericonchange',
'mozbrowserloadend',
'mozbrowserloadstart',
'mozbrowserlocationchange',
'mozbrowsermanifestchange',
'mozbrowsermetachange',
'mozbrowseropensearch',
'mozbrowseropentab',
'mozbrowseropenwindow',
'mozbrowserresize',
'mozbrowserscroll',
'mozbrowserscrollareachanged',
'mozbrowserscrollviewchange',
'mozbrowsersecuritychange',
'mozbrowserselectionstatechanged',
'mozbrowsershowmodalprompt',
'mozbrowsertitlechange',
'mozbrowserusernameandpasswordrequired',
'mozbrowservisibilitychange',
'moztimechange',
'msContentZoom',
'nomatch',
'notificationclick',
'noupdate',
'obsolete',
'offline',
'online',
'orientationchange',
'overflow',
'pagehide',
'pageshow',
'paste',
'pause',
'play',
'playing',
'pointercancel',
'pointerdown',
'pointerenter',
'pointerleave',
'pointerlockchange',
'pointerlockerror',
'pointermove',
'pointerout',
'pointerover',
'pointerup',
'popstate',
'popuphidden',
'popuphiding',
'popupshowing',
'popupshown',
'progress',
'push',
'pushsubscriptionchange',
'ratechange',
'readystatechange',
'received',
'repeatEvent',
'reset',
'resize',
'resourcetimingbufferfull',
'result',
'resume',
'resuming',
'scroll',
'seeked',
'seeking',
'select',
'selectionchange',
'selectstart',
'sent',
'show',
'slotchange',
'smartcard-insert',
'smartcard-remove',
'soundend',
'soundstart',
'speechend',
'speechstart',
'stalled',
'start',
'statechange',
'statuschange',
'stkcommand',
'stksessionend',
'storage',
'submit',
'suspend',
'timeout',
'timeupdate',
'touchcancel',
'touchend',
'touchenter',
'touchleave',
'touchmove',
'touchstart',
'transitioncancel',
'transitionend',
'transitionrun',
'transitionstart',
'underflow',
'unload',
'updateready',
'userproximity',
'ussdreceived',
'visibilitychange',
'voicechange',
'voiceschanged',
'volumechange',
'vrdisplayactivate',
'vrdisplayblur',
'vrdisplayconnect',
'vrdisplaydeactivate',
'vrdisplaydisconnect',
'vrdisplayfocus',
'vrdisplaypresentchange',
'waiting',
'wheel',
]);

0 comments on commit 015ea32

Please sign in to comment.