@aiot-toolkit/parser 2.0.6-beta.3 → 2.0.6-beta.5

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.
@@ -1,6 +1,6 @@
1
1
  import { Dictionary, Loglevel } from '@aiot-toolkit/shared-utils';
2
2
  import { StyleMessage } from '@aiot-toolkit/shared-utils';
3
- import { BlockItem, StyleItem } from '../../interface/IStyleAst';
3
+ import { BlockItem, StyleItem } from '../interface/IStyleAst';
4
4
  type ValidateInfo = {
5
5
  level: Loglevel;
6
6
  message: StyleMessage[];
@@ -5,8 +5,8 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.STYLE_ATTRIBUTE_LIST = exports.STYLE_ATTRIBUTE_CONFIG = void 0;
7
7
  var _sharedUtils = require("@aiot-toolkit/shared-utils");
8
- var _ExtendedBoxStyle = _interopRequireDefault(require("../../translate/vela/utils/ExtendedBoxStyle"));
9
- var _UxUtil = _interopRequireDefault(require("../../utils/UxUtil"));
8
+ var _ExtendedBoxStyle = _interopRequireDefault(require("../translate/vela/utils/ExtendedBoxStyle"));
9
+ var _UxUtil = _interopRequireDefault(require("../utils/UxUtil"));
10
10
  var _cssTree = _interopRequireDefault(require("css-tree"));
11
11
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
12
12
  const REGEXP_NAME = /^([a-zA-Z_]+[a-zA-Z0-9]*\s*,\s*)*[a-zA-Z_]+[a-zA-Z0-9]*$/;
@@ -1345,9 +1345,9 @@ function validateLengthNode(sourceNode) {
1345
1345
  value[0] = value[0];
1346
1346
  const validResult = validLength(value[0]);
1347
1347
  if (validResult.message.length > 0) {
1348
- validateInfo.message = [`The value verification of attribute`, {
1349
- word: attributeName
1350
- }, `failed.`, ...validResult.message];
1348
+ validateInfo.message = [`The value of attribute`, {
1349
+ word: `${attributeName}:`
1350
+ }, ...validResult.message];
1351
1351
  }
1352
1352
  }
1353
1353
  return validateInfo;
@@ -1363,11 +1363,9 @@ function validLength(value) {
1363
1363
  message: []
1364
1364
  };
1365
1365
  if (_cssTree.default.lexer.matchType('length', value.value + value.unit).error !== null) {
1366
- validateInfo.message = [`The length value is`, {
1367
- word: value.value || 'null'
1368
- }, `, unit is`, {
1369
- word: value.unit || 'null'
1370
- }, `, which does not meet the length standard.`];
1366
+ validateInfo.message = [{
1367
+ word: `${value.value}${value.unit}`
1368
+ }, `is invalid.`];
1371
1369
  }
1372
1370
  return validateInfo;
1373
1371
  }
@@ -1549,30 +1547,24 @@ function validPosition(sourceNode) {
1549
1547
  name: attributeName
1550
1548
  } = sourceNode;
1551
1549
  if (value.length <= 0) {
1552
- validateInfo.message = [`Error parsing the value of attribute`, {
1550
+ validateInfo.message = [`The value of attribute`, {
1553
1551
  word: `${attributeName}`
1554
- }];
1552
+ }, 'is invalid.'];
1555
1553
  } else if (value.length > 3) {
1556
- validateInfo.message = [`Error parsing the value length`, `of`, {
1557
- word: `'${attributeName}'`
1558
- }, `is`, {
1559
- word: value.length
1560
- }, `, which does not meet the requirement.`];
1554
+ validateInfo.message = [`The value length`, `of attribute`, {
1555
+ word: `${attributeName}: ${value.length}`
1556
+ }, `is invalid.`];
1561
1557
  } else {
1562
1558
  value.forEach(v => {
1563
1559
  v = v;
1564
1560
  const validResult = validLength(v);
1565
1561
  // 有报错
1566
1562
  if (validResult.message.length > 0) {
1567
- validateInfo.message.push(...validResult.message);
1563
+ validateInfo.message = [`The value of attribute`, {
1564
+ word: `${attributeName}:`
1565
+ }, ...validResult.message];
1568
1566
  }
1569
1567
  });
1570
- // 如果有报错,添加一下属性名
1571
- if (validateInfo.message.length > 0) {
1572
- validateInfo.message.unshift(`The value verification of attribute `, {
1573
- word: attributeName
1574
- }, `failed.`);
1575
- }
1576
1568
  }
