brighterscript-xml-plugin 0.2.1 → 1.0.0-alpha.1

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.
@@ -0,0 +1,182 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const roku_types_1 = require("brighterscript/dist/roku-types");
4
+ const systemNodes = roku_types_1.nodes;
5
+ const fieldTypeValidator = {
6
+ validateFieldType: (value, type) => {
7
+ if (!value) {
8
+ value = '';
9
+ }
10
+ const normalizedType = fieldTypeValidator.normalizeType(type);
11
+ const validator = fieldTypeValidator[normalizedType];
12
+ if (validator) {
13
+ return validator(value);
14
+ }
15
+ return normalizedType; // If no specific validator exists, assume it's valid
16
+ },
17
+ normalizeType: (value) => {
18
+ let normalized = value.toLowerCase();
19
+ if (normalized.startsWith('associative array of ')) {
20
+ normalized = 'assocarray';
21
+ }
22
+ if (normalized.startsWith('targetset')) {
23
+ normalized = 'array';
24
+ }
25
+ if (normalized.endsWith(' node') || systemNodes[normalized]) {
26
+ normalized = 'node';
27
+ }
28
+ if (normalized.endsWith(' string')) {
29
+ normalized = 'string';
30
+ }
31
+ if (normalized === 'event') {
32
+ normalized = 'string';
33
+ }
34
+ if (normalized.includes('**')) {
35
+ normalized = 'node';
36
+ }
37
+ if (normalized.startsWith('vector2d')) {
38
+ normalized = 'vector2d';
39
+ }
40
+ if (normalized.startsWith('color')) {
41
+ normalized = 'color';
42
+ }
43
+ normalized = normalized.replace('associative array', 'assocarray');
44
+ if (normalized.includes('array of')) {
45
+ normalized = normalized.replace('roarray of', '').replace('array of', '').trim();
46
+ if (normalized.endsWith('s')) {
47
+ normalized = normalized.slice(0, -1);
48
+ }
49
+ normalized = `${normalized}array`;
50
+ }
51
+ if (normalized === `float'array`) {
52
+ normalized = 'floatarray';
53
+ }
54
+ return normalized;
55
+ },
56
+ integer: (value) => {
57
+ return /^\d+$/.test(value);
58
+ },
59
+ int: (value) => {
60
+ return fieldTypeValidator.integer(value);
61
+ },
62
+ boolean: (value) => {
63
+ return /^(true|false)$/.test(value);
64
+ },
65
+ double: (value) => {
66
+ return fieldTypeValidator.float(value);
67
+ },
68
+ string: (value) => {
69
+ return typeof value === 'string';
70
+ },
71
+ float: (value) => {
72
+ return /^-?\d+(\.\d+)?$/.test(value) || /^\.\d+$/.test(value);
73
+ },
74
+ color: (value) => {
75
+ return /^(#|0[xX])([0-9a-fA-F]{3}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/.test(value);
76
+ },
77
+ str: (value) => {
78
+ return fieldTypeValidator.string(value);
79
+ },
80
+ time: (value) => {
81
+ return fieldTypeValidator.float(value);
82
+ },
83
+ bool: (value) => {
84
+ return fieldTypeValidator.boolean(value);
85
+ },
86
+ node: (value) => {
87
+ return fieldTypeValidator.string(value);
88
+ },
89
+ longinteger: (value) => {
90
+ return fieldTypeValidator.integer(value);
91
+ },
92
+ asArray: (fn, value) => {
93
+ if (value.startsWith('[') && value.endsWith(']')) {
94
+ const items = value.slice(1, -1).split(',').map(item => item.trim());
95
+ return items.every((item) => fn(item));
96
+ }
97
+ return false;
98
+ },
99
+ uri: (value) => {
100
+ return fieldTypeValidator.string(value);
101
+ },
102
+ url: (value) => {
103
+ return fieldTypeValidator.string(value);
104
+ },
105
+ vector2d: (value) => {
106
+ return fieldTypeValidator.floatarray(value);
107
+ },
108
+ floatarray: (value) => {
109
+ return fieldTypeValidator.asArray(fieldTypeValidator.float, value);
110
+ },
111
+ integerarray: (value) => {
112
+ return fieldTypeValidator.asArray(fieldTypeValidator.integer, value);
113
+ },
114
+ boolarray: (value) => {
115
+ return fieldTypeValidator.asArray(fieldTypeValidator.boolean, value);
116
+ },
117
+ booleanarray: (value) => {
118
+ return fieldTypeValidator.asArray(fieldTypeValidator.boolean, value);
119
+ },
120
+ stringarray: (value) => {
121
+ return fieldTypeValidator.asArray(fieldTypeValidator.string, value);
122
+ },
123
+ timearray: (value) => {
124
+ return fieldTypeValidator.asArray(fieldTypeValidator.float, value);
125
+ },
126
+ colorarray: (value) => {
127
+ return fieldTypeValidator.asArray(fieldTypeValidator.color, value);
128
+ },
129
+ nodearray: (value) => {
130
+ return fieldTypeValidator.asArray(fieldTypeValidator.node, value);
131
+ },
132
+ assocarray: (value) => {
133
+ try {
134
+ const obj = JSON.parse(value);
135
+ return typeof obj === 'object' && !Array.isArray(obj);
136
+ }
137
+ catch (e) {
138
+ return false;
139
+ }
140
+ },
141
+ roassociativearray: (value) => {
142
+ return fieldTypeValidator.assocarray(value);
143
+ },
144
+ roassocarray: (value) => {
145
+ return fieldTypeValidator.assocarray(value);
146
+ },
147
+ array: (value) => {
148
+ return fieldTypeValidator.asArray(fieldTypeValidator.str, value);
149
+ },
150
+ roarray: (value) => {
151
+ return fieldTypeValidator.array(value);
152
+ },
153
+ rect2d: (value) => {
154
+ return fieldTypeValidator.asArray(fieldTypeValidator.float, value);
155
+ },
156
+ rect2darray: (value) => {
157
+ return fieldTypeValidator.asArrayOfArrays(fieldTypeValidator.rect2d, value);
158
+ },
159
+ rectanglearray: (value) => {
160
+ return fieldTypeValidator.asArrayOfArrays(fieldTypeValidator.rect2d, value);
161
+ },
162
+ vector2darray: (value) => {
163
+ return fieldTypeValidator.asArrayOfArrays(fieldTypeValidator.float, value);
164
+ },
165
+ assocarrayarray: (value) => {
166
+ return fieldTypeValidator.asArrayOfArrays(fieldTypeValidator.assocarray, value);
167
+ },
168
+ roassocarrayarray: (value) => {
169
+ return fieldTypeValidator.asArrayOfArrays(fieldTypeValidator.roassocarray, value);
170
+ },
171
+ roassociativearrayarray: (value) => {
172
+ return fieldTypeValidator.asArrayOfArrays(fieldTypeValidator.roassociativearray, value);
173
+ },
174
+ asArrayOfArrays: (fn, value) => {
175
+ if (value.startsWith('[[') && value.endsWith(']]')) {
176
+ const items = value.slice(1, -1).split('],[').map(item => item.replace(/^\[|\]$/g, '').trim());
177
+ return items.every((item) => fieldTypeValidator.asArray(fn, `[${item}]`));
178
+ }
179
+ return false;
180
+ },
181
+ };
182
+ exports.default = fieldTypeValidator;
@@ -3,10 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SGXmlCompletionProvider = void 0;
4
4
  const brighterscript_1 = require("brighterscript");
5
5
  const vscode_languageserver_types_1 = require("vscode-languageserver-types");
6
- const roku_types_1 = require("brighterscript/dist/roku-types");
6
+ const data_1 = require("./data");
7
7
  const SystemCompletion_1 = require("./SystemCompletion");
8
8
  const OPEN_CLOSE_TAGS = ['<', '</', '>', '/>'];
9
- const systemNodes = roku_types_1.nodes;
9
+ const systemNodes = data_1.nodes;
10
10
  class SGXmlCompletionProvider {
11
11
  constructor(program) {
12
12
  this.program = program;
@@ -25,13 +25,13 @@ class SGXmlCompletionProvider {
25
25
  return result;
26
26
  }
27
27
  getFieldCompletions(componentName) {
28
- var _a;
28
+ var _a, _b;
29
29
  componentName = componentName.toLocaleLowerCase();
30
30
  const projectComponent = this.program.getComponent(componentName);
31
31
  let result = [];
32
32
  if (projectComponent) {
33
33
  result.push(...this.getCompletionsFromXmlFile(projectComponent.file));
34
- const extendsName = (_a = projectComponent.file.ast.component) === null || _a === void 0 ? void 0 : _a.extends;
34
+ const extendsName = (_b = (_a = projectComponent.file.ast.componentElement) === null || _a === void 0 ? void 0 : _a.extends) !== null && _b !== void 0 ? _b : 'Node';
35
35
  if (extendsName) {
36
36
  result.push(...this.getAllAvailableFields(extendsName));
37
37
  }
@@ -49,13 +49,13 @@ class SGXmlCompletionProvider {
49
49
  return result;
50
50
  }
51
51
  getCompletionsFromXmlFile(xmlFile) {
52
- var _a, _b;
53
- return ((_b = (_a = xmlFile.ast.component) === null || _a === void 0 ? void 0 : _a.api.fields.map((f) => {
52
+ var _a, _b, _c;
53
+ return ((_c = (_b = (_a = xmlFile.ast.componentElement) === null || _a === void 0 ? void 0 : _a.interfaceElement) === null || _b === void 0 ? void 0 : _b.fields.map((f) => {
54
54
  return {
55
55
  label: f.id,
56
56
  detail: `${f.type}${f.value ? `: ${f.value}` : ''}`,
57
57
  };
58
- })) !== null && _b !== void 0 ? _b : []);
58
+ })) !== null && _c !== void 0 ? _c : []);
59
59
  }
60
60
  getComponentCompletions(file, beforeToken) {
61
61
  const completions = [];
@@ -90,20 +90,20 @@ class SGXmlCompletionProvider {
90
90
  let result = [];
91
91
  if (component) {
92
92
  result = [
93
- ...((_c = (_b = (_a = component.file.ast.component) === null || _a === void 0 ? void 0 : _a.api) === null || _b === void 0 ? void 0 : _b.fields.map((f) => {
93
+ ...((_c = (_b = (_a = component.file.ast.componentElement) === null || _a === void 0 ? void 0 : _a.interfaceElement) === null || _b === void 0 ? void 0 : _b.fields.map((f) => {
94
94
  return {
95
95
  name: f.id,
96
96
  type: f.type,
97
97
  sortText: '0_' + f.id,
98
98
  };
99
99
  })) !== null && _c !== void 0 ? _c : []),
100
- ...this.getAllAvailableFields((_e = (_d = component.file.ast.component) === null || _d === void 0 ? void 0 : _d.extends) !== null && _e !== void 0 ? _e : '').map((f) => {
100
+ ...this.getAllAvailableFields((_e = (_d = component.file.ast.componentElement) === null || _d === void 0 ? void 0 : _d.extends) !== null && _e !== void 0 ? _e : '').map((f) => {
101
101
  return Object.assign(Object.assign({}, f), { sortText: '1_' + f.sortText });
102
102
  }),
103
103
  ];
104
104
  }
105
105
  else {
106
- const rokuComponent = roku_types_1.nodes[componentName.toLowerCase()];
106
+ const rokuComponent = data_1.nodes[componentName.toLowerCase()];
107
107
  if (rokuComponent) {
108
108
  const rokuComponentFields = (_f = rokuComponent.fields) === null || _f === void 0 ? void 0 : _f.filter((f) => { var _a; return (_a = f.accessPermission) === null || _a === void 0 ? void 0 : _a.includes('WRITE'); }).map((f) => {
109
109
  return {
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SGXmlDiagnostics = void 0;
4
+ const brighterscript_1 = require("brighterscript");
5
+ exports.SGXmlDiagnostics = {
6
+ UnknownComponent: (componentName, element) => ({
7
+ message: `Unknown component: ${componentName}`,
8
+ severity: brighterscript_1.DiagnosticSeverity.Error,
9
+ code: 'SG1001',
10
+ location: element.tokens.startTagName.location
11
+ }),
12
+ InvalidSystemAttribute: (attributeName, component, attr) => ({
13
+ message: `Field "${attributeName}" not found on component "${component}"`,
14
+ severity: brighterscript_1.DiagnosticSeverity.Error,
15
+ code: 'SG1002',
16
+ location: attr.tokens.key.location
17
+ }),
18
+ InvalidAttributeValue: (attributeName, expectedType, attr) => {
19
+ var _a;
20
+ return ({
21
+ message: `Field value "${attributeName}" does not match type ${expectedType}`,
22
+ severity: brighterscript_1.DiagnosticSeverity.Error,
23
+ code: 'SG1003',
24
+ location: (_a = attr.tokens.value) === null || _a === void 0 ? void 0 : _a.location
25
+ });
26
+ },
27
+ AttributeNameCaseMismatch: (attributeName, expectedName, attr) => ({
28
+ message: `Field "${attributeName}" should match case for "${expectedName}"`,
29
+ severity: brighterscript_1.DiagnosticSeverity.Warning,
30
+ code: 'SG2001',
31
+ location: attr.tokens.key.location
32
+ })
33
+ };
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.SGXmlValidator = void 0;
7
+ const data_1 = require("./data");
8
+ const FieldTypeValidator_1 = __importDefault(require("./FieldTypeValidator"));
9
+ const SGXmlDiagnostics_1 = require("./SGXmlDiagnostics");
10
+ const systemNodes = data_1.nodes;
11
+ class SGXmlValidator {
12
+ constructor(program) {
13
+ this.program = program;
14
+ }
15
+ validateXmlFile(event) {
16
+ var _a, _b, _c, _d;
17
+ const file = event.file;
18
+ const diags = (_d = (_c = (_b = (_a = file === null || file === void 0 ? void 0 : file.ast) === null || _a === void 0 ? void 0 : _a.componentElement) === null || _b === void 0 ? void 0 : _b.childrenElement) === null || _c === void 0 ? void 0 : _c.elements.flatMap((child) => {
19
+ return this.validateComponent(child);
20
+ })) !== null && _d !== void 0 ? _d : [];
21
+ event.program.diagnostics.register(diags);
22
+ }
23
+ validateComponent(element) {
24
+ const diags = [];
25
+ if (!element.tagName) {
26
+ return diags;
27
+ }
28
+ if (this.validateComponentExists(element)) {
29
+ diags.push(...this.validateNodeElement(element));
30
+ element.elements.forEach((child) => {
31
+ diags.push(...this.validateComponent(child));
32
+ });
33
+ }
34
+ else {
35
+ this.program.logger.error(`Unknown component: ${element.tagName}`, element);
36
+ const diag = SGXmlDiagnostics_1.SGXmlDiagnostics.UnknownComponent(element.tagName, element);
37
+ diags.push(diag);
38
+ }
39
+ return diags;
40
+ }
41
+ validateNodeElement(element) {
42
+ const diags = [];
43
+ const fields = this.findAllFields(element.tagName);
44
+ element.attributes.forEach((attr) => {
45
+ const field = fields.find(a => {
46
+ return a.id.toLowerCase() === attr.key.toLowerCase();
47
+ });
48
+ if (!field) {
49
+ this.program.logger.error(`Invalid system attribute: ${attr.key}`, attr);
50
+ diags.push(SGXmlDiagnostics_1.SGXmlDiagnostics.InvalidSystemAttribute(attr.key, element.tagName, attr));
51
+ return;
52
+ }
53
+ if (field.type && !FieldTypeValidator_1.default.validateFieldType(attr.value, field.type)) {
54
+ this.program.logger.error(`Invalid value for attribute: ${attr.key}`, attr);
55
+ diags.push(SGXmlDiagnostics_1.SGXmlDiagnostics.InvalidAttributeValue(attr.key, field.type, attr));
56
+ }
57
+ if (field.id !== attr.key) {
58
+ this.program.logger.warn(`Field "${attr.key}" should be "${field.id}"`, attr);
59
+ diags.push(SGXmlDiagnostics_1.SGXmlDiagnostics.AttributeNameCaseMismatch(attr.key, field.id, attr));
60
+ }
61
+ });
62
+ return diags;
63
+ }
64
+ validateComponentExists(element) {
65
+ let component = this.program.getComponent(element.tagName) || systemNodes[element.tagName.toLocaleLowerCase()];
66
+ return !!component;
67
+ }
68
+ findAllFields(name) {
69
+ var _a, _b, _c, _d, _e;
70
+ const fields = [];
71
+ let component = (_a = this.program.getComponent(name)) === null || _a === void 0 ? void 0 : _a.file;
72
+ if (component) {
73
+ if ((_c = (_b = component.ast.componentElement) === null || _b === void 0 ? void 0 : _b.interfaceElement) === null || _c === void 0 ? void 0 : _c.fields) {
74
+ fields.push(...component.ast.componentElement.interfaceElement.fields.map(f => {
75
+ return {
76
+ id: f.id,
77
+ type: f.type,
78
+ alias: f.alias
79
+ };
80
+ }) || []);
81
+ }
82
+ if ((_d = component.ast.componentElement) === null || _d === void 0 ? void 0 : _d.extends) {
83
+ fields.push(...this.findAllFields(component.ast.componentElement.extends));
84
+ }
85
+ }
86
+ component = systemNodes[name.toLocaleLowerCase()];
87
+ if (component) {
88
+ fields.push(...((_e = component.fields) === null || _e === void 0 ? void 0 : _e.map(f => {
89
+ return {
90
+ id: f.name,
91
+ type: f.type
92
+ };
93
+ })) || []);
94
+ if (component.extends) {
95
+ fields.push(...this.findAllFields(component.extends.name));
96
+ }
97
+ }
98
+ return fields;
99
+ }
100
+ }
101
+ exports.SGXmlValidator = SGXmlValidator;