eslint-plugin-ember-template-lint 0.7.0 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE.md +1 -1
- package/README.md +11 -2
- package/lib/parser/hbs-parser.js +1 -0
- package/lib/rules/hbs-worker.js +26 -6
- package/lib/rules/lint.js +19 -18
- package/package.json +1 -1
package/LICENSE.md
CHANGED
package/README.md
CHANGED
@@ -1,6 +1,15 @@
|
|
1
1
|
# eslint-plugin-hbs-template
|
2
2
|
|
3
|
-
Provide linting for hbs templates files
|
3
|
+
Provide linting (via [ember-template-lint](https://github.com/ember-template-lint/ember-template-lint)) for hbs templates files, template string literals and gts,gjs
|
4
|
+
* no extra config file needed
|
5
|
+
|
6
|
+
* no need to run extra tool for linting
|
7
|
+
|
8
|
+
* no need to run extra tool to fix lint issues
|
9
|
+
|
10
|
+
* no need to implement & maintain editor support for ember-template-lint
|
11
|
+
|
12
|
+
* it also has the advantage that it can know the scope for some template-lint rules, like no-implicit-this, and remove those messages
|
4
13
|
|
5
14
|
## Installation
|
6
15
|
|
@@ -20,7 +29,7 @@ $ npm install eslint-plugin-ember-template-lint --save-dev
|
|
20
29
|
|
21
30
|
## Usage
|
22
31
|
|
23
|
-
Add `
|
32
|
+
Add `ember-template-lint` to the plugins section of your `.eslintrc` configuration file. You can omit the `eslint-plugin-` prefix:
|
24
33
|
|
25
34
|
### 2. Modify your `.eslintrc.js`
|
26
35
|
|
package/lib/parser/hbs-parser.js
CHANGED
package/lib/rules/hbs-worker.js
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
const { runAsWorker } = require('synckit');
|
2
2
|
const { generateDifferences } = require('prettier-linter-helpers');
|
3
|
+
const Document = require('../utils/document');
|
3
4
|
|
4
5
|
async function _applyFixes(options, results, columnOffset) {
|
5
6
|
const { transform } = await import('ember-template-recast');
|
@@ -16,6 +17,18 @@ async function _applyFixes(options, results, columnOffset) {
|
|
16
17
|
let ruleNames = new Set(fixableIssues.map((r) => r.rule));
|
17
18
|
const spaces = ' '.repeat(columnOffset);
|
18
19
|
|
20
|
+
let template = currentSource;
|
21
|
+
if (columnOffset) {
|
22
|
+
template = spaces + template.split('\n').join('\n' + spaces);
|
23
|
+
}
|
24
|
+
const origDoc = new Document(template);
|
25
|
+
fixableIssues.forEach((r) => {
|
26
|
+
r.range = [
|
27
|
+
origDoc.positionToOffset({line: r.line - 1, character: r.column}),
|
28
|
+
origDoc.positionToOffset({line: r.endLine - 1, character: r.endColumn}),
|
29
|
+
];
|
30
|
+
});
|
31
|
+
|
19
32
|
for (let ruleName of ruleNames) {
|
20
33
|
let templateInfos = [{
|
21
34
|
template: currentSource,
|
@@ -23,6 +36,12 @@ async function _applyFixes(options, results, columnOffset) {
|
|
23
36
|
isStrictMode: false
|
24
37
|
}];
|
25
38
|
|
39
|
+
const diffContained = (d, range) => d.range[0] >= range[0] && d.range[1] <= range[1];
|
40
|
+
const diffIntersect = (d, range) =>
|
41
|
+
(d.range[1] >= range[0] && d.range[1] <= range[1])
|
42
|
+
||
|
43
|
+
(d.range[0] >= range[0] && d.range[0] <= range[1]);
|
44
|
+
|
26
45
|
for (let templateInfo of templateInfos) {
|
27
46
|
|
28
47
|
let rule = this._buildRule(ruleName, {
|
@@ -38,16 +57,17 @@ async function _applyFixes(options, results, columnOffset) {
|
|
38
57
|
let { code } = transform(templateInfo.template, () => visitor);
|
39
58
|
|
40
59
|
if (code !== templateInfo.template) {
|
41
|
-
|
42
|
-
if (columnOffset) {
|
43
|
-
template = spaces + template.split('\n').join('\n' + spaces);
|
44
|
-
}
|
60
|
+
|
45
61
|
if (columnOffset) {
|
46
62
|
code = spaces + code.split('\n').join('\n' + spaces);
|
47
63
|
}
|
48
64
|
const diffs = generateDifferences(template, code);
|
49
|
-
|
50
|
-
|
65
|
+
diffs.forEach((d) => {
|
66
|
+
d.range = [d.offset, d.offset + (d.deleteText?.length || 0)];
|
67
|
+
});
|
68
|
+
fixableIssues.filter(r => r.rule === ruleName).forEach((r) => {
|
69
|
+
const range = r.range;
|
70
|
+
r.fix = diffs.filter(d => diffContained(d, range) || diffIntersect(d, range));
|
51
71
|
});
|
52
72
|
}
|
53
73
|
}
|
package/lib/rules/lint.js
CHANGED
@@ -26,22 +26,23 @@ function runTemplateLint(node, context) {
|
|
26
26
|
const lintMessages = response.messages;
|
27
27
|
lintMessages.forEach((m) => {
|
28
28
|
if (m.fix) {
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
29
|
+
m.fix.forEach((fix) => {
|
30
|
+
const d = fix;
|
31
|
+
fix.range = [d.offset, d.offset + (d.deleteText?.length || 0)];
|
32
|
+
const start = originalDocument.offsetToPosition(fix.range[0]);
|
33
|
+
const end = originalDocument.offsetToPosition(fix.range[1]);
|
34
|
+
fix.range = [
|
35
|
+
originalDocument.positionToOffset({
|
36
|
+
line: start.line,
|
37
|
+
character: start.character
|
38
|
+
}),
|
39
|
+
originalDocument.positionToOffset({
|
40
|
+
line: end.line,
|
41
|
+
character: end.character
|
42
|
+
})
|
43
|
+
];
|
44
|
+
fix.range = fix.range.map(x => offset + x);
|
45
|
+
});
|
45
46
|
}
|
46
47
|
m.range = [
|
47
48
|
originalDocument.positionToOffset({
|
@@ -71,8 +72,8 @@ function runTemplateLint(node, context) {
|
|
71
72
|
if (!msg.isFixable || !msg.fix) {
|
72
73
|
return null;
|
73
74
|
}
|
74
|
-
const
|
75
|
-
return fixer.replaceTextRange(range,
|
75
|
+
const fixes = msg.fix;
|
76
|
+
return fixes.map(fix => fixer.replaceTextRange(fix.range, fix.insertText || ''));
|
76
77
|
},
|
77
78
|
loc: { start, end },
|
78
79
|
message: msg.message,
|