Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix(typescript-estree): disallow binding patterns in parameter properties#11760

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
14 commits
Select commitHold shift + click to select a range
a6aebe2
disallow binding patterns in parameter properties
dbarabashhNov 14, 2025
f288225
fixes
dbarabashhNov 14, 2025
5c6e3df
parameter left is always Indetifier
dbarabashhNov 14, 2025
e9ef2e9
add type assertions
dbarabashhNov 14, 2025
547b55f
add getParameterPropertyIdentifier
dbarabashhNov 14, 2025
511efef
fix
dbarabashhNov 14, 2025
a9f77a1
add test
dbarabashhNov 14, 2025
647b65d
linting
dbarabashhNov 14, 2025
6b175b7
fixes
dbarabashhNov 17, 2025
d824304
Merge branch 'main' into fix-invalid-parameter-properties
dbarabashhNov 17, 2025
529ae16
fix
dbarabashhNov 17, 2025
c2ee584
chore: remove SKIP_AST_SPEC_REBUILD env var in CI
JamesHenryNov 19, 2025
348e7d7
chore: types implicitly depends on ast-spec
JamesHenryNov 19, 2025
ed93f1c
Merge branch 'main' into fix-invalid-parameter-properties
JoshuaKGoldbergNov 24, 2025
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
classTest{constructor(private[foo]=[1]){}}

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
classTest{constructor(private{ bar}={bar:1}){}}

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
classTest{constructor(private ...baz){}}

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
importtype{AST_NODE_TYPES}from'../../ast-node-types';
importtype{Accessibility}from'../../base/Accessibility';
importtype{BaseNode}from'../../base/BaseNode';
importtype{Identifier}from'../../expression/Identifier/spec';
importtype{Decorator}from'../../special/Decorator/spec';
importtype{BindingName}from'../../unions/BindingName';
importtype{AssignmentPattern}from'../AssignmentPattern/spec';
importtype{RestElement}from'../RestElement/spec';

typeParameterPropertyParameter=
|(AssignmentPattern&{left:Identifier})
|Identifier;

