-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Strict Resolver, per RFC#1132 #21303
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
Open
NullVoxPopuli
wants to merge
32
commits into
main
Choose a base branch
from
nvp/strict-resolver-rfc-1132
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
b4319ca
Phase 1: copy content from ember-resolver so we can more easily asses…
NullVoxPopuli 0c04468
Copy tests from ember-strict-application-resolver
NullVoxPopuli e55c232
Adapt strict resolver tests to ember's codebase, remove AMD dependencies
NullVoxPopuli f90e45c
Replace classic resolver with StrictResolver per RFC#1132
NullVoxPopuli 0add00e
Address review: restore string utilities, expand tests, fix setup-res…
NullVoxPopuli 1ab8b52
Address review: revert package.json, add strict-resolver scenario test
NullVoxPopuli c8c25f4
Expand strict-resolver scenario with full route hierarchy and compone…
NullVoxPopuli 4355bbc
Fix CI failures: lint, type-check, docs coverage, and test cleanup
NullVoxPopuli a89a953
Shim Engine.Resolver so strict apps work with @ember/test-helpers
NullVoxPopuli 7ffb39c
Address review: inflection, colocation, drop unused helpers
NullVoxPopuli 578bd7b
Merge pull request #21307 from NullVoxPopuli-ai-agent/strict-resolver…
NullVoxPopuli 2609f4f
Scenario test: loading and error substates with strict resolver
NullVoxPopuli 1302135
Address review: drop modulePrefix from scenario app
NullVoxPopuli c557ce6
Merge pull request #21322 from NullVoxPopuli-ai-agent/scenario-loadin…
NullVoxPopuli bbd7bd8
Address review: drop cache, simplify pluralize, collapse setup-resolver
NullVoxPopuli a9cfe43
Fix prettier + drop deleted cache.js from package.json aliases
NullVoxPopuli a54e687
Drop strictResolver from v2AppScenarios; keep dedicated strict scenario
NullVoxPopuli f11a87b
Inline dasherize into strict-resolver.ts, drop the string/ directory
NullVoxPopuli dfe4b5c
Merge pull request #21326 from NullVoxPopuli-ai-agent/strict-resolver…
NullVoxPopuli 9b0f351
Apply suggestion from @NullVoxPopuli
NullVoxPopuli 7b1ca65
Register ember-page-title service in the strict-resolver scenario
NullVoxPopuli 4419a18
Merge pull request #21329 from NullVoxPopuli-ai-agent/fix-strictResol…
NullVoxPopuli 8ec017a
Add tests for the shorthand/`default` selection rule
NullVoxPopuli cec91b5
Merge pull request #21330 from NullVoxPopuli-ai-agent/shorthand-defau…
NullVoxPopuli 11e0966
Trim redundant tests from strict-resolver basic-test
NullVoxPopuli aa6e65b
Merge pull request #21331 from NullVoxPopuli-ai-agent/trim-redundant-…
NullVoxPopuli b6e004b
Trim cross-file redundancies in the strict-resolver tests
NullVoxPopuli 4924964
Fill the nested-route gaps in the strict-resolver scenario
NullVoxPopuli 8bdd546
Merge pull request #21333 from NullVoxPopuli-ai-agent/nested-route-co…
NullVoxPopuli 656f2cd
Fix some logic, re-organize tests
NullVoxPopuli f57675e
Apply suggestion from @NullVoxPopuli
NullVoxPopuli a292168
Cleanup
NullVoxPopuli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| import type { Factory, Resolver } from '@ember/owner'; | ||
|
|
||
| export class StrictResolver implements Resolver { | ||
| // Ember's router uses this flag to decide whether to auto-generate | ||
| // `${name}_loading` and `${name}_error` substates for routes defined in | ||
| // `Router.map(...)`. Since we always resolve against an ES module registry, | ||
| // we unconditionally opt in. | ||
| moduleBasedResolver = true; | ||
|
|
||
| #modules = new Map<string, unknown>(); | ||
| #plurals = new Map<string, string>(); | ||
| original: any; | ||
|
|
||
| constructor( | ||
| modules: Record<string, unknown>, | ||
| plurals: Record<string, string> | undefined = undefined | ||
| ) { | ||
| this.addModules(modules); | ||
| this.#plurals.set('config', 'config'); | ||
| if (plurals) { | ||
| for (let [singular, plural] of Object.entries(plurals)) { | ||
| this.#plurals.set(singular, plural); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| addModules(modules: Record<string, unknown>) { | ||
| for (let [moduleName, module] of Object.entries(modules)) { | ||
| this.#modules.set(this.#normalizeModule(moduleName), module); | ||
| } | ||
| } | ||
|
|
||
| #normalizeModule(moduleName: string) { | ||
| return moduleName.replace(fileExtension, '').replace(leadingDotSlash, ''); | ||
| } | ||
|
|
||
| #plural(s: string) { | ||
| return this.#plurals.get(s) ?? s + 's'; | ||
| } | ||
|
|
||
| resolve(fullName: string): Factory<object> | object | undefined { | ||
| let [type, name] = fullName.split(':') as [string, string]; | ||
| name = this.#normalizeName(type, name); | ||
| for (let strategy of [ | ||
| this.#resolveSelf, | ||
| this.#mainLookup, | ||
| this.#defaultLookup, | ||
| this.#nestedColocationLookup, | ||
| ]) { | ||
| let result = strategy.call(this, type, name); | ||
| if (result) { | ||
| return this.#extractDefaultExport(result.hit); | ||
| } | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| #extractDefaultExport(module: any): Factory<object> | object | undefined { | ||
| if (module && module['default']) { | ||
| module = module['default']; | ||
| } | ||
| return module as Factory<object> | object | undefined; | ||
| } | ||
|
|
||
| normalize(fullName: `${string}:${string}`): `${string}:${string}` { | ||
| let [type, name] = fullName.split(':') as [string, string]; | ||
| name = this.#normalizeName(type, name); | ||
| return `${type}:${name}`; | ||
| } | ||
|
|
||
| #normalizeName(type: string, name: string): string { | ||
| if ( | ||
| type === 'component' || | ||
| type === 'helper' || | ||
| type === 'modifier' || | ||
| (type === 'template' && name.indexOf('components/') === 0) | ||
| ) { | ||
| return name.replace(/_/g, '-'); | ||
| } else { | ||
| return dasherize(name.replace(/\./g, '/')); | ||
| } | ||
| } | ||
|
|
||
| #resolveSelf(type: string, name: string): Result { | ||
| if (type === 'resolver' && name === 'current') { | ||
| return { | ||
| hit: { | ||
| create: () => this, | ||
| }, | ||
| }; | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| #mainLookup(type: string, name: string): Result { | ||
| if (name === 'main') { | ||
| let module = this.#modules.get(type); | ||
| if (module) { | ||
| return { hit: module }; | ||
| } | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| #defaultLookup(type: string, name: string): Result { | ||
| let dir = this.#plural(type); | ||
| let target = `${dir}/${name}`; | ||
| let module = this.#modules.get(target); | ||
| if (module) { | ||
| return { hit: module }; | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| // Supports the nested colocation pattern where `component:my-widget` | ||
| // resolves to `./components/my-widget/index.{js,ts,gjs,gts}`. The index | ||
| // file is typically the component class, and it's commonly paired with a | ||
| // sibling `index.hbs` inside the same folder. | ||
| #nestedColocationLookup(type: string, name: string): Result { | ||
| if (type !== 'component') return undefined; | ||
|
|
||
| let dir = this.#plural(type); | ||
| let target = `${dir}/${name}/index`; | ||
| let module = this.#modules.get(target); | ||
| if (module) { | ||
| return { hit: module }; | ||
| } | ||
| return undefined; | ||
| } | ||
| } | ||
|
|
||
| const fileExtension = /\.\w{1,4}$/; | ||
| const leadingDotSlash = /^\.\//; | ||
| const camelCaseBoundary = /([a-z\d])([A-Z])/g; | ||
| const spacesAndUnderscores = /[ _]/g; | ||
|
|
||
| function dasherize(str: string): string { | ||
| return str.replace(camelCaseBoundary, '$1_$2').toLowerCase().replace(spacesAndUnderscores, '-'); | ||
| } | ||
|
|
||
| type Result = | ||
| | { | ||
| hit: any; | ||
| } | ||
| | undefined; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.