mirror of
https://github.com/microsoft/vscode.git
synced 2025-12-20 02:08:47 +00:00
Remove as any from ipynb (#277057)
* Remove `as any` from ipynb * Fixes * updates
This commit is contained in:
@@ -20,8 +20,7 @@ const jupyterLanguageToMonacoLanguageMapping = new Map([
|
||||
export function getPreferredLanguage(metadata?: nbformat.INotebookMetadata) {
|
||||
const jupyterLanguage =
|
||||
metadata?.language_info?.name ||
|
||||
// eslint-disable-next-line local/code-no-any-casts
|
||||
(metadata?.kernelspec as any)?.language;
|
||||
(metadata?.kernelspec as unknown as { language: string })?.language;
|
||||
|
||||
// Default to python language only if the Python extension is installed.
|
||||
const defaultLanguage =
|
||||
@@ -291,8 +290,7 @@ export function jupyterCellOutputToCellOutput(output: nbformat.IOutput): Noteboo
|
||||
if (fn) {
|
||||
result = fn(output);
|
||||
} else {
|
||||
// eslint-disable-next-line local/code-no-any-casts
|
||||
result = translateDisplayDataOutput(output as any);
|
||||
result = translateDisplayDataOutput(output as unknown as nbformat.IDisplayData | nbformat.IDisplayUpdate | nbformat.IExecuteResult);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -6,27 +6,26 @@
|
||||
import { CancellationError } from 'vscode';
|
||||
|
||||
export function deepClone<T>(obj: T): T {
|
||||
if (!obj || typeof obj !== 'object') {
|
||||
if (obj === null || typeof obj !== 'object') {
|
||||
return obj;
|
||||
}
|
||||
if (obj instanceof RegExp) {
|
||||
// See https://github.com/microsoft/TypeScript/issues/10990
|
||||
// eslint-disable-next-line local/code-no-any-casts
|
||||
return obj as any;
|
||||
return obj;
|
||||
}
|
||||
const result: any = Array.isArray(obj) ? [] : {};
|
||||
// eslint-disable-next-line local/code-no-any-casts
|
||||
Object.keys(<any>obj).forEach((key: string) => {
|
||||
// eslint-disable-next-line local/code-no-any-casts
|
||||
if ((<any>obj)[key] && typeof (<any>obj)[key] === 'object') {
|
||||
// eslint-disable-next-line local/code-no-any-casts
|
||||
result[key] = deepClone((<any>obj)[key]);
|
||||
if (Array.isArray(obj)) {
|
||||
return obj.map(item => deepClone(item)) as unknown as T;
|
||||
}
|
||||
const result = {};
|
||||
for (const key of Object.keys(obj as object) as Array<keyof T>) {
|
||||
const value = obj[key];
|
||||
if (value && typeof value === 'object') {
|
||||
(result as T)[key] = deepClone(value);
|
||||
} else {
|
||||
// eslint-disable-next-line local/code-no-any-casts
|
||||
result[key] = (<any>obj)[key];
|
||||
(result as T)[key] = value;
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
return result as T;
|
||||
}
|
||||
|
||||
// from https://github.com/microsoft/vscode/blob/43ae27a30e7b5e8711bf6b218ee39872ed2b8ef6/src/vs/base/common/objects.ts#L117
|
||||
|
||||
@@ -37,13 +37,12 @@ export function sortObjectPropertiesRecursively(obj: any): any {
|
||||
}
|
||||
if (obj !== undefined && obj !== null && typeof obj === 'object' && Object.keys(obj).length > 0) {
|
||||
return (
|
||||
// eslint-disable-next-line local/code-no-any-casts
|
||||
Object.keys(obj)
|
||||
.sort()
|
||||
.reduce<Record<string, any>>((sortedObj, prop) => {
|
||||
.reduce<Record<string, unknown>>((sortedObj, prop) => {
|
||||
sortedObj[prop] = sortObjectPropertiesRecursively(obj[prop]);
|
||||
return sortedObj;
|
||||
}, {}) as any
|
||||
}, {})
|
||||
);
|
||||
}
|
||||
return obj;
|
||||
@@ -58,8 +57,7 @@ export function getCellMetadata(options: { cell: NotebookCell | NotebookCellData
|
||||
...(cell.metadata ?? {})
|
||||
} satisfies CellMetadata;
|
||||
if (cell.kind === NotebookCellKindMarkup) {
|
||||
// eslint-disable-next-line local/code-no-any-casts
|
||||
delete (metadata as any).execution_count;
|
||||
delete (metadata as Record<string, unknown>).execution_count;
|
||||
}
|
||||
return metadata;
|
||||
} else {
|
||||
@@ -400,10 +398,8 @@ export function pruneCell(cell: nbformat.ICell): nbformat.ICell {
|
||||
|
||||
// Remove outputs and execution_count from non code cells
|
||||
if (result.cell_type !== 'code') {
|
||||
// eslint-disable-next-line local/code-no-any-casts
|
||||
delete (<any>result).outputs;
|
||||
// eslint-disable-next-line local/code-no-any-casts
|
||||
delete (<any>result).execution_count;
|
||||
delete (result as Record<string, unknown>).outputs;
|
||||
delete (result as Record<string, unknown>).execution_count;
|
||||
} else {
|
||||
// Clean outputs from code cells
|
||||
result.outputs = result.outputs ? (result.outputs as nbformat.IOutput[]).map(fixupOutput) : [];
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
import * as assert from 'assert';
|
||||
import * as sinon from 'sinon';
|
||||
import { CancellationTokenSource, Disposable, EventEmitter, ExtensionContext, NotebookCellKind, NotebookDocumentChangeEvent, NotebookDocumentWillSaveEvent, NotebookEdit, NotebookRange, TextDocumentSaveReason, workspace, type CancellationToken, type NotebookCell, type NotebookDocument, type WorkspaceEdit, type WorkspaceEditMetadata } from 'vscode';
|
||||
import { CancellationTokenSource, Disposable, EventEmitter, ExtensionContext, NotebookCellKind, NotebookDocumentChangeEvent, NotebookDocumentWillSaveEvent, NotebookEdit, NotebookRange, TextDocument, TextDocumentSaveReason, workspace, type CancellationToken, type NotebookCell, type NotebookDocument, type WorkspaceEdit, type WorkspaceEditMetadata } from 'vscode';
|
||||
import { activate } from '../notebookModelStoreSync';
|
||||
|
||||
suite(`Notebook Model Store Sync`, () => {
|
||||
@@ -36,9 +36,8 @@ suite(`Notebook Model Store Sync`, () => {
|
||||
disposables.push(onDidChangeNotebookDocument);
|
||||
onWillSaveNotebookDocument = new AsyncEmitter<NotebookDocumentWillSaveEvent>();
|
||||
|
||||
sinon.stub(NotebookEdit, 'updateCellMetadata').callsFake((index, metadata) => {
|
||||
// eslint-disable-next-line local/code-no-any-casts
|
||||
const edit = (NotebookEdit.updateCellMetadata as any).wrappedMethod.call(NotebookEdit, index, metadata);
|
||||
const stub = sinon.stub(NotebookEdit, 'updateCellMetadata').callsFake((index, metadata) => {
|
||||
const edit = stub.wrappedMethod.call(NotebookEdit, index, metadata);
|
||||
cellMetadataUpdates.push(edit);
|
||||
return edit;
|
||||
}
|
||||
@@ -76,8 +75,7 @@ suite(`Notebook Model Store Sync`, () => {
|
||||
test('Adding cell for non Jupyter Notebook will not result in any updates', async () => {
|
||||
sinon.stub(notebook, 'notebookType').get(() => 'some-other-type');
|
||||
const cell: NotebookCell = {
|
||||
// eslint-disable-next-line local/code-no-any-casts
|
||||
document: {} as any,
|
||||
document: {} as unknown as TextDocument,
|
||||
executionSummary: {},
|
||||
index: 0,
|
||||
kind: NotebookCellKind.Code,
|
||||
@@ -106,8 +104,7 @@ suite(`Notebook Model Store Sync`, () => {
|
||||
test('Adding cell to nbformat 4.2 notebook will result in adding empty metadata', async () => {
|
||||
sinon.stub(notebook, 'metadata').get(() => ({ nbformat: 4, nbformat_minor: 2 }));
|
||||
const cell: NotebookCell = {
|
||||
// eslint-disable-next-line local/code-no-any-casts
|
||||
document: {} as any,
|
||||
document: {} as unknown as TextDocument,
|
||||
executionSummary: {},
|
||||
index: 0,
|
||||
kind: NotebookCellKind.Code,
|
||||
@@ -138,8 +135,7 @@ suite(`Notebook Model Store Sync`, () => {
|
||||
test('Added cell will have a cell id if nbformat is 4.5', async () => {
|
||||
sinon.stub(notebook, 'metadata').get(() => ({ nbformat: 4, nbformat_minor: 5 }));
|
||||
const cell: NotebookCell = {
|
||||
// eslint-disable-next-line local/code-no-any-casts
|
||||
document: {} as any,
|
||||
document: {} as unknown as TextDocument,
|
||||
executionSummary: {},
|
||||
index: 0,
|
||||
kind: NotebookCellKind.Code,
|
||||
@@ -173,8 +169,7 @@ suite(`Notebook Model Store Sync`, () => {
|
||||
test('Do not add cell id if one already exists', async () => {
|
||||
sinon.stub(notebook, 'metadata').get(() => ({ nbformat: 4, nbformat_minor: 5 }));
|
||||
const cell: NotebookCell = {
|
||||
// eslint-disable-next-line local/code-no-any-casts
|
||||
document: {} as any,
|
||||
document: {} as unknown as TextDocument,
|
||||
executionSummary: {},
|
||||
index: 0,
|
||||
kind: NotebookCellKind.Code,
|
||||
@@ -210,8 +205,7 @@ suite(`Notebook Model Store Sync`, () => {
|
||||
test('Do not perform any updates if cell id and metadata exists', async () => {
|
||||
sinon.stub(notebook, 'metadata').get(() => ({ nbformat: 4, nbformat_minor: 5 }));
|
||||
const cell: NotebookCell = {
|
||||
// eslint-disable-next-line local/code-no-any-casts
|
||||
document: {} as any,
|
||||
document: {} as unknown as TextDocument,
|
||||
executionSummary: {},
|
||||
index: 0,
|
||||
kind: NotebookCellKind.Code,
|
||||
@@ -248,10 +242,9 @@ suite(`Notebook Model Store Sync`, () => {
|
||||
}
|
||||
}));
|
||||
const cell: NotebookCell = {
|
||||
// eslint-disable-next-line local/code-no-any-casts
|
||||
document: {
|
||||
languageId: 'javascript'
|
||||
} as any,
|
||||
} as unknown as TextDocument,
|
||||
executionSummary: {},
|
||||
index: 0,
|
||||
kind: NotebookCellKind.Code,
|
||||
@@ -271,10 +264,9 @@ suite(`Notebook Model Store Sync`, () => {
|
||||
cellChanges: [
|
||||
{
|
||||
cell,
|
||||
// eslint-disable-next-line local/code-no-any-casts
|
||||
document: {
|
||||
languageId: 'javascript'
|
||||
} as any,
|
||||
} as unknown as TextDocument,
|
||||
metadata: undefined,
|
||||
outputs: undefined,
|
||||
executionSummary: undefined
|
||||
@@ -300,10 +292,9 @@ suite(`Notebook Model Store Sync`, () => {
|
||||
}
|
||||
}));
|
||||
const cell: NotebookCell = {
|
||||
// eslint-disable-next-line local/code-no-any-casts
|
||||
document: {
|
||||
languageId: 'javascript'
|
||||
} as any,
|
||||
} as unknown as TextDocument,
|
||||
executionSummary: {},
|
||||
index: 0,
|
||||
kind: NotebookCellKind.Code,
|
||||
@@ -344,10 +335,9 @@ suite(`Notebook Model Store Sync`, () => {
|
||||
}
|
||||
}));
|
||||
const cell: NotebookCell = {
|
||||
// eslint-disable-next-line local/code-no-any-casts
|
||||
document: {
|
||||
languageId: 'javascript'
|
||||
} as any,
|
||||
} as unknown as TextDocument,
|
||||
executionSummary: {},
|
||||
index: 0,
|
||||
kind: NotebookCellKind.Code,
|
||||
@@ -368,10 +358,9 @@ suite(`Notebook Model Store Sync`, () => {
|
||||
cellChanges: [
|
||||
{
|
||||
cell,
|
||||
// eslint-disable-next-line local/code-no-any-casts
|
||||
document: {
|
||||
languageId: 'javascript'
|
||||
} as any,
|
||||
} as unknown as TextDocument,
|
||||
metadata: undefined,
|
||||
outputs: undefined,
|
||||
executionSummary: undefined
|
||||
@@ -397,10 +386,9 @@ suite(`Notebook Model Store Sync`, () => {
|
||||
}
|
||||
}));
|
||||
const cell: NotebookCell = {
|
||||
// eslint-disable-next-line local/code-no-any-casts
|
||||
document: {
|
||||
languageId: 'powershell'
|
||||
} as any,
|
||||
} as unknown as TextDocument,
|
||||
executionSummary: {},
|
||||
index: 0,
|
||||
kind: NotebookCellKind.Code,
|
||||
@@ -421,10 +409,9 @@ suite(`Notebook Model Store Sync`, () => {
|
||||
cellChanges: [
|
||||
{
|
||||
cell,
|
||||
// eslint-disable-next-line local/code-no-any-casts
|
||||
document: {
|
||||
languageId: 'powershell'
|
||||
} as any,
|
||||
} as unknown as TextDocument,
|
||||
metadata: undefined,
|
||||
outputs: undefined,
|
||||
executionSummary: undefined
|
||||
@@ -456,8 +443,7 @@ suite(`Notebook Model Store Sync`, () => {
|
||||
});
|
||||
|
||||
const cell: NotebookCell = {
|
||||
// eslint-disable-next-line local/code-no-any-casts
|
||||
document: {} as any,
|
||||
document: {} as unknown as TextDocument,
|
||||
executionSummary: {},
|
||||
index: 0,
|
||||
kind: NotebookCellKind.Code,
|
||||
|
||||
Reference in New Issue
Block a user