Skip to content

Commit

Permalink
warning on undefined controller or action
Browse files Browse the repository at this point in the history
  • Loading branch information
AndersGM committed Feb 8, 2023
1 parent 8217e9b commit 6ededb8
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/core/application.ts
Expand Up @@ -15,6 +15,7 @@ export class Application implements ErrorHandler {
readonly actionDescriptorFilters: ActionDescriptorFilters
logger: Logger = console
debug = false
warnings = true

static start(element?: Element, schema?: Schema): Application {
const application = new this(element, schema)
Expand Down Expand Up @@ -90,6 +91,12 @@ export class Application implements ErrorHandler {
window.onerror?.(message, "", 0, 0, error)
}

handleWarning(warning: string, message: string, detail: object) {
if (this.warnings) {
this.logger.warn(`%s\n\n%s\n\n%o`, message, warning, detail)
}
}

// Debug logging

logDebugActivity = (identifier: string, functionName: string, detail: object = {}): void => {
Expand Down
9 changes: 9 additions & 0 deletions src/core/binding_observer.ts
Expand Up @@ -92,4 +92,13 @@ export class BindingObserver implements ValueListObserverDelegate<Action> {
elementUnmatchedValue(element: Element, action: Action) {
this.disconnectAction(action)
}

elementMatchedNoValue(element: Element, token: Token) {
const { content } = token
this.context.handleWarning(
`Element references an undefined action "${content}"`,
`Warning connecting action ${content}`,
{ element, action: content }
)
}
}
6 changes: 6 additions & 0 deletions src/core/context.ts
Expand Up @@ -101,6 +101,12 @@ export class Context implements ErrorHandler, TargetObserverDelegate, OutletObse
this.application.handleError(error, `Error ${message}`, detail)
}

handleWarning(warning: string, message: string, detail: object = {}) {
const { identifier, controller, element } = this
detail = Object.assign({ identifier, controller, element }, detail)
this.application.handleWarning(warning, `Warning ${message}`, detail)
}

// Debug logging

logDebugActivity = (functionName: string, detail: object = {}): void => {
Expand Down
11 changes: 9 additions & 2 deletions src/core/router.ts
Expand Up @@ -88,10 +88,17 @@ export class Router implements ScopeObserverDelegate {
}

scopeConnected(scope: Scope) {
this.scopesByIdentifier.add(scope.identifier, scope)
const module = this.modulesByIdentifier.get(scope.identifier)
const { element, identifier } = scope
this.scopesByIdentifier.add(identifier, scope)
const module = this.modulesByIdentifier.get(identifier)
if (module) {
module.connectContextForScope(scope)
} else {
this.application.handleWarning(
`Element references undefined controller ${identifier}`,
`Warning connecting controller ${identifier}`,
{ identifier, element }
)
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/core/scope_observer.ts
Expand Up @@ -61,6 +61,8 @@ export class ScopeObserver implements ValueListObserverDelegate<Scope> {
}
}

elementMatchedNoValue(element: Element, token: Token) {}

elementUnmatchedValue(element: Element, value: Scope) {
const referenceCount = this.scopeReferenceCounts.get(value)
if (referenceCount) {
Expand Down
5 changes: 4 additions & 1 deletion src/mutation-observers/value_list_observer.ts
Expand Up @@ -3,6 +3,7 @@ import { Token, TokenListObserver, TokenListObserverDelegate } from "./token_lis
export interface ValueListObserverDelegate<T> {
parseValueForToken(token: Token): T | undefined
elementMatchedValue(element: Element, value: T): void
elementMatchedNoValue(element: Element, token: Token): void
elementUnmatchedValue(element: Element, value: T): void
}

Expand Down Expand Up @@ -50,10 +51,12 @@ export class ValueListObserver<T> implements TokenListObserverDelegate {

tokenMatched(token: Token) {
const { element } = token
const { value } = this.fetchParseResultForToken(token)
const { error, value } = this.fetchParseResultForToken(token)
if (value) {
this.fetchValuesByTokenForElement(element).set(token, value)
this.delegate.elementMatchedValue(element, value)
} else if (error) {
this.delegate.elementMatchedNoValue(element, token)
}
}

Expand Down
Expand Up @@ -100,4 +100,8 @@ export default class ValueListObserverTests extends ObserverTestCase implements
elementUnmatchedValue(element: Element, value: Value) {
this.recordCall("elementUnmatchedValue", element, value.id, value.token.content)
}

elementMatchedNoValue(element: Element, token: Token) {
this.recordCall("elementMatchedNoValue", element, token)
}
}

0 comments on commit 6ededb8

Please sign in to comment.