Skip to content

Commit

Permalink
fix: don't include type arguments as part of the service identifier i…
Browse files Browse the repository at this point in the history
…nside parsed constructor arguments
  • Loading branch information
wessberg committed May 21, 2021
1 parent c59150f commit 3a2f3ff
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
updateClassExpression,
} from "../../../util/ts-util";
import { VisitorContext } from "../../visitor-context";
import { pickServiceOrImplementationName } from "../util";

export function visitClassLikeDeclaration(
options: BeforeVisitorOptions<TS.ClassLikeDeclaration>
Expand Down Expand Up @@ -91,8 +92,8 @@ function getParameterTypeNamesAsArrayLiteral(
if (parameter.type == null) {
constructorParams[i] = compatFactory.createIdentifier("undefined");
} else {
constructorParams[i] = compatFactory.createStringLiteral(
parameter.type.getFullText().trim()
constructorParams[i] = compatFactory.createNoSubstitutionTemplateLiteral(
pickServiceOrImplementationName(parameter.type, context)
);
}
}
Expand Down
39 changes: 37 additions & 2 deletions test/constructor-arguments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ test(
constructor(foo) {
this.foo = foo;
}
static get [Symbol.for("___CTOR_ARGS___")]() { return ["IFoo"]; }
static get [Symbol.for("___CTOR_ARGS___")]() { return [\`IFoo\`]; }
}
`)
);
Expand Down Expand Up @@ -66,9 +66,44 @@ test(
this.foo = foo;
this.bar = bar;
}
static get [Symbol.for("___CTOR_ARGS___")]() { return ["IFoo", undefined]; }
static get [Symbol.for("___CTOR_ARGS___")]() { return [\`IFoo\`, undefined]; }
}
`)
);
}
);

test(
"When declaring service dependencies via constructor arguments, their type arguments should be irrelevant. #1",
withTypeScript,
(t, { typescript }) => {
const bundle = generateTransformerResult(
[
{
entry: true,
fileName: "index.ts",
text: `
interface IFoo<T> {}
class Foo {
constructor (private foo: IFoo<string>) {}
}
`,
},
],
{ typescript }
);
const [file] = bundle;

t.deepEqual(
formatCode(file.text),
formatCode(`\
class Foo {${gte(typescript.version, "4.3.0") ? `\n\t\tfoo;` : ""}
constructor(foo) {
this.foo = foo;
}
static get [Symbol.for("___CTOR_ARGS___")]() { return [\`IFoo\`]; }
}
`)
);
}
);

0 comments on commit 3a2f3ff

Please sign in to comment.