@aiot-toolkit/parser 2.0.5-beta.4 → 2.0.5-beta.6

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.
@@ -91,9 +91,10 @@ class StyleParser {
91
91
  validError && errors.push(validError);
92
92
  }
93
93
  });
94
+
94
95
  // 解析错误全部抛出
95
96
  errors = this.printError('parserError', errors);
96
- errors.push(..._StyleUtil.default.collectSyntaxError(CsstreeValidator(originAst, fileName)));
97
+ errors.push(..._StyleUtil.default.collectSyntaxError(CsstreeValidator(originAst, fileName), this.styleMapUtil, fileExt !== '.css', this.styleLineOffset));
97
98
  // 校验错误打印warn
98
99
  this.printError('validateError', errors);
99
100
  const cssAst = {
@@ -48,7 +48,7 @@ global.normalize = function (map) {
48
48
  if (/^\d*\.?\d*$/.test(val) && !isNaN(parseFloat(val))) {
49
49
  map[key] = +val
50
50
  // 标准尺寸
51
- map[key + 'Std'] = Math.round((map[key] * 750) / global.Env.deviceWidth)
51
+ map[key + 'Std'] = Math.round((map[key] * global.Env.designWidth) / global.Env.deviceWidth)
52
52
  }
53
53
  })
54
54
  return map
