Skip to content

Commit

Permalink
fix: getLocationForJsonPath returns proper location for root path (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip authored Sep 3, 2019
1 parent 996747d commit 606db9a
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
},
"devDependencies": {
"@stoplight/scripts": "~5.1",
"@stoplight/types": "^9.1.2",
"@stoplight/types": "^11.1.0",
"@types/jest": "^24.0",
"@types/js-yaml": "3.12.1",
"@types/lodash": "^4.14.136",
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/getLocationForJsonPath.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('getLocationForJsonPath', () => {

it.each`
start | end | path
${[0, 0]} | ${[21, 0]} | ${[]}
${[0, 0]} | ${[0, 10]} | ${['children', 0]}
${[2, 0]} | ${[2, 31]} | ${['children', 1, 'children', 0]}
${[2, 32]} | ${[2, 40]} | ${['children', 1, 'children', 1, 'children', 0]}
Expand Down
3 changes: 2 additions & 1 deletion src/getJsonPathForPosition.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { GetJsonPathForPosition, IPosition, JsonPath } from '@stoplight/types';
import * as Unist from 'unist';
import { MarkdownParserResult } from './types';

export const getJsonPathForPosition: GetJsonPathForPosition<Unist.Node> = ({ ast }, position) => {
export const getJsonPathForPosition: GetJsonPathForPosition<MarkdownParserResult> = ({ ast }, position) => {
const path: JsonPath = [];
findNodeAtPosition(ast, position, path);
return path;
Expand Down
9 changes: 5 additions & 4 deletions src/getLocationForJsonPath.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { GetLocationForJsonPath, JsonPath } from '@stoplight/types';
import _get = require('lodash/get');
import { get as _get } from 'lodash';
import * as Unist from 'unist';
import { MarkdownParserResult } from './types';

export const getLocationForJsonPath: GetLocationForJsonPath<Unist.Node> = ({ ast }, path: JsonPath) => {
const data = _get(ast, path) as Unist.Node | undefined;
if (data === undefined) return;
export const getLocationForJsonPath: GetLocationForJsonPath<MarkdownParserResult> = ({ ast }, path: JsonPath) => {
const data = path.length === 0 ? ast : (_get(ast, path) as Unist.Node | undefined);
if (data === void 0) return;

return {
range: {
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -760,10 +760,10 @@
tslib "1.9.3"
typedoc "0.14.2"

"@stoplight/types@^9.1.2":
version "9.1.2"
resolved "https://registry.yarnpkg.com/@stoplight/types/-/types-9.1.2.tgz#0b2ed2032ae901491543b9dd19309ff3a0acf12a"
integrity sha512-eNeWwTZ1E47mrprEdgtOutovVKxYTbm8k/B7Lh4JAusGoiHbMmzVT+IDdayBpOCzq+qKLzuP0z3IZo74IXaaEg==
"@stoplight/types@^11.1.0":
version "11.1.0"
resolved "https://registry.yarnpkg.com/@stoplight/types/-/types-11.1.0.tgz#b29ecd8c8ad27bb564c7820c99f93c27f1d11bf1"
integrity sha512-z4VqnYIDTcj9R0f8AIDitBWkiAKKJ4W/Z5GJz/3fnBZBa/TD9Oak0xHbicXFXUvqTVCv/7D32PjPYiUX6W/EfQ==
dependencies:
"@types/json-schema" "^7.0.3"

Expand Down

0 comments on commit 606db9a

Please sign in to comment.