How to use the jsonc-parser.findNodeAtLocation function in jsonc-parser

To help you get started, we’ve selected a few jsonc-parser examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github microsoft / vscode / extensions / extension-editing / src / extensionLinter.ts View on Github external
private readPackageJsonInfo(folder: Uri, tree: JsonNode) {
		const engine = tree && findNodeAtLocation(tree, ['engines', 'vscode']);
		const repo = tree && findNodeAtLocation(tree, ['repository', 'url']);
		const info: PackageJsonInfo = {
			isExtension: !!(engine && engine.type === 'string'),
			hasHttpsRepository: !!(repo && repo.type === 'string' && repo.value && parseUri(repo.value).scheme.toLowerCase() === 'https')
		};
		const str = folder.toString();
		const oldInfo = this.folderToPackageJsonInfo[str];
		if (oldInfo && (oldInfo.isExtension !== info.isExtension || oldInfo.hasHttpsRepository !== info.hasHttpsRepository)) {
			this.packageJsonChanged(folder); // clears this.folderToPackageJsonInfo[str]
		}
		this.folderToPackageJsonInfo[str] = info;
		return info;
	}
github microsoft / vscode / extensions / extension-editing / src / extensionLinter.ts View on Github external
if (document.isClosed) {
				return;
			}

			const diagnostics: Diagnostic[] = [];

			const tree = parseTree(document.getText());
			const info = this.readPackageJsonInfo(this.getUriFolder(document.uri), tree);
			if (info.isExtension) {

				const icon = findNodeAtLocation(tree, ['icon']);
				if (icon && icon.type === 'string') {
					this.addDiagnostics(diagnostics, document, icon.offset + 1, icon.offset + icon.length - 1, icon.value, Context.ICON, info);
				}

				const badges = findNodeAtLocation(tree, ['badges']);
				if (badges && badges.type === 'array') {
					badges.children.map(child => findNodeAtLocation(child, ['url']))
						.filter(url => url && url.type === 'string')
						.map(url => this.addDiagnostics(diagnostics, document, url.offset + 1, url.offset + url.length - 1, url.value, Context.BADGE, info));
				}

			}
			this.diagnosticsCollection.set(document.uri, diagnostics);
		});
	}
github microsoft / vscode / extensions / typescript-language-features / src / features / tsconfig.ts View on Github external
private getReferencesLinks(document: vscode.TextDocument, root: jsonc.Node) {
		return mapChildren(
			jsonc.findNodeAtLocation(root, ['references']),
			child => {
				const pathNode = jsonc.findNodeAtLocation(child, ['path']);
				if (!this.isPathValue(pathNode)) {
					return undefined;
				}

				return new vscode.DocumentLink(this.getRange(document, pathNode),
					basename(pathNode.value).match('.json$')
						? this.getFileTarget(document, pathNode)
						: this.getFolderTarget(document, pathNode));
			});
	}
github webhintio / hint / packages / utils / src / json-parser.ts View on Github external
public getLocation(path: string, options?: IJSONLocationOptions): ProblemLocation | null {
        const segments = this.pathToSegments(path);
        let node = null;

        while (!node && segments.length > 0) {
            node = findNodeAtLocation(this._root, segments) || null;

            segments.pop();
        }

        /**
         * The node isn't in the current file. Use alternative path if provided. This happens
         * when extending configurations.
         */
        if (!node && this._alternatePath && path !== this._alternatePath) {

            return this.getLocation(this._alternatePath, options);
        }

        return this.offsetToLocation(this.getAdjustedOffset(node, path, options));
    }
github DaGhostman / vscode-tree-view / src / providers / json.ts View on Github external
private getChildrenOffsets(node: json.Node): string[] {
        const offsets = [];
        for (const child of node.children) {
            const childPath = json.getLocation(this.text, child.offset).path;
            const childNode = json.findNodeAtLocation(this.tree, childPath);
            if (childNode) {
                offsets.push(childNode.offset.toString());
            }
        }
        return offsets;
    }
github webhintio / hint / packages / utils / src / json-parser.ts View on Github external
public scope(path: string): IJSONResult | null {
        const segments = this.pathToSegments(path);
        const node = findNodeAtLocation(this._root, segments);
        const value = this.findValueAtLocation(segments);

        return node ? new JSONResult(value, node, this._lines) : null;
    }
github microsoft / vscode-extension-samples / tree-view-sample / src / jsonOutline.ts View on Github external
getTreeItem(offset: number): vscode.TreeItem {
		const path = json.getLocation(this.text, offset).path
		const valueNode = json.findNodeAtLocation(this.tree, path);
		if (valueNode) {
			let hasChildren = valueNode.type === 'object' || valueNode.type === 'array';
			let treeItem: vscode.TreeItem = new vscode.TreeItem(this.getLabel(valueNode), hasChildren ? valueNode.type === 'object' ? vscode.TreeItemCollapsibleState.Expanded : vscode.TreeItemCollapsibleState.Collapsed : vscode.TreeItemCollapsibleState.None);
			treeItem.command = {
				command: 'extension.openJsonSelection',
				title: '',
				arguments: [new vscode.Range(this.editor.document.positionAt(valueNode.offset), this.editor.document.positionAt(valueNode.offset + valueNode.length))]
			};
			treeItem.iconPath = this.getIcon(valueNode);
			treeItem.contextValue = valueNode.type;
			return treeItem;
		}
		return null;
	}
github aws / aws-toolkit-vscode / src / lambda / config / templates.ts View on Github external
private ensureJsonPropertyExists(
        jsonPath: jsonParser.JSONPath,
        value: any,
        allowedTypes: jsonParser.NodeType[] = ['object', 'null']
    ) {
        const root = jsonParser.parseTree(this.json)
        const node = jsonParser.findNodeAtLocation(root, jsonPath)
        const allowedTypesSet = new Set(allowedTypes)

        if (node && !allowedTypesSet.has(node.type)) {
            throw new TemplatesConfigFieldTypeError({
                message: 'Invalid configuration',
                jsonPath: jsonPath,
                actualType: node.type,
                expectedTypes: allowedTypes
            })
        }

        if (!node || node.type === 'null') {
            const edits = jsonParser.modify(this.json, jsonPath, value, this.modificationOptions)

            if (edits.length > 0) {
                this.json = jsonParser.applyEdits(this.json, edits)
github microsoft / vscode-extension-samples / tree-view-sample / src / jsonOutline.ts View on Github external
private onDocumentChanged(changeEvent: vscode.TextDocumentChangeEvent): void {
		if (this.autoRefresh && changeEvent.document.uri.toString() === this.editor.document.uri.toString()) {
			for (const change of changeEvent.contentChanges) {
				const path = json.getLocation(this.text, this.editor.document.offsetAt(change.range.start)).path;
				path.pop();
				const node = path.length ? json.findNodeAtLocation(this.tree, path) : void 0;
				this.parseTree();
				this._onDidChangeTreeData.fire(node ? node.offset : void 0);
			}
		}
	}