Skip to content

Test ci 1 #1

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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 number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const a = 1;

const prettierConfig = prettier.resolveConfig(__dirname) ?? {};
const START_OF_LINE_WHITESPACE_MATCHER = /^([ ]*)/;
const SEARCH_START_OF_LINE_WHITESPACE_MATCHER = /[^ ]/;
const BACKTICK_REGEX = /`/g;
const TEMPLATE_EXPR_OPENER = /\$\{/g;

Expand All @@ -55,16 +56,16 @@ function getExpectedIndentForNode(
sourceCodeLines: string[],
): number {
const lineIdx = node.loc.start.line - 1;
const indent = START_OF_LINE_WHITESPACE_MATCHER.exec(
sourceCodeLines[lineIdx],
)![1];
return indent.length;
const indent = sourceCodeLines[lineIdx].search(
SEARCH_START_OF_LINE_WHITESPACE_MATCHER,
);
if (indent === -1) {
return 0;
}
return indent;
}
function doIndent(line: string, indent: number): string {
for (let i = 0; i < indent; i += 1) {
line = ' ' + line;
}
return line;
return ' '.repeat(indent) + line;
}

function getQuote(code: string): '"' | "'" | null {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
TSESTree,
} from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import type { SourceCode } from '@typescript-eslint/utils/ts-eslint';
import {
isBigIntLiteralType,
isBooleanLiteralType,
Expand Down Expand Up @@ -122,6 +123,7 @@ function isValidFalseBooleanCheckType(
export function gatherLogicalOperands(
node: TSESTree.LogicalExpression,
parserServices: ParserServicesWithTypeInformation,
sourceCode: Readonly<SourceCode>,
options: PreferOptionalChainOptions,
): {
operands: Operand[];
Expand Down Expand Up @@ -157,7 +159,20 @@ export function gatherLogicalOperands(
comparedExpression.type === AST_NODE_TYPES.UnaryExpression &&
comparedExpression.operator === 'typeof'
) {
// typeof x === 'undefined'
const argument = comparedExpression.argument;
if (argument.type === AST_NODE_TYPES.Identifier) {
const reference = sourceCode
.getScope(argument)
.references.find(ref => ref.identifier.name === argument.name);

if (!reference?.resolved?.defs.length) {
// typeof window === 'undefined'
result.push({ type: OperandValidity.Invalid });
continue;
}
}

// typeof x.y === 'undefined'
result.push({
type: OperandValidity.Valid,
comparedName: comparedExpression.argument,
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin/src/rules/prefer-optional-chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ export default createRule<
const { operands, newlySeenLogicals } = gatherLogicalOperands(
node,
parserServices,
context.sourceCode,
options,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,7 @@ describe('hand-crafted cases', () => {
declare const x: 0n | { a: string };
!x || x.a;
`,
"typeof globalThis !== 'undefined' && globalThis.Array();",
],
invalid: [
// two errors
Expand Down Expand Up @@ -1915,6 +1916,42 @@ describe('hand-crafted cases', () => {
},
],
},
{
code: `
function foo(globalThis?: { Array: Function }) {
typeof globalThis !== 'undefined' && globalThis.Array();
}
`,
output: `
function foo(globalThis?: { Array: Function }) {
globalThis?.Array();
}
`,
errors: [
{
messageId: 'preferOptionalChain',
},
],
},
{
code: `
typeof globalThis !== 'undefined' && globalThis.Array && globalThis.Array();
`,
output: null,
errors: [
{
messageId: 'preferOptionalChain',
suggestions: [
{
messageId: 'optionalChainSuggest',
output: `
typeof globalThis !== 'undefined' && globalThis.Array?.();
`,
},
],
},
],
},
],
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/type-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"clean": "tsc -b tsconfig.build.json --clean",
"postclean": "rimraf dist && rimraf _ts3.4 && rimraf _ts4.3 && rimraf coverage",
"format": "prettier --write \"./**/*.{ts,mts,cts,tsx,js,mjs,cjs,jsx,json,md,css}\" --ignore-path ../../.prettierignore",
"lint": "npx nx lint",
"lint": "npx nx lin --verbose",
"test": "jest --coverage",
"typecheck": "tsc -p tsconfig.json --noEmit"
},
Expand Down