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

Rule proposal: call-arguments-length #1356

Open
fisker opened this issue Jun 15, 2021 · 4 comments
Open

Rule proposal: call-arguments-length #1356

fisker opened this issue Jun 15, 2021 · 4 comments

Comments

@fisker
Copy link
Collaborator

fisker commented Jun 15, 2021

A generic rule to enforce correct call arguments length.

For example CanvasRenderingContext2D.drawImage() can config as {'*.drawImage': [3, 5, 9]}, so it must be called with 3 or 5 or 9 arguments.

// Fail
context.drawImage(image, dx);
context.drawImage(image, dx, dy, dWidth, );

// Pass
context.drawImage(image, dx, dy);

This rule accepts a configurable list, for each method of function, value can be a number or a range, or allowed lengths.

{
	rules: {
		'unicorn/call-arguments-length':[
			'error',
			{
				'parseInt': 2,
				'*.drawImage': [3, 5, 9],
				'*.addEventListener': {min: 2}
			}
		]
	}
}

Ref: #974 (comment)

@sindresorhus
Copy link
Owner

I think we should ship with a default preset for this rule that covers a lot of built-ins.

@sindresorhus
Copy link
Owner

This is now accepted.

@zanminkian
Copy link
Contributor

zanminkian commented Sep 17, 2024

This rule should support new expression.

{
  rules: {
    'unicorn/call-arguments-length': ['error', {
      'parseInt': 2,
      '*.drawImage': [3, 5, 9],
      '*.addEventListener': {min: 2},
      'new Set': {max: 1}
    }]
  }
}

One edge case: spreading operator like new Set(...args) should be regarded as 1 argument?

@zanminkian
Copy link
Contributor

I think spreading parameters should be regarded as error if the function has specified the arguments length

// config
{
  rules: {
    'unicorn/call-arguments-length': ['error', {
      'new Set': {max: 1}
    }]
  }
}

// code
new Set(); // pass
new Set(foo); // pass
new Set(...foo); // fail

So that, the issue #2412 can be solved by this rule.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants