Skip to content

Commit

Permalink
apply rules to colocated templates
Browse files Browse the repository at this point in the history
Before, component rules about the component itself were not getting applied to co-located templates unless you explicitly set a layout on the rule.
  • Loading branch information
ef4 committed Nov 25, 2020
1 parent 6a0895d commit 17c2d31
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 18 deletions.
57 changes: 39 additions & 18 deletions packages/compat/src/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import {
Resolver,
TemplateCompiler,
PackageCache,
explicitRelative,
extensionsPattern,
Package,
} from '@embroider/core';
import {
ActivePackageRules,
ComponentRules,
ModuleRules,
PackageRules,
PreprocessedComponentRule,
preprocessComponentRule,
ActivePackageRules,
ModuleRules,
} from './dependency-rules';
import {
Package,
PackageCache,
Resolver,
TemplateCompiler,
explicitRelative,
extensionsPattern,
} from '@embroider/core';
import { dirname, join, relative, sep } from 'path';

import { Options as AdjustImportsOptions } from '@embroider/core/src/babel-plugin-adjust-imports';
import { Memoize } from 'typescript-memoize';
import Options from './options';
import { join, relative, dirname, sep } from 'path';
import { pathExistsSync } from 'fs-extra';
import { ResolvedDep } from '@embroider/core/src/resolver';
import { dasherize } from './dasherize-component-name';
import { makeResolverTransform } from './resolver-transform';
import { Memoize } from 'typescript-memoize';
import { ResolvedDep } from '@embroider/core/src/resolver';
import { Options as AdjustImportsOptions } from '@embroider/core/src/babel-plugin-adjust-imports';

import { pathExistsSync } from 'fs-extra';
import resolve from 'resolve';

export interface ComponentResolution {
Expand Down Expand Up @@ -160,8 +160,29 @@ export default class CompatResolver implements Resolver {
return resolution;
}

private findComponentRules(absPath: string) {
return this.rules.components.get(absPath);
private findComponentRules(absPath: string): PreprocessedComponentRule | undefined {
let rules = this.rules.components.get(absPath);
if (rules) {
return rules;
}

// co-located templates aren't visible to the resolver, because they never
// get resolved from a template (they are always handled directly by the
// corresponding JS module, which the resolver *does* see). This means their
// paths won't ever be in `this.rules.components`. But we do want them to
// "inherit" the rules that are attached to their corresonding JS module.
if (absPath.endsWith('.hbs')) {
let stem = absPath.slice(0, -4);
for (let ext of this.params.adjustImportsOptions.resolvableExtensions) {
if (ext !== '.hbs') {
let rules = this.rules.components.get(stem + ext);
if (rules) {
return rules;
}
}
}
}
return undefined;
}

private isIgnoredComponent(dasherizedName: string) {
Expand Down
16 changes: 16 additions & 0 deletions packages/compat/tests/resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,22 @@ describe('compat-resolver', function () {
);
});

test('acceptsComponentArguments matches co-located template', function () {
let packageRules = [
{
package: 'the-app',
components: {
'<FormBuilder />': {
acceptsComponentArguments: ['title'],
},
},
},
];
let findDependencies = configure({ staticComponents: true, packageRules });
givenFile('components/form-builder.js');
expect(findDependencies('components/form-builder.hbs', `{{component title}}`)).toEqual([]);
});

test(`element block params are not in scope for element's own attributes`, function () {
let packageRules = [
{
Expand Down

0 comments on commit 17c2d31

Please sign in to comment.