@@ -32,21 +32,28 @@ class StyleMapUtil {
32
32
  const {
33
33
  projectPath
34
34
  } = this.options;
35
- const nodePosition = this.sourceMapConsumer.originalPositionFor({
35
+ const nodeStartPosition = this.sourceMapConsumer.originalPositionFor({
36
36
  line: location.start.line,
37
37
  column: location.start.column
38
38
  });
39
- if (nodePosition.source?.endsWith('.ux') && nodePosition.line) {
40
- nodePosition.line = nodePosition.line - 1 + this.lineOffset;
39
+ const nodeEndPosition = this.sourceMapConsumer.originalPositionFor({
40
+ line: location.end.line,
41
+ column: location.end.column
42
+ });
43
+ if (nodeStartPosition.source?.endsWith('.ux') && nodeStartPosition.line) {
44
+ nodeStartPosition.line = nodeStartPosition.line - 1 + this.lineOffset;
45
+ }
46
+ if (nodeEndPosition.source?.endsWith('.ux') && nodeEndPosition.line) {
47
+ nodeEndPosition.line = nodeEndPosition.line - 1 + this.lineOffset;
41
48
  }
42
49
  return {
43
50
  pos: 0,
44
51
  end: 0,
45
- startLine: nodePosition.line || 0,
46
- startColumn: nodePosition.column || 0,
47
- endLine: nodePosition.line ? nodePosition.line + (location.end.line - location.start.line) : 0,
48
- endColumn: location.end.column - 1,
49
- source: nodePosition.source ? _path.default.resolve(projectPath, decodeURI(nodePosition.source)) : ''
52
+ startLine: nodeStartPosition.line || 0,
53
+ startColumn: nodeStartPosition.column || 0,
54
+ endLine: nodeEndPosition.line || 0,
55
+ endColumn: nodeEndPosition.column || 0,
56
+ source: nodeStartPosition.source ? _path.default.relative(projectPath, decodeURI(nodeStartPosition.source)) : ''
50
57
  };
51
58
  }
52
59
  }
@@ -104,9 +104,10 @@ declare class StyleUtil {
104
104
  /**
105
105
  * 收集CSS语法错误
106
106
  * @param errors
107
+ * @param trace 是否需要追溯到上一次sourcemap, default: false
107
108
  * @returns
108
109
  */
109
- static collectSyntaxError(errors: Array<any>): IStyleError[];
110
+ static collectSyntaxError(errors: Array<any>, instance: StyleMapUtil, trace?: boolean, startLineOffset?: number): IStyleError[];
110
111
  /**
111
112
  * 相同选择器时融合样式
112
113
  * 1. 遍历待融合列表,如果元素长度为3,判断选择器列表是否在存储列表中存在
@@ -312,9 +312,12 @@ class StyleUtil {
312
312
  /**
313
313
  * 收集CSS语法错误
314
314
  * @param errors
315
+ * @param trace 是否需要追溯到上一次sourcemap, default: false
315
316
  * @returns
316
317
  */
317
- static collectSyntaxError(errors) {
318
+ static collectSyntaxError(errors, instance) {
319
+ let trace = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
320
+ let startLineOffset = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
318
321
  let syntaxErrors = [];
319
322
  for (let i = 0; i < errors.length; i++) {
320
323
  const error = errors[i];
@@ -328,30 +331,85 @@ class StyleUtil {
328
331
  continue;
329
332
  }
330
333
  if (loc) {
331
- syntaxErrors.push({
332
- message,
333
- details,
334
- startLine: loc?.start?.line || 0,
335
- startCol: loc?.start?.column || 0,
336
- startOffset: loc?.start?.offset || 0,
337
- endLine: loc?.start?.line || 0,
338
- endCol: loc?.start?.column || 0,
339
- endOffset: loc?.end?.offset || 0,
340
- source: css
341
- });
334
+ if (!trace) {
335
+ syntaxErrors.push({
336
+ message,
337
+ details,
338
+ startLine: loc?.start?.line || 0,
339
+ startCol: (loc?.start?.column || 1) - 1,
340
+ startOffset: loc?.start?.offset || 0,
341
+ endLine: loc?.start?.line || 0,
342
+ endCol: (loc?.start?.column || 1) - 1,
343
+ endOffset: loc?.end?.offset || 0,
344
+ source: css
345
+ });
346
+ } else {
347
+ const transformedLoc = instance.transfromLocToPosition({
348
+ start: {
349
+ line: loc.start.line,
350
+ column: loc.start.column,
351
+ offset: 0
352
+ },
353
+ end: {
354
+ line: loc.start.line,
355
+ column: loc.start.column,
356
+ offset: 0
357
+ },
358
+ source: css
359
+ });
360
+ syntaxErrors.push({
361
+ message,
362
+ details,
363
+ startLine: transformedLoc?.startLine - startLineOffset + 1,
364
+ startCol: transformedLoc?.startColumn,
365
+ startOffset: transformedLoc?.pos,
366
+ endLine: transformedLoc?.endLine - startLineOffset + 1,
367
+ endCol: transformedLoc?.endColumn,
368
+ endOffset: transformedLoc?.end,
369
+ source: css
370
+ });
371
+ }
342
372
  } else {
343
373
  const propertyLength = error?.property?.length || 0;
344
- syntaxErrors.push({
345
- message,
346
- details,
347
- startLine: error.line || 0,
348
- startCol: error.column || 0,
349
- startOffset: error.offset || 0,
350
- endLine: error.line || 0,
351
- endCol: (error.column || 0) + propertyLength,
352
- endOffset: (error.offset || 0) + propertyLength,
353
- source: css
354
- });
374
+ if (!trace) {
375
+ syntaxErrors.push({
376
+ message,
377
+ details,
378
+ startLine: error.line || 0,
379
+ startCol: error.column || 0,
380
+ startOffset: error.offset || 0,
381
+ endLine: error.line || 0,
382
+ endCol: (error.column || 0) + propertyLength - 1,
383
+ endOffset: (error.offset || 0) + propertyLength - 1,
384
+ source: css
385
+ });
386
+ } else {
387
+ const transformedLoc = instance.transfromLocToPosition({
388
+ start: {
389
+ line: error.line,
390
+ column: error.column,
391
+ offset: 0
392
+ },
393
+ end: {
394
+ line: error.line,
395
+ column: error.column,
396
+ offset: 0
397
+ },
398
+ source: css
399
+ });
400
+ syntaxErrors.push({
401
+ message,
402
+ details,
403
+ startLine: transformedLoc?.startLine,
404
+ startCol: transformedLoc?.startColumn,
405
+ startOffset: transformedLoc?.pos,
406
+ endLine: transformedLoc?.endLine,
407
+ endCol: transformedLoc?.endColumn,
408
+ endOffset: transformedLoc?.end,
409
+ source: css
410
+ });
411
+ }
412
+ continue;
355
413
  }
356
414
  }
357
415
  return syntaxErrors;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiot-toolkit/parser",
3
- "version": "2.0.5-beta.4",
3
+ "version": "2.0.5-beta.6",
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,8 +20,8 @@
20
20
  "test": "node ./__tests__/parser.test.js"
21
21
  },
22
22
  "dependencies": {
23
- "@aiot-toolkit/generator": "2.0.5-beta.4",
24
- "@aiot-toolkit/shared-utils": "2.0.5-beta.4",
23
+ "@aiot-toolkit/generator": "2.0.5-beta.6",
24
+ "@aiot-toolkit/shared-utils": "2.0.5-beta.6",
25
25
  "@babel/core": "^7.23.6",
26
26
  "@babel/generator": "^7.24.10",
27
27
  "@babel/parser": "^7.24.8",
@@ -33,7 +33,7 @@
33
33
  "css-tree": "npm:aiot-css-tree@^2.3.1",
34
34
  "csstree-validator": "^3.0.0",
35
35
  "eslint": "^8.46.0",
36
- "file-lane": "2.0.5-beta.4",
36
+ "file-lane": "2.0.5-beta.6",
37
37
  "fs-extra": "^11.2.0",
38
38
  "google-protobuf": "^3.21.2",
39
39
  "less": "^4.2.0",
@@ -60,5 +60,5 @@
60
60
  "@types/reserved-words": "^0.1.4",
61
61
  "babel-plugin-tester": "^11.0.4"
62
62
  },
63
- "gitHead": "cfa77c8f2d3916b703ac23a93a9dcc0df1bc261a"
63
+ "gitHead": "84131732e5cea68517c787f2bdc9acdb997530bb"
64
64
  }