forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[eslint] prevent using constructor property params in initializers (e…
- Loading branch information
1 parent
f8a036c
commit 584b7a1
Showing
31 changed files
with
430 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
packages/kbn-eslint-plugin-eslint/rules/no_constructor_args_in_property_initializers.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
const tsEstree = require('@typescript-eslint/typescript-estree'); | ||
const traverse = require('eslint-traverse'); | ||
const esTypes = tsEstree.AST_NODE_TYPES; | ||
|
||
/** @typedef {import("eslint").Rule.RuleModule} Rule */ | ||
/** @typedef {import("@typescript-eslint/typescript-estree").TSESTree.Node} Node */ | ||
/** @typedef {import("@typescript-eslint/typescript-estree").TSESTree.ClassBody} ClassBody */ | ||
/** @typedef {import("@typescript-eslint/typescript-estree").TSESTree.Parameter} Parameter */ | ||
/** @typedef {import("@typescript-eslint/typescript-estree").TSESTree.TSParameterProperty} TSParameterProperty */ | ||
|
||
/** | ||
* @param {Parameter} param | ||
* @returns {param is TSParameterProperty} | ||
*/ | ||
function isTsParameterProperty(param) { | ||
return param.type === esTypes.TSParameterProperty; | ||
} | ||
|
||
/** | ||
* @param {string} arg | ||
*/ | ||
const errorMsg = (arg) => | ||
`The constructor argument "${arg}" can't be used in a class property intializer, define the property in the constructor instead`; | ||
|
||
/** @type {Rule} */ | ||
module.exports = { | ||
meta: { | ||
schema: [], | ||
}, | ||
create: (context) => ({ | ||
ClassBody(_) { | ||
const node = /** @type {ClassBody} */ (_); | ||
|
||
const constructor = node.body.find( | ||
(n) => n.type === esTypes.MethodDefinition && n.kind === 'constructor' | ||
); | ||
|
||
if (!constructor || constructor.type !== esTypes.MethodDefinition) { | ||
return; | ||
} | ||
|
||
const constructorArgProps = constructor.value.params | ||
.filter(isTsParameterProperty) | ||
.map((p) => { | ||
if (p.parameter.type === esTypes.Identifier) { | ||
return p.parameter.name; | ||
} | ||
|
||
if ( | ||
p.parameter.type === esTypes.AssignmentPattern && | ||
p.parameter.left.type === esTypes.Identifier | ||
) { | ||
return p.parameter.left.name; | ||
} | ||
}); | ||
|
||
if (!constructorArgProps.length) { | ||
return; | ||
} | ||
|
||
for (const prop of node.body) { | ||
if (prop.type !== esTypes.PropertyDefinition) { | ||
continue; | ||
} | ||
|
||
const visitor = (path) => { | ||
/** @type {Node} node */ | ||
const node = path.node; | ||
|
||
if ( | ||
node.type === esTypes.FunctionExpression || | ||
node.type === esTypes.ArrowFunctionExpression | ||
) { | ||
return traverse.STOP; | ||
} | ||
|
||
if ( | ||
node.type === esTypes.MemberExpression && | ||
node.object.type === esTypes.ThisExpression && | ||
node.property.type === esTypes.Identifier && | ||
node.property.name && | ||
constructorArgProps.includes(node.property.name) | ||
) { | ||
context.report({ | ||
message: errorMsg(node.property.name), | ||
loc: node.property.loc, | ||
}); | ||
} | ||
}; | ||
|
||
traverse(context, prop, visitor); | ||
} | ||
}, | ||
}), | ||
}; |
100 changes: 100 additions & 0 deletions
100
packages/kbn-eslint-plugin-eslint/rules/no_constructor_args_in_property_initializers.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
const { RuleTester } = require('eslint'); | ||
const rule = require('./no_constructor_args_in_property_initializers'); | ||
const dedent = require('dedent'); | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: require.resolve('@typescript-eslint/parser'), | ||
parserOptions: { | ||
sourceType: 'module', | ||
ecmaVersion: 2018, | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
}, | ||
}); | ||
|
||
ruleTester.run('@kbn/eslint/no_constructor_args_in_property_initializers', rule, { | ||
valid: [ | ||
{ | ||
code: dedent` | ||
class Foo { | ||
bar = 'baz' | ||
} | ||
`, | ||
}, | ||
{ | ||
code: dedent` | ||
class Foo { | ||
bar = 'baz' | ||
constructor(private readonly foo: Box) {} | ||
} | ||
`, | ||
}, | ||
{ | ||
code: dedent` | ||
class Foo { | ||
bar = 'baz' | ||
constructor(private readonly foo: () => void) {} | ||
get = () => { | ||
return this.foo() | ||
} | ||
} | ||
`, | ||
}, | ||
], | ||
|
||
invalid: [ | ||
// no catch | ||
{ | ||
code: dedent` | ||
class Foo { | ||
bar = this.foo.split().reverse() | ||
constructor(private readonly foo: string) {} | ||
} | ||
`, | ||
errors: [ | ||
{ | ||
line: 2, | ||
message: `The constructor argument "foo" can't be used in a class property intializer, define the property in the constructor instead`, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: dedent` | ||
class Foo { | ||
bar = this.foo() | ||
constructor(private readonly foo: () => void) {} | ||
} | ||
`, | ||
errors: [ | ||
{ | ||
line: 2, | ||
message: `The constructor argument "foo" can't be used in a class property intializer, define the property in the constructor instead`, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: dedent` | ||
class Foo { | ||
bar = this.foo() | ||
constructor(private readonly foo: (() => void) = defaultValue) {} | ||
} | ||
`, | ||
errors: [ | ||
{ | ||
line: 2, | ||
message: `The constructor argument "foo" can't be used in a class property intializer, define the property in the constructor instead`, | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.