Skip to content
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

refactor: use node lookup method instead of custom code #134

Merged
merged 1 commit into from
Sep 3, 2022
Merged
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
4 changes: 2 additions & 2 deletions src/manifest/fileReferences.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Node } from 'jsonc-parser';
import { Range, TextDocument } from 'vscode';

import { getPluginsArrayNode, rangeForOffset } from './utils/iteratePlugins';
import { findPluginsNode, rangeForOffset } from './utils/iteratePlugins';

function rangeForMatch(document: TextDocument, match: { index: number; length: number }) {
return new Range(
Expand All @@ -15,7 +15,7 @@ export function iterateFileReferences(
node: Node | undefined,
callback: (props: { fileReference: string; range: Range; match: RegExpMatchArray }) => void
) {
const pluginsNode = getPluginsArrayNode(node);
const pluginsNode = findPluginsNode(node);
const pluginsRange = pluginsNode ? rangeForOffset(document, pluginsNode) : null;

const matches = document.getText().matchAll(/"(\.\/.*)"/g);
Expand Down
25 changes: 5 additions & 20 deletions src/manifest/utils/iteratePlugins.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Node } from 'jsonc-parser';
import { findNodeAtLocation, Node } from 'jsonc-parser';
import { Position, Range, TextDocument } from 'vscode';

import { parseExpoJson } from './parseExpoJson';
Expand All @@ -20,23 +20,12 @@ export interface PluginRange {
props?: JsonRange;
}

export function getPluginsArrayNode(appJson: Node | undefined) {
if (appJson?.children) {
for (const child of appJson.children) {
const children = child.children;
if (children) {
if (children && children.length === 2 && isPlugins(children[0].value)) {
return children[1];
}
}
}
}

return null;
export function findPluginsNode(appJson: Node | undefined) {
return appJson ? findNodeAtLocation(appJson, ['plugins']) : null;
}

function iteratePlugins(appJson: Node | undefined, iterator: (node: Node) => void) {
const pluginsNode = getPluginsArrayNode(appJson);
const pluginsNode = findPluginsNode(appJson);

if (pluginsNode?.children) {
pluginsNode.children.forEach(iterator);
Expand Down Expand Up @@ -113,15 +102,11 @@ export function parseSourceRanges(text: string): { appJson?: Node; plugins: Plug

export function positionIsInPlugins(document: TextDocument, position: Position) {
const { node } = parseExpoJson(document.getText());
const pluginsNode = getPluginsArrayNode(node);
const pluginsNode = findPluginsNode(node);
if (pluginsNode) {
const range = rangeForOffset(document, pluginsNode);
return range.contains(position);
}

return false;
}

function isPlugins(value: string) {
return value === 'plugins';
}
32 changes: 13 additions & 19 deletions src/manifest/utils/parseExpoJson.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Node, ParseError, parseTree } from 'jsonc-parser';
import { Node, ParseError, parseTree, findNodeAtLocation } from 'jsonc-parser';
import path from 'path';
import { TextDocument } from 'vscode';

Expand All @@ -19,27 +19,21 @@ export function parseExpoJson(text: string): { node: Node | undefined; errors: P
if (text in expoJsonCache) {
return { node: expoJsonCache[text], errors: [] };
}

const errors: ParseError[] = [];
function findUpExpoObject(node: Node | undefined): Node | undefined {
if (node?.children) {
for (const child of node.children) {
if (
child.type === 'property' &&
child.children?.[0]?.value === 'expo' &&
child.children?.[1]?.type === 'object'
) {
return findUpExpoObject(child.children[1]);
}
}
}
return node;
const tree = parseTree(text, errors);

if (!tree) {
return { errors, node: undefined };
}
// Ensure we get the expo object if it exists.
const node = findUpExpoObject(parseTree(text, errors));
if (node) {

const expo = findNodeAtLocation(tree, ['expo']);

if (expo) {
expoJsonCache = {
[text]: node,
[text]: expo,
};
}
return { node, errors };

return { errors, node: expo };
}