Skip to content

Commit

Permalink
feat(plugin-conventions): improve compatibility with uppercase resour…
Browse files Browse the repository at this point in the history
…ce name

Convention now support ua-foo.js to match resource class name UAFoo.
  • Loading branch information
3cp committed Sep 5, 2019
1 parent 625ec6a commit b67b839
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
36 changes: 36 additions & 0 deletions packages/__tests__/plugin-conventions/preprocess-resource.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ export class FooBar {}
assert.equal(result.code, expected);
});

it('injects customElement decorator for loosely equal class name', function () {
const code = `export class UAFooBar {}\n`;
const expected = `import * as __au2ViewDef from './ua-foo-bar.html';
import { customElement } from '@aurelia/runtime';
@customElement(__au2ViewDef)
export class UAFooBar {}
`;
const result = preprocessResource(
{
path: path.join('bar', 'ua-foo-bar.js'),
contents: code,
filePair: 'ua-foo-bar.html'
},
preprocessOptions()
);
assert.equal(result.code, expected);
});

it('injects view decorator', function () {
const code = `export class FooBar {}\n`;
const expected = `import * as __au2ViewDef from './foo-bar-view.html';
Expand All @@ -64,6 +82,24 @@ export class FooBar {}
assert.equal(result.code, expected);
});

it('injects view decorator for loosely equal class name', function () {
const code = `export class UAFooBar {}\n`;
const expected = `import * as __au2ViewDef from './ua-foo-bar-view.html';
import { view } from '@aurelia/runtime';
@view(__au2ViewDef)
export class UAFooBar {}
`;
const result = preprocessResource(
{
path: path.join('bar', 'ua-foo-bar.js'),
contents: code,
filePair: 'ua-foo-bar-view.html'
},
preprocessOptions()
);
assert.equal(result.code, expected);
});

it('injects customElement decorator for non-kebab case file name', function () {
const code = `export class FooBar {}\n`;
const expected = `import * as __au2ViewDef from './FooBar.html';
Expand Down
8 changes: 6 additions & 2 deletions packages/plugin-conventions/src/preprocess-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ function findDecoratedResourceType(node: ts.Node): ResourceType | void {
}
}

function isKindOfSame(name1: string, name2: string): boolean {
return name1.replace(/-/g, '') === name2.replace(/-/g, '');
}

function findResource(node: ts.Node, expectedResourceName: string, filePair: string | undefined, code: string): IFoundResource | void {
if (!ts.isClassDeclaration(node)) return;
if (!node.name) return;
Expand All @@ -206,7 +210,7 @@ function findResource(node: ts.Node, expectedResourceName: string, filePair: str

const className = node.name.text;
const {name, type} = nameConvention(className);
const isImplicitResource = name === expectedResourceName;
const isImplicitResource = isKindOfSame(name, expectedResourceName);
const decoratedType = findDecoratedResourceType(node);

if (decoratedType) {
Expand All @@ -220,7 +224,7 @@ function findResource(node: ts.Node, expectedResourceName: string, filePair: str
if (isImplicitResource && filePair) {
return {
implicitStatement: { pos: pos, end: node.end },
runtimeImportName: kebabCase(filePair).startsWith(name + '-view') ? 'view' : 'customElement'
runtimeImportName: kebabCase(filePair).startsWith(expectedResourceName + '-view') ? 'view' : 'customElement'
};
}
} else {
Expand Down

0 comments on commit b67b839

Please sign in to comment.