Skip to content

Commit 274d3fa

Browse files
chore(lint): enable @typescript-eslint/no-unnecessary-condition (#5630)
Co-authored-by: KazariEX <[email protected]>
1 parent ab1ed6e commit 274d3fa

36 files changed

+179
-204
lines changed

extensions/vscode/lib/welcome.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ let panel: vscode.WebviewPanel | undefined;
66

77
export function activate(context: vscode.ExtensionContext) {
88
if (
9-
context.globalState.get('vue.showUpdates', true)
9+
context.globalState.get<boolean>('vue.showUpdates', true)
1010
&& context.globalState.get('vue-welcome') !== welcomeVersion
1111
) {
1212
context.globalState.update('vue-welcome', welcomeVersion);
@@ -331,7 +331,7 @@ function getWelcomeHtml(context: vscode.ExtensionContext) {
331331
<div style="display: flex; justify-content: center; margin: 1.5rem 0;">
332332
<label>
333333
<input type="checkbox" onchange="toggleShowUpdates(this.checked)" ${
334-
context.globalState.get('vue.showUpdates', true) ? 'checked' : ''
334+
context.globalState.get<boolean>('vue.showUpdates', true) ? 'checked' : ''
335335
}>
336336
<span>Show release notes on update</span>
337337
</label>

packages/component-meta/lib/base.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ interface ComponentMeta<T> {
269269
}
270270

271271
const componentType = typeChecker.getTypeOfSymbolAtLocation(_export, symbolNode);
272-
const symbolProperties = componentType.getProperties() ?? [];
272+
const symbolProperties = componentType.getProperties();
273273

274274
let _type: ReturnType<typeof getType> | undefined;
275275
let _props: ReturnType<typeof getProps> | undefined;
@@ -451,7 +451,7 @@ interface ComponentMeta<T> {
451451
typeChecker: ts.TypeChecker,
452452
componentPath: string,
453453
) {
454-
const sourceFile = program?.getSourceFile(getMetaFileName(componentPath));
454+
const sourceFile = program.getSourceFile(getMetaFileName(componentPath));
455455
if (!sourceFile) {
456456
throw 'Could not find main source file';
457457
}
@@ -530,8 +530,8 @@ function createSchemaResolvers(
530530

531531
function resolveNestedProperties(prop: ts.Symbol): PropertyMeta {
532532
const subtype = typeChecker.getTypeOfSymbolAtLocation(prop, symbolNode);
533-
let schema: PropertyMetaSchema;
534-
let declarations: Declaration[];
533+
let schema: PropertyMetaSchema | undefined;
534+
let declarations: Declaration[] | undefined;
535535

536536
return {
537537
name: prop.getEscapedName().toString(),
@@ -557,8 +557,8 @@ function createSchemaResolvers(
557557
const signatures = propType.getCallSignatures();
558558
const paramType = signatures[0]?.parameters[0];
559559
const subtype = paramType ? typeChecker.getTypeOfSymbolAtLocation(paramType, symbolNode) : typeChecker.getAnyType();
560-
let schema: PropertyMetaSchema;
561-
let declarations: Declaration[];
560+
let schema: PropertyMetaSchema | undefined;
561+
let declarations: Declaration[] | undefined;
562562

563563
return {
564564
name: prop.getName(),
@@ -575,8 +575,8 @@ function createSchemaResolvers(
575575
}
576576
function resolveExposedProperties(expose: ts.Symbol): ExposeMeta {
577577
const subtype = typeChecker.getTypeOfSymbolAtLocation(expose, symbolNode);
578-
let schema: PropertyMetaSchema;
579-
let declarations: Declaration[];
578+
let schema: PropertyMetaSchema | undefined;
579+
let declarations: Declaration[] | undefined;
580580

581581
return {
582582
name: expose.getName(),
@@ -592,8 +592,8 @@ function createSchemaResolvers(
592592
};
593593
}
594594
function resolveEventSignature(call: ts.Signature): EventMeta {
595-
let schema: PropertyMetaSchema[];
596-
let declarations: Declaration[];
595+
let schema: PropertyMetaSchema[] | undefined;
596+
let declarations: Declaration[] | undefined;
597597
let subtype = undefined;
598598
let subtypeStr = '[]';
599599
let getSchema = () => [] as PropertyMetaSchema[];
@@ -666,7 +666,7 @@ function createSchemaResolvers(
666666
visited.add(subtype);
667667

668668
if (subtype.isUnion()) {
669-
let schema: PropertyMetaSchema[];
669+
let schema: PropertyMetaSchema[] | undefined;
670670
return {
671671
kind: 'enum',
672672
type,
@@ -676,7 +676,7 @@ function createSchemaResolvers(
676676
};
677677
}
678678
else if (typeChecker.isArrayLikeType(subtype)) {
679-
let schema: PropertyMetaSchema[];
679+
let schema: PropertyMetaSchema[] | undefined;
680680
return {
681681
kind: 'array',
682682
type,
@@ -690,7 +690,7 @@ function createSchemaResolvers(
690690
&& (subtype.isClassOrInterface() || subtype.isIntersection()
691691
|| (subtype as ts.ObjectType).objectFlags & ts.ObjectFlags.Anonymous)
692692
) {
693-
let schema: Record<string, PropertyMeta>;
693+
let schema: Record<string, PropertyMeta> | undefined;
694694
return {
695695
kind: 'object',
696696
type,
@@ -939,7 +939,7 @@ function resolvePropsOption(
939939

940940
for (const prop of props.properties) {
941941
if (ts.isPropertyAssignment(prop)) {
942-
const name = prop.name?.getText(ast);
942+
const name = prop.name.getText(ast);
943943
if (ts.isObjectLiteralExpression(prop.initializer)) {
944944
const defaultProp = prop.initializer.properties.find(p =>
945945
ts.isPropertyAssignment(p) && p.name.getText(ast) === 'default'

packages/component-meta/tests/index.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,12 +1201,12 @@ const worker = (checker: ComponentMetaChecker, withTsconfig: boolean) =>
12011201

12021202
const a = meta.props.find(prop =>
12031203
prop.name === 'foo'
1204-
&& prop.required === true
1204+
&& prop.required
12051205
&& prop.type === 'string'
12061206
);
12071207
const b = meta.props.find(prop =>
12081208
prop.name === 'bar'
1209-
&& prop.required === false
1209+
&& !prop.required
12101210
&& prop.type === 'number | undefined'
12111211
);
12121212

@@ -1225,12 +1225,12 @@ const worker = (checker: ComponentMetaChecker, withTsconfig: boolean) =>
12251225

12261226
const a = meta.props.find(prop =>
12271227
prop.name === 'foo'
1228-
&& prop.required === true
1228+
&& prop.required
12291229
&& prop.type === 'string'
12301230
);
12311231
const b = meta.props.find(prop =>
12321232
prop.name === 'bar'
1233-
&& prop.required === false
1233+
&& !prop.required
12341234
&& prop.type === 'number | undefined'
12351235
);
12361236

@@ -1255,12 +1255,12 @@ const worker = (checker: ComponentMetaChecker, withTsconfig: boolean) =>
12551255

12561256
const a = Foo.props.find(prop =>
12571257
prop.name === 'foo'
1258-
&& prop.required === true
1258+
&& prop.required
12591259
&& prop.type === 'string'
12601260
);
12611261
const b = Bar.props.find(prop =>
12621262
prop.name === 'bar'
1263-
&& prop.required === false
1263+
&& !prop.required
12641264
&& prop.type === 'number | undefined'
12651265
);
12661266

packages/language-core/lib/codegen/script/componentSelf.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function* generateComponentSelf(
3838
yield `${varName},${newLine}`;
3939
}
4040
yield `}),${newLine}`;
41-
if (options.sfc.scriptSetup && options.scriptSetupRanges && !ctx.bypassDefineComponent) {
41+
if (!ctx.bypassDefineComponent) {
4242
const emitOptionCodes = [...generateEmitsOption(options, options.scriptSetupRanges)];
4343
yield* emitOptionCodes;
4444
yield* generatePropsOption(

packages/language-core/lib/codegen/script/index.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,7 @@ function* generateScript(
8080
blockName: options.sfc.script.name,
8181
offset: exportDefault.expression.start,
8282
setting: 'vue.inlayHints.optionsWrapper',
83-
label: options.vueCompilerOptions.optionsWrapper.length
84-
? options.vueCompilerOptions.optionsWrapper[0]
85-
: '[Missing optionsWrapper[0]]',
83+
label: options.vueCompilerOptions.optionsWrapper[0],
8684
tooltip: [
8785
'This is virtual code that is automatically wrapped for type support, it does not affect your runtime behavior, you can customize it via `vueCompilerOptions.optionsWrapper` option in tsconfig / jsconfig.',
8886
'To hide it, you can set `"vue.inlayHints.optionsWrapper": false` in IDE settings.',
@@ -91,9 +89,7 @@ function* generateScript(
9189
blockName: options.sfc.script.name,
9290
offset: exportDefault.expression.end,
9391
setting: 'vue.inlayHints.optionsWrapper',
94-
label: options.vueCompilerOptions.optionsWrapper.length >= 2
95-
? options.vueCompilerOptions.optionsWrapper[1]
96-
: '[Missing optionsWrapper[1]]',
92+
label: options.vueCompilerOptions.optionsWrapper[1],
9793
});
9894
yield generateSfcBlockSection(options.sfc.script, 0, exportDefault.expression.start, codeFeatures.all);
9995
yield options.vueCompilerOptions.optionsWrapper[0];

packages/language-core/lib/codegen/script/scriptSetup.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ function* generateSetupFunction(
145145
[
146146
`let __VLS_exposed!: `,
147147
generateSfcBlockSection(scriptSetup, typeArg.start, typeArg.end, codeFeatures.all),
148-
`${endOfLine}`,
148+
endOfLine,
149149
],
150150
callExp.start,
151151
callExp.start,
@@ -160,7 +160,7 @@ function* generateSetupFunction(
160160
[
161161
`const __VLS_exposed = `,
162162
generateSfcBlockSection(scriptSetup, arg.start, arg.end, codeFeatures.all),
163-
`${endOfLine}`,
163+
endOfLine,
164164
],
165165
callExp.start,
166166
callExp.start,

packages/language-core/lib/codegen/template/element.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ export function* generateComponent(
153153
: codeFeatures.doNotReportTs2339AndTs2551,
154154
},
155155
);
156-
yield `${endOfLine}`;
156+
yield endOfLine;
157157

158158
const camelizedTag = camelize(node.tag);
159159
if (identifierRegex.test(camelizedTag)) {
@@ -186,7 +186,7 @@ export function* generateComponent(
186186
},
187187
},
188188
);
189-
yield `${endOfLine}`;
189+
yield endOfLine;
190190
}
191191
}
192192

@@ -255,7 +255,7 @@ export function* generateComponent(
255255
if (ctx.inVFor) {
256256
yield `[]`;
257257
}
258-
yield `${endOfLine}`;
258+
yield endOfLine;
259259

260260
if (refName && offset) {
261261
ctx.addTemplateRef(refName, `typeof ${ctx.getHoistVariable(componentInstanceVar)}`, offset);

packages/language-core/lib/codegen/template/elementProps.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,7 @@ export function* generateElementProps(
236236
yield `,${newLine}`;
237237
}
238238
else if (
239-
prop.type === CompilerDOM.NodeTypes.DIRECTIVE
240-
&& prop.name === 'bind'
239+
prop.name === 'bind'
241240
&& !prop.arg
242241
&& prop.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION
243242
) {
@@ -301,8 +300,8 @@ export function* generatePropExp(
301300
const propVariableName = camelize(exp.loc.source);
302301

303302
if (identifierRegex.test(propVariableName)) {
304-
const isDestructuredProp = options.destructuredPropNames?.has(propVariableName) ?? false;
305-
const isTemplateRef = options.templateRefNames?.has(propVariableName) ?? false;
303+
const isDestructuredProp = options.destructuredPropNames.has(propVariableName);
304+
const isTemplateRef = options.templateRefNames.has(propVariableName);
306305

307306
const codes = generateCamelized(
308307
exp.loc.source,
@@ -366,7 +365,7 @@ function getShouldCamelize(
366365
return (
367366
prop.type !== CompilerDOM.NodeTypes.DIRECTIVE
368367
|| !prop.arg
369-
|| (prop.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION && prop.arg.isStatic)
368+
|| (prop.arg.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION && prop.arg.isStatic)
370369
)
371370
&& hyphenateAttr(propName) === propName
372371
&& !options.vueCompilerOptions.htmlAttributes.some(pattern => isMatch(propName, pattern));

packages/language-core/lib/codegen/template/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ function* generateSlots(
122122
}
123123
yield `?: (props: typeof ${slot.propsVar}) => any }`;
124124
}
125-
yield `${endOfLine}`;
125+
yield endOfLine;
126126
}
127127
return `__VLS_Slots`;
128128
}

packages/language-core/lib/codegen/template/slotOutlet.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ export function* generateSlotOutlet(
2525
return prop.name === 'name';
2626
}
2727
if (
28-
prop.type === CompilerDOM.NodeTypes.DIRECTIVE
29-
&& prop.name === 'bind'
28+
prop.name === 'bind'
3029
&& prop.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION
3130
) {
3231
return prop.arg.content === 'name';
@@ -74,7 +73,7 @@ export function* generateSlotOutlet(
7473
nameProp.loc.start.offset,
7574
nameProp.loc.end.offset,
7675
codeFeatures.verification,
77-
`${options.slotsAssignName ?? '__VLS_slots'}`,
76+
options.slotsAssignName ?? '__VLS_slots',
7877
...codes,
7978
);
8079
}

0 commit comments

Comments
 (0)