1577
1569
  return validateInfo;
1578
1570
  }
@@ -93,6 +93,12 @@ const elementConfig = {
93
93
  }
94
94
  }
95
95
  },
96
+ markdown: {
97
+ allowTextChildren: true,
98
+ attributes: {
99
+ value: {}
100
+ }
101
+ },
96
102
  'arc-text': {
97
103
  allowTextChildren: true,
98
104
  attributes: {
@@ -1,8 +1,9 @@
1
1
  /**
2
2
  * IStyleError
3
3
  */
4
+ import { StyleMessage } from '@aiot-toolkit/shared-utils';
4
5
  export interface IStyleError {
5
- message: string;
6
+ messages: StyleMessage[];
6
7
  details: string;
7
8
  startLine: number;
8
9
  startCol: number;
@@ -14,7 +14,8 @@ class ScriptParser {
14
14
  parser(content) {
15
15
  // const { onLog } = this.options
16
16
  const ast = {
17
- content
17
+ // 判断content是否包含有效js内容,若只包含\n\r\t空白符,则content为''
18
+ content: content.trim() ? content : ''
18
19
  };
19
20
  return {
20
21
  ast
@@ -598,17 +598,10 @@ class StyleParser {
598
598
  let errors = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
599
599
  let isParserError = errorType === 'parserError';
600
600
  let sourceCodeStyle = _sharedUtils.Loglevel.WARN;
601
- const {
602
- onLog,
603
- filePath
604
- } = this.options;
605
601
  if (isParserError) {
606
602
  sourceCodeStyle = _sharedUtils.Loglevel.ERROR;
607
603
  }
608
604
  errors.forEach(error => {
609
- const sourceCode = error.source ? [`, source code:`, {
610
- word: `${error.source}`
611
- }] : [];
612
605
  const position = this.styleMapUtil.transfromLocToPosition({
613
606
  start: {
614
607
  line: error.startLine,
@@ -624,14 +617,7 @@ class StyleParser {
624
617
  });
625
618
  position.startColumn++;
626
619
  position.endColumn++;
627
- onLog({
628
- level: sourceCodeStyle,
629
- filePath,
630
- position,
631
- message: [{
632
- word: error.message
633
- }, ...sourceCode]
634
- });
620
+ _StyleUtil.default.handleStyleError(sourceCodeStyle, error.messages, this.options, position);
635
621
  });
636
622
  return [];
637
623
  }
@@ -20,6 +20,7 @@ declare class UxParser implements IParser<IUxAst> {
20
20
  * 1. template:
21
21
  * a. 最多1个
22
22
  * b. 子元素有且仅有1个,不能是 block
23
+ * 2. 必须有闭合标签, 示例: `<script>....</script>`正确, `<script>aaaa`错误
23
24
  */
24
25
  private validateContent;
25
26
  /**
@@ -201,6 +201,7 @@ class UxParser {
201
201
  * 1. template:
202
202
  * a. 最多1个
203
203
  * b. 子元素有且仅有1个,不能是 block
204
+ * 2. 必须有闭合标签, 示例: `<script>....</script>`正确, `<script>aaaa`错误
204
205
  */
205
206
  validateContent(childNodes) {
206
207
  let result = true;
@@ -249,21 +250,22 @@ class UxParser {
249
250
  }
250
251
 
251
252
  // 2
252
- // if (scriptCount !== 1) {
253
- // onLog({
254
- // level: Loglevel.THROW,
255
- // filePath,
256
- // message: [
257
- // { word: '<script>' },
258
- // `There are`,
259
- // { word: scriptCount },
260
- // `, but expect to have`,
261
- // { word: 1 }
262
- // ]
263
- // })
264
- // result = false
265
- // }
266
-
253
+ childNodes.forEach(node => {
254
+ if (_TemplateUtil.default.isElement(node)) {
255
+ const {
256
+ sourceCodeLocation
257
+ } = node;
258
+ if (sourceCodeLocation && !sourceCodeLocation.endTag) {
259
+ onLog({
260
+ level: _sharedUtils.Loglevel.ERROR,
261
+ filePath,
262
+ position: _TemplateUtil.default.parse5LocationToPosition(sourceCodeLocation.startTag),
263
+ message: [`<${node.nodeName}>`, 'must have an end tag']
264
+ });
265
+ result = false;
266
+ }
267
+ }
268
+ });
267
269
  return result;
268
270
  }
269
271
  /**
@@ -51,7 +51,10 @@ class ScriptToTypescript {
51
51
  * @returns
52
52
  */
53
53
  babelScript(sourceTree) {
54
- const sourceCode = sourceTree.content ? sourceTree.content : `""`;
54
+ if (sourceTree.content === '') {
55
+ return '';
56
+ }
57
+ const sourceCode = sourceTree.content;
55
58
  const relativePath = _path.default.relative(this.options.projectPath, this.options.filePath);
56
59
  const plugins = [
57
60
  // 替换所有的require节点
@@ -137,10 +137,6 @@ class StyleToTypescript {
137
137
  * @param name
138
138
  */
139
139
  addThorwLog(type, name, position, info) {
140
- const {
141
- onLog,
142
- filePath
143
- } = this.options;
144
140
  const logLevel = name === ' ' || name === '>' ? _sharedUtils.Loglevel.WARN : _sharedUtils.Loglevel.THROW;
145
141
  const message = info ? [`### StyleToTypescript ### Selector type unsupport`, {
146
142
  word: info
@@ -149,12 +145,7 @@ class StyleToTypescript {
149
145
  }, ',name:', {
150
146
  word: name
151
147
  }];
152
- onLog({
153
- filePath,
154
- position,
155
- level: logLevel,
156
- message
157
- });
148
+ _StyleUtil.default.handleStyleError(logLevel, message, this.options, position);
158
149
  }
159
150
  translateAtrule(sourceNode) {
160
151
  let targetNodes = [];
@@ -1,3 +1,4 @@
1
+ import { IPosition, Loglevel, StyleMessage } from '@aiot-toolkit/shared-utils';
1
2
  import { AtrulePrelude, CssNode, Declaration, Identifier, MediaFeature, MediaQuery, MediaQueryList, SelectorList, SyntaxParseError } from 'css-tree';
2
3
  import IOptions from '../config/IOptions';
3
4
  import { IImageFunction } from '../interface/IImageResource';
@@ -226,5 +227,6 @@ declare class StyleUtil {
226
227
  * @returns
227
228
  */
228
229
  static translateToNormalValue(value: any): any;
230
+ static handleStyleError(errorLevel: Loglevel, messages: StyleMessage[], options: IOptions, position: IPosition | undefined): void;
229
231
  }
230
232
  export default StyleUtil;
@@ -11,8 +11,8 @@ var _lodash = _interopRequireDefault(require("lodash"));
11
11
  var _path = _interopRequireDefault(require("path"));
12
12
  var _CompressConfig = require("../config/CompressConfig");
13
13
  var _StyleSelectorType = require("../enum/StyleSelectorType");
14
- var _StyleAttributeConfig = require("../config/vela/StyleAttributeConfig");
15
14
  var _tinycolor = _interopRequireDefault(require("tinycolor2"));
15
+ var _StyleBaseConfig = require("../config/StyleBaseConfig");
16
16
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
17
17
  /**
18
18
  * StyleUtil
@@ -181,8 +181,7 @@ class StyleUtil {
181
181
  }
182
182
  const {
183
183
  projectPath,
184
- filePath,
185
- onLog
184
+ filePath
186
185
  } = options;
187
186
  const rootDir = compilerOption.sourceRoot;
188
187
  const targetPath = _path.default.resolve(projectPath, rootDir);
@@ -196,12 +195,7 @@ class StyleUtil {
196
195
  };
197
196
  if (!assetsPath) {
198
197
  // 警告,路径值不存在
199
- onLog({
200
- filePath,
201
- level: _sharedUtils.Loglevel.WARN,
202
- position,
203
- message: 'url value is null'
204
- });
198
+ StyleUtil.handleStyleError(_sharedUtils.Loglevel.WARN, ['url value is null'], options, position);
205
199
  return '';
206
200
  }
207
201
  // 1.
@@ -210,16 +204,11 @@ class StyleUtil {
210
204
  if (_fsExtra.default.existsSync(_path.default.join(targetPath, assetsPath))) {
211
205
  return assetsPath;
212
206
  } else {
213
- onLog({
214
- filePath,
215
- level: _sharedUtils.Loglevel.ERROR,
216
- position,
217
- message: ['The absolute path ', {
218
- word: `(${assetsAbsolutePath})`
219
- }, 'to the current value ', {
220
- word: `(${assetsPath})`
221
- }, 'does not exist']
222
- });
207
+ StyleUtil.handleStyleError(_sharedUtils.Loglevel.ERROR, ['The absolute path ', {
208
+ word: `(${assetsAbsolutePath})`
209
+ }, 'to the current value ', {
210
+ word: `(${assetsPath})`
211
+ }, 'does not exist'], options, position);
223
212
  return '';
224
213
  }
225
214
  }
@@ -291,7 +280,9 @@ class StyleUtil {
291
280
  loc
292
281
  } = fallbackNode;
293
282
  parserError = {
294
- message: name,
283
+ messages: [{
284
+ word: name
285
+ }],
295
286
  details: message,
296
287
  startLine: loc?.start?.line || loc?.start?.startLine || 0,
297
288
  startCol: loc?.start?.column || loc?.start?.startCol || 0,
@@ -321,14 +312,29 @@ class StyleUtil {
321
312
  message,
322
313
  details,
323
314
  loc,
324
- css
315
+ css,
316
+ property
325
317
  } = error;
326
- if (message.includes('Unknown property') && _StyleAttributeConfig.STYLE_ATTRIBUTE_LIST.includes(_sharedUtils.StringUtil.hyphenedToCamel(error.property)) || message.includes('Invalid value for `@media` prelude')) {
318
+ let messages = [];
319
+ if (message.includes('Unknown property') && _StyleBaseConfig.STYLE_ATTRIBUTE_LIST.includes(_sharedUtils.StringUtil.hyphenedToCamel(error.property)) || message.includes('Invalid value for `@media` prelude')) {
327
320
  continue;
328
321
  }
322
+ // 优化值报错的信息,csstree-validator值报错时为Invalida value for
323
+ if (message.includes('Invalid value for')) {
324
+ // 请检查属性的值是否符合要求
325
+ messages = [`The value of attribute`, {
326
+ word: `${property}: ${css}`
327
+ }, 'is invalid.'];
328
+ css = '';
329
+ } else {
330
+ // 高亮原始报错信息
331
+ messages = [{
332
+ word: message
333
+ }];
334
+ }
329
335
  if (loc) {
330
336
  syntaxErrors.push({
331
- message,
337
+ messages,
332
338
  details,
333
339
  startLine: loc?.start?.line || 0,
334
340
  startCol: loc?.start?.column || 1,
@@ -341,7 +347,7 @@ class StyleUtil {
341
347
  } else {
342
348
  const propertyLength = error?.property?.length || 0;
343
349
  syntaxErrors.push({
344
- message,
350
+ messages,
345
351
  details,
346
352
  startLine: error.line || 0,
347
353
  startCol: error.column || 0,
@@ -567,10 +573,6 @@ class StyleUtil {
567
573
  * @returns
568
574
  */
569
575
  static translateKeyFramesRule(rule, options) {
570
- const {
571
- onLog,
572
- filePath
573
- } = options;
574
576
  const {
575
577
  prelude,
576
578
  block,
@@ -593,12 +595,8 @@ class StyleUtil {
593
595
  result.time = 100;
594
596
  break;
595
597
  default:
596
- onLog({
597
- level: _sharedUtils.Loglevel.THROW,
598
- message: `${firstName} is not a valid time value`,
599
- filePath,
600
- position
601
- });
598
+ const errorMessage = [`${firstName} is not a valid time value`];
599
+ StyleUtil.handleStyleError(_sharedUtils.Loglevel.THROW, errorMessage, options, position);
602
600
  break;
603
601
  }
604
602
  }
@@ -632,20 +630,14 @@ class StyleUtil {
632
630
  value: valueNode
633
631
  } = sourceNode;
634
632
  // 属性校验
635
- const config = _StyleAttributeConfig.STYLE_ATTRIBUTE_CONFIG[name];
633
+ const config = _StyleBaseConfig.STYLE_ATTRIBUTE_CONFIG[name];
636
634
  if (config?.validate) {
637
- const validateResult = config.validate(sourceNode);
638
- if (validateResult.message.length > 0) {
639
- const {
640
- onLog,
641
- filePath
642
- } = options;
643
- onLog({
644
- filePath: filePath,
645
- level: validateResult.level,
646
- position: sourceNode.position,
647
- message: [`Attribute verification failed,`, ...validateResult.message]
648
- });
635
+ const {
636
+ message: validMessages,
637
+ level: validInfoLevel
638
+ } = config.validate(sourceNode);
639
+ if (validMessages.length > 0) {
640
+ StyleUtil.handleStyleError(validInfoLevel, validMessages, options, sourceNode.position);
649
641
  }
650
642
  }
651
643
  if (config?.translate) {
@@ -769,5 +761,26 @@ class StyleUtil {
769
761
  }
770
762
  return value;
771
763
  }
764
+ static handleStyleError(errorLevel, messages, options, position) {
765
+ const {
766
+ onLog,
767
+ filePath,
768
+ projectPath
769
+ } = options;
770
+ // position.source存储的是相对路径,报错时要用绝对路径
771
+ let updatedPosition;
772
+ if (position) {
773
+ updatedPosition = {
774
+ ...position,
775
+ source: position.source && _path.default.resolve(projectPath, position.source)
776
+ };
777
+ }
778
+ onLog({
779
+ level: errorLevel,
780
+ message: messages,
781
+ filePath,
782
+ position: updatedPosition
783
+ });
784
+ }
772
785
  }
773
786
  var _default = exports.default = StyleUtil;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiot-toolkit/parser",
3
- "version": "2.0.6-beta.3",
3
+ "version": "2.0.6-beta.5",
4
4
  "description": "Parse the source code of aiot and convert it to the AST (Abstract Syntax Tree) of the target code.",
5
5
  "keywords": [
6
6
  "aiot",
@@ -20,7 +20,7 @@
20
20
  "test": "node ./__tests__/parser.test.js"
21
21
  },
22
22
  "dependencies": {
23
- "@aiot-toolkit/shared-utils": "2.0.6-beta.3",
23
+ "@aiot-toolkit/shared-utils": "2.0.6-beta.5",
24
24
  "@babel/core": "^7.23.6",
25
25
  "@babel/generator": "^7.24.10",
26
26
  "@babel/parser": "^7.24.8",
@@ -31,7 +31,7 @@
31
31
  "css-tree": "npm:aiot-css-tree@^2.3.1",
32
32
  "csstree-validator": "^3.0.0",
33
33
  "eslint": "^8.46.0",
34
- "file-lane": "2.0.6-beta.3",
34
+ "file-lane": "2.0.6-beta.5",
35
35
  "fs-extra": "^11.2.0",
36
36
  "google-protobuf": "^3.21.2",
37
37
  "less": "^4.2.0",
@@ -60,5 +60,5 @@
60
60
  "@types/tinycolor2": "^1.4.6",
61
61
  "babel-plugin-tester": "^11.0.4"
62
62
  },
63
- "gitHead": "2e2ba4b1d293218736343237d963fe93c5fb27a8"
63
+ "gitHead": "d40eba7eacc8334bce1e952c39365513a171719e"
64
64
  }