Skip to content

Commit

Permalink
fix(log-feature): Variable getting its value from a function call
Browse files Browse the repository at this point in the history
  • Loading branch information
Chakroun-Anas committed Oct 20, 2024
1 parent 85338e6 commit 2d7d212
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 19 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes to the "turbo-console-log" extension will be documented in this file.

## [2.10.5]

### Fixed

- Improved Turbo Console Log to accurately insert log messages for variable assignments, specifically when a variable is assigned a value from a function call in TypeScript and JavaScript files.

## [2.10.4]

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "turbo-console-log",
"displayName": "Turbo Console Log",
"description": "Automating the process of writing meaningful log messages.",
"version": "2.10.4",
"version": "2.10.5",
"publisher": "ChakrounAnas",
"engines": {
"vscode": "^1.50.0"
Expand Down
28 changes: 13 additions & 15 deletions src/debug-message/js/JSDebugMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,35 +455,33 @@ export class JSDebugMessage extends DebugMessage {
const isOpeningCurlyBraceContext = document
.lineAt(multilineParenthesisVariable?.closingContextLine as number)
.text.includes('{');
const isOpeningParenthesisContext = document
.lineAt(selectionLine)
.text.includes('(');
if (isOpeningCurlyBraceContext || isOpeningParenthesisContext) {
if (this.lineCodeProcessing.isAssignedToVariable(currentLineText)) {
if (this.lineCodeProcessing.isAssignedToVariable(currentLineText)) {
if (isOpeningCurlyBraceContext) {
return {
isChecked: true,
metadata: {
openingContextLine: selectionLine,
closingContextLine: closingContextLine(
document,
multilineParenthesisVariable?.closingContextLine as number,
isOpeningCurlyBraceContext
? BracketType.CURLY_BRACES
: BracketType.PARENTHESIS,
BracketType.CURLY_BRACES,
),
} as Pick<LogMessage, 'metadata'>,
};
}
return {
isChecked: true,
metadata: {
openingContextLine:
multilineParenthesisVariable?.openingContextLine as number,
closingContextLine:
multilineParenthesisVariable?.closingContextLine as number,
} as Pick<LogMessage, 'metadata'>,
isChecked: false,
};
}
return {
isChecked: true,
metadata: {
openingContextLine:
multilineParenthesisVariable?.openingContextLine as number,
closingContextLine:
multilineParenthesisVariable?.closingContextLine as number,
} as Pick<LogMessage, 'metadata'>,
};
}
return {
isChecked: false,
Expand Down
4 changes: 3 additions & 1 deletion src/line-code-processing/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ export class JSLineCodeProcessing implements LineCodeProcessing {
return false;
}
isAssignedToVariable(loc: string): boolean {
return /(const|let|var).*{?\s*}?=.*/.test(loc);
return /^(?:const|let|var)?\s*(\w+|\$?\w+(\.\w+)*)(\s*{[^}]*}\s*)?\s*=/.test(
loc,
);
}
isAffectationToVariable(loc: string): boolean {
return /.*=.*/.test(loc);
Expand Down
22 changes: 22 additions & 0 deletions src/test/files/js/log-feature/variable/valueFromFunctionCall.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// @ts-nocheck

export function useUserSettings() {
const currencies = useQuery({
enabled: false,
queryFn: () => sdk.unified.getCurrencies(),
queryKey: ['settings', 'currencies'],
});
const setCurrentCurrency = (value: string) => {
setCookie(CURRENCY_COOKIE, value, {
sameSite: 'strict',
secure: true,
});
currencies.refetch();
queryClient.removeQueries({
queryKey: ['lazyProduct'],
type: 'active',
});
setCurrency(value);
};
return { setCurrentCurrency, setCurrentLocale };
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ export default (): void => {
).to.equal(true);
activeTextEditor.selections = [
new vscode.Selection(
new vscode.Position(25, 11),
new vscode.Position(25, 15),
new vscode.Position(25, 10),
new vscode.Position(25, 14),
),
];
await vscode.commands.executeCommand(
Expand Down
2 changes: 2 additions & 0 deletions src/test/integration/js/log-feature/variable/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import deconstructionVarAssignmentTest from './deconstructionVarAssignment';
import deconstructionArgFunction from './deconstructionArgFunction';
import primitiveVariableTest from './primitiveVariable';
import logLastLineTest from './logLastLine';
import valueFromFunctionCall from './valueFromFunctionCall';

export default (): void => {
describe('Variable context menu', () => {
deconstructionVarAssignmentTest();
primitiveVariableTest();
logLastLineTest();
deconstructionArgFunction();
valueFromFunctionCall();
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import * as vscode from 'vscode';
import Mocha, { it, describe } from 'mocha';
import { expect } from 'chai';
import {
openDocument,
expectActiveTextEditorWithFile,
documentLinesChanged,
NaturalEditorPosition,
naturalEditorLine,
} from '../../../helpers';
import { ProgrammingLanguage } from '../../../../../entities';

export default (): void => {
describe('Variable getting its value from a function call', () => {
Mocha.before(async () => {
await openDocument(
ProgrammingLanguage.JAVASCRIPT,
'log-feature/variable',
'valueFromFunctionCall.ts',
);
});
Mocha.after(async () => {
await vscode.commands.executeCommand(
'workbench.action.closeActiveEditor',
[],
);
});
it('Case 1', async () => {
const { activeTextEditor } = vscode.window;
expectActiveTextEditorWithFile(
activeTextEditor,
'valueFromFunctionCall.ts',
);
if (activeTextEditor) {
activeTextEditor.selections = [
new vscode.Selection(
new NaturalEditorPosition(4, 9),
new NaturalEditorPosition(4, 19),
),
];
await vscode.commands.executeCommand(
'turboConsoleLog.displayLogMessage',
[],
);
await Promise.all(
documentLinesChanged(activeTextEditor.document, [
naturalEditorLine(9),
]),
);
expect(
/console\.log\(.*/.test(
activeTextEditor.document.lineAt(naturalEditorLine(9)).text,
),
).to.equal(true);
}
});
});
};

0 comments on commit 2d7d212

Please sign in to comment.