eslint-plugin-ember-template-lint 0.8.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,7 +4,7 @@ const templateLintConfig = {
4
4
  };
5
5
 
6
6
  function registerRule(name, config) {
7
- templateLintConfig.rules[name] = config;
7
+ templateLintConfig.rules[name] = Array.isArray(config) ? config[0] : config;
8
8
  }
9
9
 
10
10
  function registerPlugin(name) {
@@ -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
- let template = templateInfo.template;
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
- fixableIssues.filter(r => r.rule === ruleName).forEach((r, i) => {
50
- r.fix = diffs[i];
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
- const d = m.fix;
30
- m.fix.range = [d.offset, d.offset + (d.deleteText?.length || 0)];
31
- m.range = m.fix.range;
32
- const start = originalDocument.offsetToPosition(m.fix.range[0]);
33
- const end = originalDocument.offsetToPosition(m.fix.range[1]);
34
- m.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
- m.fix.range = m.fix.range.map(x => offset + x);
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 - columnOffset);
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 range = msg.range;
75
- return fixer.replaceTextRange(range, msg.fix.insertText || '');
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,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-ember-template-lint",
3
- "version": "0.8.0",
3
+ "version": "0.10.0",
4
4
  "description": "Provide linting for ember template",
5
5
  "keywords": [
6
6
  "eslint",