exportinterfaceTSParameterPropertyextendsBaseNode{
type:AST_NODE_TYPES.TSParameterProperty;
accessibility:Accessibility|undefined;
decorators:Decorator[];
override:boolean;
parameter:AssignmentPattern|BindingName|RestElement;
parameter:ParameterPropertyParameter;
readonly:boolean;
static:boolean;
}
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -359,19 +359,10 @@ export default createRule<Options, MessageIds>({
node:TSESTree.TSParameterProperty,
):void{
constnodeType='parameter property';
// HAS to be an identifier or assignment or TSC will throw
if(
node.parameter.type!==AST_NODE_TYPES.Identifier&&
node.parameter.type!==AST_NODE_TYPES.AssignmentPattern
){
return;
}

constnodeName=
node.parameter.type===AST_NODE_TYPES.Identifier
?node.parameter.name
:// has to be an Identifier or TSC will throw an error
(node.parameter.leftasTSESTree.Identifier).name;
:(node.parameter.leftasTSESTree.Identifier).name;

switch(paramPropCheck){
case'explicit':{
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -109,8 +109,7 @@ export default createRule({
((node.parameter.type===AST_NODE_TYPES.Identifier&&// constructor (public foo) {}
node.parameter.name===name)||
(node.parameter.type===AST_NODE_TYPES.AssignmentPattern&&// constructor (public foo = 1) {}
node.parameter.left.type===AST_NODE_TYPES.Identifier&&
node.parameter.left.name===name))
(node.parameter.leftasTSESTree.Identifier).name===name))
);
}

Expand Down
11 changes: 1 addition & 10 deletionspackages/eslint-plugin/src/rules/parameter-properties.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -107,19 +107,10 @@ export default createRule<Options, MessageIds>({
constmodifiers=getModifiers(node);

if(!allow.includes(modifiers)){
// HAS to be an identifier or assignment or TSC will throw
if(
node.parameter.type!==AST_NODE_TYPES.Identifier&&
node.parameter.type!==AST_NODE_TYPES.AssignmentPattern
){
return;
}

constname=
node.parameter.type===AST_NODE_TYPES.Identifier
?node.parameter.name
:// has to be an Identifier or TSC will throw an error
(node.parameter.leftasTSESTree.Identifier).name;
:(node.parameter.leftasTSESTree.Identifier).name;

context.report({
node,
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -59,22 +59,11 @@ function extractNonComputedName(
*/
exportfunctionextractNameForMember(node:MemberNode):ExtractedName|null{
if(node.type===AST_NODE_TYPES.TSParameterProperty){
switch(node.parameter.type){
caseAST_NODE_TYPES.ArrayPattern:
caseAST_NODE_TYPES.ObjectPattern:
caseAST_NODE_TYPES.RestElement:
// Nonsensical properties -- see https://github.com/typescript-eslint/typescript-eslint/issues/11708
returnnull;

caseAST_NODE_TYPES.AssignmentPattern:
if(node.parameter.left.type!==AST_NODE_TYPES.Identifier){
returnnull;
}
returnextractNonComputedName(node.parameter.left);

caseAST_NODE_TYPES.Identifier:
returnextractNonComputedName(node.parameter);
}
constidentifier=
node.parameter.type===AST_NODE_TYPES.Identifier
?node.parameter
:(node.parameter.leftasTSESTree.Identifier);
returnextractNonComputedName(identifier);
}

if(node.computed){
Expand Down
10 changes: 3 additions & 7 deletionspackages/eslint-plugin/src/util/collectUnusedVariables.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -141,22 +141,18 @@ class UnusedVarsVisitor extends Visitor {
}

protectedTSParameterProperty(node:TSESTree.TSParameterProperty):void{
letidentifier:TSESTree.Identifier|null=null;
letidentifier:TSESTree.Identifier;
switch(node.parameter.type){
caseAST_NODE_TYPES.AssignmentPattern:
if(node.parameter.left.type===AST_NODE_TYPES.Identifier){
identifier=node.parameter.left;
}
identifier=node.parameter.leftasTSESTree.Identifier;
break;

caseAST_NODE_TYPES.Identifier:
identifier=node.parameter;
break;
}

if(identifier){
this.markVariableAsUsed(identifier);
}
this.markVariableAsUsed(identifier);
}

privatecollectUnusedVariables(
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -302,14 +302,6 @@ class Test {
},
{
code:`
class Test {
constructor(private { x }: any[]) {}
}
`,
options:[{accessibility:'no-public'}],
},
{
code:`
class Test {
#foo = 1;
#bar() {}
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1382,27 +1382,16 @@ export class Test {
{
code:`
export class Test {
constructor(
public foo,
private ...bar,
) {}
constructor(public foo) {}
}
`,
errors:[
{
column:12,
column:22,
data:{
name:'foo',
},
line:4,
messageId:'missingArgType',
},
{
column:5,
data:{
name:'bar',
},
line:5,
line:3,
messageId:'missingArgType',
},
],
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -113,18 +113,6 @@ class Foo {
`,
options:[{allow:['public readonly','private']}],
},
// Semantically invalid test case
`
class Foo {
constructor(private ...name: string[]) {}
}
`,
// Semantically invalid test case
`
class Foo {
constructor(private [test]: [string]) {}
}
`,
{
code:`
class Foo {
Expand Down
18 changes: 18 additions & 0 deletionspackages/typescript-estree/src/check-modifiers.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -370,6 +370,24 @@ export function checkModifiers(node: ts.Node): void {
'A parameter property is only allowed in a constructor implementation.',
);
}
constparam=nodeasts.ParameterDeclaration;

if(param.dotDotDotToken){
throwError(
modifier,
'A parameter property cannot be a rest parameter.',
);
}

if(
param.name.kind===SyntaxKind.ArrayBindingPattern||
param.name.kind===SyntaxKind.ObjectBindingPattern
){
throwError(
modifier,
'A parameter property may not be declared using a binding pattern.',
);
}
}

// There are more cases in `checkGrammarObjectLiteralExpression` in TypeScript.
Expand Down
2 changes: 1 addition & 1 deletionpackages/typescript-estree/src/convert.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1823,7 +1823,7 @@ export class Converter {
accessibility: getTSNodeAccessibility(node),
decorators: [],
override: hasModifier(SyntaxKind.OverrideKeyword, node),
parameter: result,
parameter: result as TSESTree.TSParameterProperty['parameter'],
readonly: hasModifier(SyntaxKind.ReadonlyKeyword, node),
static: hasModifier(SyntaxKind.StaticKeyword, node),
});
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp