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 b565eb7
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/core/application.ts
Original file line number Diff line number Diff line change
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
8 changes: 8 additions & 0 deletions src/core/binding_observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,12 @@ export class BindingObserver implements ValueListObserverDelegate<Action> {
elementUnmatchedValue(element: Element, action: Action) {
this.disconnectAction(action)
}

elementMatchedNoValue(element: Element, token: Token) {
this.context.handleWarning(
`Element references undefined action ${token.content}`,
`Warning connecting action ${token.content}`,
{ element, action: token.content }
)
}
}
6 changes: 6 additions & 0 deletions src/core/context.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
5 changes: 4 additions & 1 deletion src/mutation-observers/value_list_observer.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 b565eb7

Please sign in to comment.