auto-cr-rules 2.0.33 → 2.0.34

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.
@@ -4,9 +4,12 @@ exports.noDeepRelativeImports = void 0;
4
4
  var types_1 = require("../types");
5
5
  var MAX_DEPTH = 2;
6
6
  exports.noDeepRelativeImports = (0, types_1.defineRule)('no-deep-relative-imports', { tag: 'base', severity: types_1.RuleSeverity.Warning }, function (_a) {
7
- var helpers = _a.helpers, messages = _a.messages, language = _a.language;
8
- for (var _i = 0, _b = helpers.imports; _i < _b.length; _i++) {
9
- var reference = _b[_i];
7
+ var _b, _c;
8
+ var ast = _a.ast, helpers = _a.helpers, messages = _a.messages, language = _a.language, source = _a.source;
9
+ var moduleStart = (_c = (_b = ast.span) === null || _b === void 0 ? void 0 : _b.start) !== null && _c !== void 0 ? _c : 0;
10
+ var lineIndex = buildLineIndex(source);
11
+ for (var _i = 0, _d = helpers.imports; _i < _d.length; _i++) {
12
+ var reference = _d[_i];
10
13
  if (!helpers.isRelativePath(reference.value)) {
11
14
  continue;
12
15
  }
@@ -22,12 +25,76 @@ exports.noDeepRelativeImports = (0, types_1.defineRule)('no-deep-relative-import
22
25
  { text: 'Use a path alias (for example: @shared/deep/utils).' },
23
26
  { text: 'Create an index file at a higher level to re-export the module and shorten the import.' },
24
27
  ];
28
+ var line = reference.span
29
+ ? resolveLine(lineIndex, bytePosToCharIndex(source, moduleStart, reference.span.start))
30
+ : undefined;
25
31
  helpers.reportViolation({
26
32
  description: description,
27
33
  code: reference.value,
28
34
  suggestions: suggestions,
29
35
  span: reference.span,
30
- });
36
+ line: line,
37
+ }, reference.span);
31
38
  }
32
39
  }
33
40
  });
41
+ var buildLineIndex = function (source) {
42
+ var offsets = [0];
43
+ for (var index = 0; index < source.length; index += 1) {
44
+ if (source[index] === '\n') {
45
+ offsets.push(index + 1);
46
+ }
47
+ }
48
+ return { offsets: offsets };
49
+ };
50
+ var resolveLine = function (_a, position) {
51
+ var offsets = _a.offsets;
52
+ var low = 0;
53
+ var high = offsets.length - 1;
54
+ while (low <= high) {
55
+ var mid = Math.floor((low + high) / 2);
56
+ var current = offsets[mid];
57
+ if (current === position) {
58
+ return mid + 1;
59
+ }
60
+ if (current < position) {
61
+ low = mid + 1;
62
+ }
63
+ else {
64
+ high = mid - 1;
65
+ }
66
+ }
67
+ return high + 1;
68
+ };
69
+ var readUtf8Character = function (source, index, code) {
70
+ if (code <= 0x7f) {
71
+ return { bytes: 1, nextIndex: index + 1 };
72
+ }
73
+ if (code <= 0x7ff) {
74
+ return { bytes: 2, nextIndex: index + 1 };
75
+ }
76
+ if (code >= 0xd800 && code <= 0xdbff && index + 1 < source.length) {
77
+ var next = source.charCodeAt(index + 1);
78
+ if (next >= 0xdc00 && next <= 0xdfff) {
79
+ return { bytes: 4, nextIndex: index + 2 };
80
+ }
81
+ }
82
+ return { bytes: 3, nextIndex: index + 1 };
83
+ };
84
+ var bytePosToCharIndex = function (source, moduleStart, bytePos) {
85
+ if (bytePos <= moduleStart) {
86
+ return 0;
87
+ }
88
+ var index = 0;
89
+ var byteOffset = moduleStart;
90
+ while (index < source.length) {
91
+ var code = source.charCodeAt(index);
92
+ var _a = readUtf8Character(source, index, code), bytes = _a.bytes, nextIndex = _a.nextIndex;
93
+ if (byteOffset + bytes > bytePos) {
94
+ return index;
95
+ }
96
+ byteOffset += bytes;
97
+ index = nextIndex;
98
+ }
99
+ return source.length;
100
+ };
@@ -65,7 +65,10 @@ var visitTryStatements = function (ast, callback) {
65
65
  }
