UNPKG

wireit

Version:

Upgrade your npm scripts to make them smarter and more efficient

71 lines • 2.28 kB
/** * @license * Copyright 2022 Google LLC * SPDX-License-Identifier: Apache-2.0 */ import * as jsonParser from 'jsonc-parser'; import { parseTree as parseTreeInternal } from 'jsonc-parser'; import * as pathlib from 'path'; export function findNamedNodeAtLocation(astNode, path, file) { const node = findNodeAtLocation(astNode, path); const parent = node?.parent; if (node === undefined || parent === undefined) { return { ok: true, value: undefined }; } const name = parent.children?.[0]; if (parent.type !== 'property' || name === undefined) { return { ok: false, error: { type: 'failure', reason: 'invalid-config-syntax', script: { packageDir: pathlib.dirname(file.path) }, diagnostic: { severity: 'error', message: `Expected a property, but got a ${parent.type}`, location: { file, range: { offset: astNode.offset, length: astNode.length }, }, }, }, }; } node.name = name; return { ok: true, value: node }; } export function findNodeAtLocation(astNode, path) { return jsonParser.findNodeAtLocation(astNode, path); } export function parseTree(filePath, json) { const errors = []; const result = parseTreeInternal(json, errors); if (errors.length > 0) { const diagnostics = errors.map((error) => ({ severity: 'error', message: `JSON syntax error`, location: { file: { path: filePath, contents: json, ast: result, }, range: { offset: error.offset, length: error.length, }, }, })); return { ok: false, error: { type: 'failure', reason: 'invalid-json-syntax', script: { packageDir: pathlib.dirname(filePath) }, diagnostics, }, }; } return { ok: true, value: result }; } //# sourceMappingURL=ast.js.map