@player-tools/json-language-service 0.8.1 → 0.8.2--canary.169.3835

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -6,12 +6,13 @@
6
6
  "types"
7
7
  ],
8
8
  "name": "@player-tools/json-language-service",
9
- "version": "0.8.1",
9
+ "version": "0.8.2--canary.169.3835",
10
10
  "main": "dist/cjs/index.cjs",
11
11
  "dependencies": {
12
- "@player-tools/xlr": "0.8.1",
13
- "@player-tools/xlr-sdk": "0.8.1",
14
- "@player-tools/xlr-utils": "0.8.1",
12
+ "@player-tools/xlr": "0.8.2--canary.169.3835",
13
+ "@player-tools/xlr-sdk": "0.8.2--canary.169.3835",
14
+ "@player-tools/xlr-utils": "0.8.2--canary.169.3835",
15
+ "@player-ui/player": "0.7.3",
15
16
  "change-case": "^4.1.1",
16
17
  "cross-fetch": "^3.0.5",
17
18
  "detect-indent": "^6.0.0",
package/src/constants.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  import type { Filters } from "@player-tools/xlr-sdk";
2
+ import { TransformFunction } from "@player-tools/xlr";
3
+
2
4
  import type { PlayerLanguageServicePlugin } from ".";
3
5
  import { AssetWrapperArrayPlugin } from "./plugins/asset-wrapper-array-plugin";
4
6
  import { SchemaInfoPlugin } from "./plugins/binding-schema-plugin";
5
7
  import { XLRPlugin } from "./plugins/xlr-plugin";
6
8
  import { DuplicateIDPlugin } from "./plugins/duplicate-id-plugin";
7
- import { LegacyActionPlugin } from "./plugins/legacy-action-plugin";
8
- import { LegacyTemplatePlugin } from "./plugins/legacy-template-plugin";
9
9
  import { MissingAssetWrapperPlugin } from "./plugins/missing-asset-wrapper-plugin";
10
10
  import { NavStatePlugin } from "./plugins/nav-state-plugin";
11
11
  import { ViewNodePlugin } from "./plugins/view-node-plugin";
@@ -20,8 +20,6 @@ export const PLUGINS: Array<PlayerLanguageServicePlugin> = [
20
20
  new DuplicateIDPlugin(),
21
21
  new ViewNodePlugin(),
22
22
  new SchemaInfoPlugin(),
23
- new LegacyTemplatePlugin(),
24
- new LegacyActionPlugin(),
25
23
  new AssetWrapperArrayPlugin(),
26
24
  new NavStatePlugin(),
27
25
  new MissingAssetWrapperPlugin(),
@@ -32,7 +30,7 @@ export const DEFAULT_FILTERS: Filters = {
32
30
  typeFilter: "Transformed",
33
31
  };
34
32
 
35
- export const TRANSFORM_FUNCTIONS = [
33
+ export const TRANSFORM_FUNCTIONS: Array<TransformFunction> = [
36
34
  applyAssetWrapperOrSwitch,
37
35
  applyValueRefs,
38
36
  applyCommonProps,
package/src/index.ts CHANGED
@@ -79,7 +79,36 @@ export class PlayerLanguageService {
79
79
  Map<Diagnostic, Violation>
80
80
  >();
81
81
 
82
- public readonly hooks = {
82
+ public readonly hooks: {
83
+ onDocumentUpdate: SyncHook<[DocumentContext], Record<string, any>>;
84
+ validate: AsyncParallelHook<[DocumentContext, ValidationContext], void>;
85
+ onValidateEnd: SyncWaterfallHook<
86
+ [
87
+ Diagnostic[],
88
+ {
89
+ /** The context of the document */
90
+ documentContext: DocumentContext;
91
+ /** A callback for adding a new fixable rule */
92
+ addFixableViolation: (diag: Diagnostic, violation: Violation) => void;
93
+ }
94
+ ],
95
+ Record<string, any>
96
+ >;
97
+ complete: AsyncParallelHook<
98
+ [EnhancedDocumentContextWithPosition, CompletionContext],
99
+ void
100
+ >;
101
+ hover: SyncBailHook<
102
+ [EnhancedDocumentContextWithPosition],
103
+ Hover | undefined,
104
+ Record<string, any>
105
+ >;
106
+ definition: SyncBailHook<
107
+ [EnhancedDocumentContextWithPosition],
108
+ Location | undefined,
109
+ Record<string, any>
110
+ >;
111
+ } = {
83
112
  onDocumentUpdate: new SyncHook<[DocumentContext]>(),
84
113
 
85
114
  validate: new AsyncParallelHook<
@@ -200,7 +229,7 @@ export class PlayerLanguageService {
200
229
  };
201
230
  }
202
231
 
203
- public onClose(document: TextDocument) {
232
+ public onClose(document: TextDocument): void {
204
233
  this.fixableViolationsForDocument.delete(document.uri);
205
234
  this.parseCache.delete(document.uri);
206
235
  }
@@ -423,11 +452,11 @@ export class PlayerLanguageService {
423
452
  );
424
453
  }
425
454
 
426
- public addLSPPlugin(plugin: PlayerLanguageServicePlugin) {
455
+ public addLSPPlugin(plugin: PlayerLanguageServicePlugin): void {
427
456
  plugin.apply(this);
428
457
  }
429
458
 
430
- async setAssetTypes(typeFiles: Array<string>) {
459
+ async setAssetTypes(typeFiles: Array<string>): Promise<void> {
431
460
  // await this.typescriptService.setAssetTypes(typeFiles);
432
461
  typeFiles.forEach((file) => {
433
462
  // Find a better way of loading default types
@@ -443,17 +472,19 @@ export class PlayerLanguageService {
443
472
  });
444
473
  }
445
474
 
446
- async setAssetTypesFromModule(manifest: Array<TSManifest>) {
447
- manifest.forEach((m) => {
448
- if (m.capabilities["Types"]?.length) {
449
- this.XLRService.XLRSDK.loadDefinitionsFromModule(m);
450
- } else {
451
- this.XLRService.XLRSDK.loadDefinitionsFromModule(
452
- m,
453
- DEFAULT_FILTERS,
454
- TRANSFORM_FUNCTIONS
455
- );
456
- }
457
- });
475
+ async setAssetTypesFromModule(manifest: Array<TSManifest>): Promise<void> {
476
+ await Promise.allSettled(
477
+ manifest.map((m) => {
478
+ if (m.capabilities["Types"]?.length) {
479
+ return this.XLRService.XLRSDK.loadDefinitionsFromModule(m);
480
+ } else {
481
+ return this.XLRService.XLRSDK.loadDefinitionsFromModule(
482
+ m,
483
+ DEFAULT_FILTERS,
484
+ TRANSFORM_FUNCTIONS
485
+ );
486
+ }
487
+ })
488
+ );
458
489
  }
459
490
  }
@@ -16,7 +16,7 @@ export interface PlayerContentProvider {
16
16
  export async function walk(
17
17
  node: ASTNode,
18
18
  visitor: (n: ASTNode) => Promise<boolean>
19
- ) {
19
+ ): Promise<void> {
20
20
  const queue: ASTNode[] = [node];
21
21
  let stop = false;
22
22
 
@@ -51,7 +51,7 @@ const checkSwitchCase = (node: StringASTNode): boolean => {
51
51
  export class AssetWrapperArrayPlugin implements PlayerLanguageServicePlugin {
52
52
  name = "asset-wrapper-to-array";
53
53
 
54
- apply(service: PlayerLanguageService) {
54
+ apply(service: PlayerLanguageService): void {
55
55
  service.hooks.validate.tap(
56
56
  this.name,
57
57
  async (documentInfo, validationContext) => {
@@ -246,7 +246,7 @@ function getLocationForSchemaType(
246
246
  export class SchemaInfoPlugin implements PlayerLanguageServicePlugin {
247
247
  name = "view-node";
248
248
 
249
- apply(service: PlayerLanguageService) {
249
+ apply(service: PlayerLanguageService): void {
250
250
  let schemaInfo: SchemaInfo | undefined;
251
251
 
252
252
  service.hooks.onDocumentUpdate.tap(this.name, (ctx) => {
@@ -110,7 +110,7 @@ const createValidationVisitor = (ctx: ValidationContext): ASTVisitor => {
110
110
  export class DuplicateIDPlugin implements PlayerLanguageServicePlugin {
111
111
  name = "duplicate-id";
112
112
 
113
- apply(service: PlayerLanguageService) {
113
+ apply(service: PlayerLanguageService): void {
114
114
  service.hooks.validate.tap(this.name, async (ctx, validation) => {
115
115
  validation.useASTVisitor(createValidationVisitor(validation));
116
116
  });
@@ -93,7 +93,7 @@ const getFlowNode = (node: ASTNode): FlowASTNode | undefined => {
93
93
  export class NavStatePlugin implements PlayerLanguageServicePlugin {
94
94
  name = "nav-state";
95
95
 
96
- apply(service: PlayerLanguageService) {
96
+ apply(service: PlayerLanguageService): void {
97
97
  service.hooks.validate.tap(this.name, async (ctx, validation) => {
98
98
  validation.useASTVisitor(createValidationVisitor(validation));
99
99
  });
@@ -142,7 +142,7 @@ const getViewInfo = (ctx: DocumentContext): DocumentViewInfo => {
142
142
  export class ViewNodePlugin implements PlayerLanguageServicePlugin {
143
143
  name = "view-node";
144
144
 
145
- apply(service: PlayerLanguageService) {
145
+ apply(service: PlayerLanguageService): void {
146
146
  let viewInfo: DocumentViewInfo | undefined;
147
147
 
148
148
  service.hooks.validate.tap(this.name, async (ctx, validation) => {
@@ -332,7 +332,7 @@ function hover(ctx: EnhancedDocumentContextWithPosition) {
332
332
  export class XLRPlugin implements PlayerLanguageServicePlugin {
333
333
  name = "xlr-plugin";
334
334
 
335
- apply(service: PlayerLanguageService) {
335
+ apply(service: PlayerLanguageService): void {
336
336
  service.hooks.validate.tap(this.name, async (ctx, validation) => {
337
337
  validation.useASTVisitor(
338
338
  createValidationVisitor(validation, service.XLRService.XLRSDK)
package/src/utils.ts CHANGED
@@ -115,7 +115,9 @@ export function formatLikeNode(
115
115
  }
116
116
 
117
117
  /** Maps the string identifying the FlowType to the named type */
118
- export function mapFlowStateToType(flowType: string | undefined) {
118
+ export function mapFlowStateToType(
119
+ flowType: string | undefined
120
+ ): string | undefined {
119
121
  let flowXLR;
120
122
  switch (flowType) {
121
123
  case "VIEW":
@@ -1,6 +1,7 @@
1
1
  import type { Filters } from "@player-tools/xlr-sdk";
2
+ import { TransformFunction } from "@player-tools/xlr";
2
3
  import type { PlayerLanguageServicePlugin } from ".";
3
4
  export declare const PLUGINS: Array<PlayerLanguageServicePlugin>;
4
5
  export declare const DEFAULT_FILTERS: Filters;
5
- export declare const TRANSFORM_FUNCTIONS: import("@player-tools/xlr").TransformFunction[];
6
+ export declare const TRANSFORM_FUNCTIONS: Array<TransformFunction>;
6
7
  //# sourceMappingURL=constants.d.ts.map
package/types/index.d.ts CHANGED
@@ -25,15 +25,25 @@ export declare class PlayerLanguageService {
25
25
  readonly hooks: {
26
26
  onDocumentUpdate: SyncHook<[DocumentContext], Record<string, any>>;
27
27
  validate: AsyncParallelHook<[DocumentContext, ValidationContext], void>;
28
- onValidateEnd: SyncWaterfallHook<[Diagnostic[], {
29
- /** The context of the document */
30
- documentContext: DocumentContext;
31
- /** A callback for adding a new fixable rule */
32
- addFixableViolation: (diag: Diagnostic, violation: Violation) => void;
33
- }], Record<string, any>>;
34
- complete: AsyncParallelHook<[EnhancedDocumentContextWithPosition, CompletionContext], void>;
35
- hover: SyncBailHook<[EnhancedDocumentContextWithPosition], Hover | undefined, Record<string, any>>;
36
- definition: SyncBailHook<[EnhancedDocumentContextWithPosition], Location | undefined, Record<string, any>>;
28
+ onValidateEnd: SyncWaterfallHook<[
29
+ Diagnostic[],
30
+ {
31
+ /** The context of the document */
32
+ documentContext: DocumentContext;
33
+ /** A callback for adding a new fixable rule */
34
+ addFixableViolation: (diag: Diagnostic, violation: Violation) => void;
35
+ }
36
+ ], Record<string, any>>;
37
+ complete: AsyncParallelHook<[
38
+ EnhancedDocumentContextWithPosition,
39
+ CompletionContext
40
+ ], void>;
41
+ hover: SyncBailHook<[
42
+ EnhancedDocumentContextWithPosition
43
+ ], Hover | undefined, Record<string, any>>;
44
+ definition: SyncBailHook<[
45
+ EnhancedDocumentContextWithPosition
46
+ ], Location | undefined, Record<string, any>>;
37
47
  };
38
48
  constructor(config?: {
39
49
  /** A list of plugins to include */
@@ -1,168 +0,0 @@
1
- import { test, expect, describe, beforeEach } from "vitest";
2
- import { TextDocument } from "vscode-languageserver-textdocument";
3
- import {
4
- ReferenceAssetsWebPluginManifest,
5
- Types,
6
- } from "@player-tools/static-xlrs";
7
- import { PlayerLanguageService } from "../..";
8
- import { toTextDocument } from "../../utils";
9
-
10
- describe("action-plugin", () => {
11
- let service: PlayerLanguageService;
12
-
13
- beforeEach(async () => {
14
- service = new PlayerLanguageService();
15
- await service.setAssetTypesFromModule([
16
- Types,
17
- ReferenceAssetsWebPluginManifest,
18
- ]);
19
- });
20
-
21
- test("fixes old actions", async () => {
22
- let testDocument = toTextDocument(
23
- JSON.stringify(
24
- {
25
- id: "foo",
26
- views: [
27
- {
28
- id: "bar",
29
- type: "collection",
30
- actions: [
31
- {
32
- id: "action-1",
33
- value: "Next",
34
- },
35
- {
36
- value: "other",
37
- label: {
38
- asset: {
39
- id: "other-label",
40
- type: "text",
41
- value: "Other",
42
- },
43
- },
44
- },
45
- ],
46
- },
47
- ],
48
- },
49
- null,
50
- 2
51
- )
52
- );
53
-
54
- let diags = await service.validateTextDocument(testDocument);
55
-
56
- expect(diags).toHaveLength(4);
57
- expect(diags?.map((m) => m.message)).toMatchInlineSnapshot(`
58
- [
59
- "Content Validation Error - missing: Property "navigation" missing from type "Flow"",
60
- "View is not reachable",
61
- "Migrate to an action-asset",
62
- "Migrate to an action-asset",
63
- ]
64
- `);
65
-
66
- const actions = await service.getCodeActionsInRange(testDocument, {
67
- diagnostics: diags ?? [],
68
- });
69
-
70
- expect(actions).toHaveLength(2);
71
-
72
- const editActions = actions[0].edit?.changes?.[testDocument.uri];
73
-
74
- const appliedAction = TextDocument.applyEdits(
75
- testDocument,
76
- editActions ?? []
77
- );
78
-
79
- expect(appliedAction).toMatchInlineSnapshot(`
80
- "{
81
- "id": "foo",
82
- "views": [
83
- {
84
- "id": "bar",
85
- "type": "collection",
86
- "actions": [
87
- {
88
- "asset": {
89
- "type": "action",
90
- "id": "action-1",
91
- "value": "Next"
92
- }
93
- },
94
- {
95
- "value": "other",
96
- "label": {
97
- "asset": {
98
- "id": "other-label",
99
- "type": "text",
100
- "value": "Other"
101
- }
102
- }
103
- }
104
- ]
105
- }
106
- ]
107
- }"
108
- `);
109
-
110
- testDocument = TextDocument.update(
111
- testDocument,
112
- [
113
- {
114
- text: appliedAction,
115
- },
116
- ],
117
- 2
118
- );
119
- diags = await service.validateTextDocument(testDocument);
120
-
121
- const nextActions = await service.getCodeActionsInRange(testDocument, {
122
- diagnostics: diags ?? [],
123
- });
124
-
125
- expect(nextActions).toHaveLength(1);
126
-
127
- const nextAction = nextActions[0].edit?.changes?.[testDocument.uri];
128
-
129
- const nextAppliedAction = TextDocument.applyEdits(
130
- testDocument,
131
- nextAction ?? []
132
- );
133
-
134
- expect(nextAppliedAction).toMatchInlineSnapshot(`
135
- "{
136
- "id": "foo",
137
- "views": [
138
- {
139
- "id": "bar",
140
- "type": "collection",
141
- "actions": [
142
- {
143
- "asset": {
144
- "type": "action",
145
- "id": "action-1",
146
- "value": "Next"
147
- }
148
- },
149
- {
150
- "asset": {
151
- "type": "action",
152
- "value": "other",
153
- "label": {
154
- "asset": {
155
- "id": "other-label",
156
- "type": "text",
157
- "value": "Other"
158
- }
159
- }
160
- }
161
- }
162
- ]
163
- }
164
- ]
165
- }"
166
- `);
167
- });
168
- });
@@ -1,97 +0,0 @@
1
- import { test, expect, describe, beforeEach } from "vitest";
2
- import { TextDocument } from "vscode-languageserver-textdocument";
3
- import {
4
- ReferenceAssetsWebPluginManifest,
5
- Types,
6
- } from "@player-tools/static-xlrs";
7
- import { PlayerLanguageService } from "../..";
8
- import { toTextDocument } from "../../utils";
9
-
10
- describe("template-plugin", () => {
11
- let service: PlayerLanguageService;
12
-
13
- beforeEach(async () => {
14
- service = new PlayerLanguageService();
15
- await service.setAssetTypesFromModule([
16
- Types,
17
- ReferenceAssetsWebPluginManifest,
18
- ]);
19
- });
20
-
21
- test("fixes old templates", async () => {
22
- const testDocument = toTextDocument(
23
- JSON.stringify(
24
- {
25
- id: "foo",
26
- views: [
27
- {
28
- id: "bar",
29
- type: "collection",
30
- templateData: "foo.bar",
31
- templateOutput: "values",
32
- template: {
33
- asset: {
34
- id: "asset-_index_",
35
- type: "text",
36
- value: "_index_",
37
- },
38
- },
39
- },
40
- ],
41
- },
42
- null,
43
- 2
44
- )
45
- );
46
-
47
- const diags = await service.validateTextDocument(testDocument);
48
-
49
- expect(diags?.map((m) => m.message)).toMatchInlineSnapshot(`
50
- [
51
- "Content Validation Error - missing: Property "navigation" missing from type "Flow"",
52
- "View is not reachable",
53
- "Migrate to the template[] syntax.",
54
- "Migrate to the template[] syntax.",
55
- "View Validation Error - type: Expected an array but got an "object"",
56
- ]
57
- `);
58
-
59
- const actions = await service.getCodeActionsInRange(testDocument, {
60
- diagnostics: diags ?? [],
61
- });
62
-
63
- expect(actions).toHaveLength(2);
64
-
65
- const editActions = actions[0].edit?.changes?.[testDocument.uri];
66
-
67
- const appliedAction = TextDocument.applyEdits(
68
- testDocument,
69
- editActions ?? []
70
- );
71
-
72
- expect(appliedAction).toMatchInlineSnapshot(`
73
- "{
74
- "id": "foo",
75
- "views": [
76
- {
77
- "id": "bar",
78
- "type": "collection",
79
- "template": [
80
- {
81
- "value": {
82
- "asset": {
83
- "id": "asset-_index_",
84
- "type": "text",
85
- "value": "_index_"
86
- }
87
- },
88
- "output": "values",
89
- "data": "foo.bar"
90
- }
91
- ]
92
- }
93
- ]
94
- }"
95
- `);
96
- });
97
- });
@@ -1,79 +0,0 @@
1
- import { DiagnosticSeverity } from "vscode-languageserver-types";
2
- import type { PlayerLanguageService, PlayerLanguageServicePlugin } from "..";
3
- import type { ASTNode } from "../parser";
4
- import { getNodeValue } from "../parser";
5
- import type { ASTVisitor, ValidationContext } from "../types";
6
-
7
- /** Create an AST visitor for checking the legacy action */
8
- function createRuleVisitor(context: ValidationContext): ASTVisitor {
9
- /** Check if a node is using the action asset or not */
10
- const checkForLegacyAction = (node: ASTNode) => {
11
- if (node.type === "asset") {
12
- return;
13
- }
14
-
15
- if (
16
- node.type === "object" &&
17
- !node.properties.some(
18
- (p) =>
19
- p.keyNode.value === "asset" ||
20
- p.keyNode.value === "dynamicSwitch" ||
21
- p.keyNode.value === "staticSwitch"
22
- )
23
- ) {
24
- context.addViolation({
25
- message: "Migrate to an action-asset",
26
- node,
27
- severity: DiagnosticSeverity.Warning,
28
- fix: () => {
29
- const newActionAsset = {
30
- asset: {
31
- type: "action",
32
- ...getNodeValue(node),
33
- },
34
- };
35
-
36
- return {
37
- name: "Convert to Asset",
38
- edit: {
39
- type: "replace",
40
- node,
41
- value: JSON.stringify(newActionAsset, null, 2),
42
- },
43
- };
44
- },
45
- });
46
- }
47
- };
48
-
49
- return {
50
- ViewNode: (viewNode) => {
51
- // Check for an `actions` array of non-assets
52
-
53
- const actionsProp = viewNode.properties.find(
54
- (p) => p.keyNode.value === "actions"
55
- );
56
-
57
- if (!actionsProp || actionsProp.valueNode?.type !== "array") {
58
- return;
59
- }
60
-
61
- // Go through the array and add a violation/fix for anything that's not an asset
62
-
63
- actionsProp.valueNode.children.forEach((action) => {
64
- checkForLegacyAction(action);
65
- });
66
- },
67
- };
68
- }
69
-
70
- /** A plugin that validates and corrects the usage of non-asset actions in a view */
71
- export class LegacyActionPlugin implements PlayerLanguageServicePlugin {
72
- name = "legacy-action";
73
-
74
- apply(service: PlayerLanguageService) {
75
- service.hooks.validate.tap(this.name, async (ctx, validationContext) => {
76
- validationContext.useASTVisitor(createRuleVisitor(validationContext));
77
- });
78
- }
79
- }