Skip to content
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

fix(jsii-diff): renaming a positional argument is a breaking change in Python #2937

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions packages/jsii-diff/test/python.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { expectError } from './util';

// ----------------------------------------------------------------------

test.each([
['class', 'constructor'],
['class', 'method'],
['interface', 'method'],
])(
'not okay to rename a positional parameter',
(scope, decl) =>
expectError(
/positional parameter was renamed from 'previous' to 'current'/,
// Note: name is ITest so we're good for both class & interface... Yes, this is ugly.
`
export ${scope} ITest {
${decl}(previous: any)${decl === 'constructor' ? '' : ': void'}${scope === 'class' ? ' { previous.use(); }' : ';'}
}`,
`
export ${scope} ITest {
${decl}(current: any)${decl === 'constructor' ? '' : ': void'}${scope === 'class' ? ' { current.use(); }' : ';'}
}`,
),
);
4 changes: 4 additions & 0 deletions packages/jsii/lib/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,10 @@ function _defaultValidations(): ValidationFunction[] {
),
);
}
// Standardize to the root method's argument signature, because in
// Python and Ruby, positional parameters can also be referred to by
// name, which makes their names be a part of the function's signature.
actParam.name = expParam.name;
}
}

Expand Down
107 changes: 107 additions & 0 deletions packages/jsii/test/compat.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { sourceToAssemblyHelper } from '../lib';

////////////////////////////////////////////////////////////////////////////////
// In Python and Ruby, positional parameters can be referred to by name, making
// them part of a function's signature. Always using the root implementation
// parameter names makes no difference in the undelying runtime, as JS
// positional arguments are... positional, but it reduces friction for Python
// and Ruby developers.
test('overriding methods use the overriden parameter names', async () => {
const assembly = await sourceToAssemblyHelper(`
export abstract class AbstractClass {
public abstract method(param: number): void;
}

export class ConcreteClass extends AbstractClass {
private constructor() { super(); }

public method(_arg: number): void {
// Nothing to do...
}
}
`);

expect(assembly.types!['testpkg.AbstractClass']).toEqual({
abstract: true,
assembly: 'testpkg',
fqn: 'testpkg.AbstractClass',
kind: 'class',
methods: [
{
abstract: true,
locationInModule: { filename: 'index.ts', line: 3 },
name: 'method',
parameters: [{ name: 'param', type: { primitive: 'number' } }],
},
],
initializer: {},
locationInModule: { filename: 'index.ts', line: 2 },
name: 'AbstractClass',
});

expect(assembly.types!['testpkg.ConcreteClass']).toEqual({
assembly: 'testpkg',
base: 'testpkg.AbstractClass',
fqn: 'testpkg.ConcreteClass',
kind: 'class',
methods: [
{
locationInModule: { filename: 'index.ts', line: 9 },
name: 'method',
overrides: 'testpkg.AbstractClass',
parameters: [{ name: 'param', type: { primitive: 'number' } }],
},
],
locationInModule: { filename: 'index.ts', line: 6 },
name: 'ConcreteClass',
});
});

test('implementing methods use the interface parameter names', async () => {
const assembly = await sourceToAssemblyHelper(`
export interface IInterface {
method(param: number): void;
}

export class ConcreteClass implements IInterface {
private constructor() {}

public method(_arg: number): void {
// Nothing to do...
}
}
`);

expect(assembly.types!['testpkg.IInterface']).toEqual({
assembly: 'testpkg',
fqn: 'testpkg.IInterface',
kind: 'interface',
methods: [
{
abstract: true,
locationInModule: { filename: 'index.ts', line: 3 },
name: 'method',
parameters: [{ name: 'param', type: { primitive: 'number' } }],
},
],
locationInModule: { filename: 'index.ts', line: 2 },
name: 'IInterface',
});

expect(assembly.types!['testpkg.ConcreteClass']).toEqual({
assembly: 'testpkg',
interfaces: ['testpkg.IInterface'],
fqn: 'testpkg.ConcreteClass',
kind: 'class',
methods: [
{
locationInModule: { filename: 'index.ts', line: 9 },
name: 'method',
overrides: 'testpkg.IInterface',
parameters: [{ name: 'param', type: { primitive: 'number' } }],
},
],
locationInModule: { filename: 'index.ts', line: 6 },
name: 'ConcreteClass',
});
});