Skip to content

Commit

Permalink
fix(deconflicting): add support for CallSignatures in deconflicting l…
Browse files Browse the repository at this point in the history
…ogic
  • Loading branch information
wessberg committed Apr 13, 2021
1 parent a3dcf2b commit 3699a4e
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
Expand Up @@ -36,6 +36,7 @@ import {preserveMeta} from "../../util/clone-node-with-meta";
import {deconflictFunctionTypeNode} from "./visitor/deconflict-function-type";
import {deconflictImportTypeNode} from "./visitor/deconflict-import-type-node";
import {deconflictConstructorDeclaration} from "./visitor/deconflict-constructor-declaration";
import {deconflictCallSignatureDeclaration} from "./visitor/deconflict-call-signature-declaration";

/**
* Deconflicts the given Node. Everything but LValues will be updated here
Expand Down Expand Up @@ -82,6 +83,8 @@ function deconflictNode({node, ...options}: DeconflicterVisitorOptions<TS.Node>)
return deconflictIndexSignatureDeclaration({node, ...options});
} else if (options.typescript.isMethodSignature(node)) {
return deconflictMethodSignature({node, ...options});
} else if (options.typescript.isCallSignatureDeclaration(node)) {
return deconflictCallSignatureDeclaration({node, ...options});
} else if (options.typescript.isModuleDeclaration(node)) {
return deconflictModuleDeclaration({node, ...options});
} else if (options.typescript.isNamespaceImport(node)) {
Expand Down
@@ -0,0 +1,36 @@
import {DeconflicterVisitorOptions} from "../deconflicter-visitor-options";
import {TS} from "../../../../../../type/ts";
import {cloneLexicalEnvironment} from "../../../util/clone-lexical-environment";
import {nodeArraysAreEqual} from "../../../util/node-arrays-are-equal";
import {ContinuationOptions} from "../deconflicter-options";
import {preserveMeta} from "../../../util/clone-node-with-meta";

/**
* Deconflicts the given CallSignature.
*/
export function deconflictCallSignatureDeclaration(options: DeconflicterVisitorOptions<TS.CallSignatureDeclaration>): TS.CallSignatureDeclaration | undefined {
const {node, continuation, lexicalEnvironment, compatFactory} = options;

// The type, type parameters, as well as the parameters share the same lexical environment
const nextContinuationOptions: ContinuationOptions = {lexicalEnvironment: cloneLexicalEnvironment(lexicalEnvironment)};

const typeParametersContResult =
node.typeParameters == null ? undefined : compatFactory.createNodeArray(node.typeParameters.map(typeParameter => continuation(typeParameter, nextContinuationOptions)));
const parametersContResult = compatFactory.createNodeArray(node.parameters.map(parameter => continuation(parameter, nextContinuationOptions)));
const typeContResult = node.type == null ? undefined : continuation(node.type, nextContinuationOptions);

const isIdentical =
nodeArraysAreEqual(typeParametersContResult, node.typeParameters) &&
nodeArraysAreEqual(parametersContResult, node.parameters) &&
typeContResult === node.type;

if (isIdentical) {
return node;
}

return preserveMeta(
compatFactory.updateCallSignature(node, typeParametersContResult, parametersContResult, typeContResult),
node,
options
);
}
68 changes: 68 additions & 0 deletions test/deconflict.test.ts
Expand Up @@ -1173,6 +1173,74 @@ export { HelloWorld, GoodbyeWorld };`)
);
});

test("Deconflicts symbols. #22", withTypeScript, async (t, {typescript}) => {
const bundle = await generateRollupBundle(
[
{
entry: true,
fileName: "virtual-src/index.ts",
text: `\
export { default as foo } from "./foo";
export { default as bar } from "./bar";
`
},
{
entry: false,
fileName: "virtual-src/foo.ts",
text: `\
interface Foo {
<T, R>(value: T, fn: (value: T) => R): R;
}
const foo: Foo = (value: any, fn: any) => {
return fn(value)
}
export default foo;
`
},
{
entry: false,
fileName: "virtual-src/bar.ts",
text: `\
interface Bar {
<T, R>(value: T, fn: (value: T) => R): R;
}
const bar: Bar = (value: any, fn: any) => {
return fn(value)
}
export default bar;
`
}
],
{
typescript,
debug: false
}
);
const {
declarations: [file]
} = bundle;

t.deepEqual(
formatCode(file.code),
formatCode(`\
interface Foo {
<T, R>(value: T, fn: (value: T) => R): R;
}
declare const foo: Foo;
interface Bar {
<T, R>(value: T, fn: (value: T) => R): R;
}
declare const bar: Bar;
export { foo, bar };
`)
);
});

test("Will merge declarations declared in same SourceFile rather than deconflict. #1", withTypeScript, async (t, {typescript}) => {
const bundle = await generateRollupBundle(
[
Expand Down

0 comments on commit 3699a4e

Please sign in to comment.