66
66
  };
67
67
  exports.noSwallowedErrors = (0, types_1.defineRule)('no-swallowed-errors', { tag: 'base', severity: types_1.RuleSeverity.Warning }, function (_a) {
68
- var ast = _a.ast, helpers = _a.helpers, messages = _a.messages;
68
+ var _b, _c;
69
+ var ast = _a.ast, helpers = _a.helpers, messages = _a.messages, source = _a.source;
70
+ var moduleStart = (_c = (_b = ast.span) === null || _b === void 0 ? void 0 : _b.start) !== null && _c !== void 0 ? _c : 0;
71
+ var lineIndex = buildLineIndex(source);
69
72
  visitTryStatements(ast, function (statement) {
70
73
  if (statement.type !== 'TryStatement') {
71
74
  return;
@@ -76,20 +79,82 @@ exports.noSwallowedErrors = (0, types_1.defineRule)('no-swallowed-errors', { tag
76
79
  }
77
80
  var body = handler.body;
78
81
  var statements = body.stmts;
79
- if (statements.length === 0) {
82
+ var report = function () {
83
+ var line = resolveLine(lineIndex, bytePosToCharIndex(source, moduleStart, body.span.start));
80
84
  helpers.reportViolation({
81
85
  description: messages.swallowedError(),
86
+ line: line,
82
87
  span: body.span,
83
- });
88
+ }, body.span);
89
+ };
90
+ if (statements.length === 0) {
91
+ report();
84
92
  return;
85
93
  }
86
94
  var hasThrow = containsThrowStatement(statements);
87
95
  var hasLogging = hasLoggingCall(statements);
88
96
  if (!hasThrow && !hasLogging) {
89
- helpers.reportViolation({
90
- description: messages.swallowedError(),
91
- span: body.span,
92
- });
97
+ report();
93
98
  }
94
99
  });
95
100
  });
101
+ var buildLineIndex = function (source) {
102
+ var offsets = [0];
103
+ for (var index = 0; index < source.length; index += 1) {
104
+ if (source[index] === '\n') {
105
+ offsets.push(index + 1);
106
+ }
107
+ }
108
+ return { offsets: offsets };
109
+ };
110
+ var resolveLine = function (_a, position) {
111
+ var offsets = _a.offsets;
112
+ var low = 0;
113
+ var high = offsets.length - 1;
114
+ while (low <= high) {
115
+ var mid = Math.floor((low + high) / 2);
116
+ var current = offsets[mid];
117
+ if (current === position) {
118
+ return mid + 1;
119
+ }
120
+ if (current < position) {
121
+ low = mid + 1;
122
+ }
123
+ else {
124
+ high = mid - 1;
125
+ }
126
+ }
127
+ return high + 1;
128
+ };
129
+ var readUtf8Character = function (source, index, code) {
130
+ if (code <= 0x7f) {
131
+ return { bytes: 1, nextIndex: index + 1 };
132
+ }
133
+ if (code <= 0x7ff) {
134
+ return { bytes: 2, nextIndex: index + 1 };
135
+ }
136
+ if (code >= 0xd800 && code <= 0xdbff && index + 1 < source.length) {
137
+ var next = source.charCodeAt(index + 1);
138
+ if (next >= 0xdc00 && next <= 0xdfff) {
139
+ return { bytes: 4, nextIndex: index + 2 };
140
+ }
141
+ }
142
+ return { bytes: 3, nextIndex: index + 1 };
143
+ };
144
+ var bytePosToCharIndex = function (source, moduleStart, bytePos) {
145
+ if (bytePos <= moduleStart) {
146
+ return 0;
147
+ }
148
+ var index = 0;
149
+ var byteOffset = moduleStart;
150
+ while (index < source.length) {
151
+ var code = source.charCodeAt(index);
152
+ var _a = readUtf8Character(source, index, code), bytes = _a.bytes, nextIndex = _a.nextIndex;
153
+ if (byteOffset + bytes > bytePos) {
154
+ return index;
155
+ }
156
+ byteOffset += bytes;
157
+ index = nextIndex;
158
+ }
159
+ return source.length;
160
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "auto-cr-rules",
3
- "version": "2.0.33",
3
+ "version": "2.0.34",
4
4
  "description": "Extensible static analysis rule set for the auto-cr automated code review toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/types/index.d.ts",