Skip to content

Commit

Permalink
fix: avoid calling typeParameters getter (#2327)
Browse files Browse the repository at this point in the history
  • Loading branch information
KaelWD authored Nov 29, 2023
1 parent 4db54a7 commit e008d34
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 16 deletions.
3 changes: 2 additions & 1 deletion lib/rules/define-emits-declaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ module.exports = {
}

case 'runtime': {
const typeArguments = node.typeArguments || node.typeParameters
const typeArguments =
'typeArguments' in node ? node.typeArguments : node.typeParameters
if (typeArguments && typeArguments.params.length > 0) {
context.report({
node,
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/define-props-declaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ module.exports = {
}

case 'runtime': {
const typeArguments = node.typeArguments || node.typeParameters
const typeArguments =
'typeArguments' in node ? node.typeArguments : node.typeParameters
if (typeArguments && typeArguments.params.length > 0) {
context.report({
node,
Expand Down
4 changes: 3 additions & 1 deletion lib/rules/require-typed-ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ module.exports = {
}

const typeArguments =
ref.node.typeArguments || ref.node.typeParameters
'typeArguments' in ref.node
? ref.node.typeArguments
: ref.node.typeParameters
if (typeArguments == null) {
if (
ref.node.parent.type === 'VariableDeclarator' &&
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/valid-define-emits.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ module.exports = {
onDefineEmitsEnter(node) {
defineEmitsNodes.push(node)

const typeArguments = node.typeArguments || node.typeParameters
const typeArguments =
'typeArguments' in node ? node.typeArguments : node.typeParameters
if (node.arguments.length > 0) {
if (typeArguments && typeArguments.params.length > 0) {
// `defineEmits` has both a literal type and an argument.
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/valid-define-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ module.exports = {
})
}

const typeArguments = node.typeArguments || node.typeParameters
const typeArguments =
'typeArguments' in node ? node.typeArguments : node.typeParameters
if (typeArguments) {
context.report({
node: typeArguments,
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/valid-define-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ module.exports = {
onDefinePropsEnter(node) {
definePropsNodes.push(node)

const typeArguments = node.typeArguments || node.typeParameters
const typeArguments =
'typeArguments' in node ? node.typeArguments : node.typeParameters
if (node.arguments.length > 0) {
if (typeArguments && typeArguments.params.length > 0) {
// `defineProps` has both a literal type and an argument.
Expand Down
6 changes: 4 additions & 2 deletions lib/utils/indent-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,8 @@ module.exports.defineVisitor = function create(
},
/** @param {CallExpression} node */
CallExpression(node) {
const typeArguments = node.typeArguments || node.typeParameters
const typeArguments =
'typeArguments' in node ? node.typeArguments : node.typeParameters
const firstToken = tokenStore.getFirstToken(node)
const rightToken = tokenStore.getLastToken(node)
const leftToken = /** @type {Token} */ (
Expand Down Expand Up @@ -1695,7 +1696,8 @@ module.exports.defineVisitor = function create(
},
/** @param {NewExpression} node */
NewExpression(node) {
const typeArguments = node.typeArguments || node.typeParameters
const typeArguments =
'typeArguments' in node ? node.typeArguments : node.typeParameters
const newToken = tokenStore.getFirstToken(node)
const calleeToken = tokenStore.getTokenAfter(newToken)
const rightToken = tokenStore.getLastToken(node)
Expand Down
10 changes: 7 additions & 3 deletions lib/utils/indent-ts.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,10 @@ function defineVisitor({
* @param {TSTypeReference | TSInstantiationExpression} node
*/
'TSTypeReference, TSInstantiationExpression'(node) {
const typeArguments = node.typeArguments || node.typeParameters
const typeArguments =
'typeArguments' in node
? node.typeArguments
: /** @type {any} typescript-eslint v5 */ (node).typeParameters
if (typeArguments) {
const firstToken = tokenStore.getFirstToken(node)
setOffset(tokenStore.getFirstToken(typeArguments), 1, firstToken)
Expand Down Expand Up @@ -1183,8 +1186,9 @@ function defineVisitor({
setOffset([dotToken, propertyToken], 1, firstToken)
}
const typeArguments =
node.typeArguments ||
/** @type {any} typescript-eslint v5 */ (node).typeParameters
'typeArguments' in node
? node.typeArguments
: /** @type {any} typescript-eslint v5 */ (node).typeParameters
if (typeArguments) {
setOffset(tokenStore.getFirstToken(typeArguments), 1, firstToken)
}
Expand Down
6 changes: 4 additions & 2 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3000,7 +3000,8 @@ function getComponentPropsFromDefineProps(context, node) {
}
]
}
const typeArguments = node.typeArguments || node.typeParameters
const typeArguments =
'typeArguments' in node ? node.typeArguments : node.typeParameters
if (typeArguments && typeArguments.params.length > 0) {
return getComponentPropsFromTypeDefine(context, typeArguments.params[0])
}
Expand Down Expand Up @@ -3033,7 +3034,8 @@ function getComponentEmitsFromDefineEmits(context, node) {
}
]
}
const typeArguments = node.typeArguments || node.typeParameters
const typeArguments =
'typeArguments' in node ? node.typeArguments : node.typeParameters
if (typeArguments && typeArguments.params.length > 0) {
return getComponentEmitsFromTypeDefine(context, typeArguments.params[0])
}
Expand Down
15 changes: 12 additions & 3 deletions lib/utils/ts-utils/ts-ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,10 @@ function inferRuntimeType(context, node, checked = new Set()) {
return ['Array']
}
case 'NonNullable': {
const typeArguments = node.typeArguments || node.typeParameters
const typeArguments =
'typeArguments' in node
? node.typeArguments
: /** @type {any} typescript-eslint v5 */ (node).typeParameters
if (typeArguments && typeArguments.params[0]) {
return inferRuntimeType(
context,
Expand All @@ -441,15 +444,21 @@ function inferRuntimeType(context, node, checked = new Set()) {
break
}
case 'Extract': {
const typeArguments = node.typeArguments || node.typeParameters
const typeArguments =
'typeArguments' in node
? node.typeArguments
: /** @type {any} typescript-eslint v5 */ (node).typeParameters
if (typeArguments && typeArguments.params[1]) {
return inferRuntimeType(context, typeArguments.params[1], checked)
}
break
}
case 'Exclude':
case 'OmitThisParameter': {
const typeArguments = node.typeArguments || node.typeParameters
const typeArguments =
'typeArguments' in node
? node.typeArguments
: /** @type {any} typescript-eslint v5 */ (node).typeParameters
if (typeArguments && typeArguments.params[0]) {
return inferRuntimeType(context, typeArguments.params[0], checked)
}
Expand Down

0 comments on commit e008d34

Please sign in to comment.