ember-eslint-parser 0.7.0 → 0.9.0

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,8 @@
1
- const fs = require('node:fs');
2
- const { transformForLint } = require('./transforms');
3
- const { replaceRange } = require('./transforms');
1
+ import fs from 'node:fs';
2
+ import { createRequire } from 'node:module';
3
+ import { transformForLint, replaceRange } from './transforms.js';
4
+
5
+ const require = createRequire(import.meta.url);
4
6
 
5
7
  let patchTs, replaceExtensions, syncMtsGtsSourceFiles, typescriptParser, isPatched, allowGjs;
6
8
 
@@ -80,12 +82,32 @@ try {
80
82
  let jsCode = code;
81
83
  const sourceFile = ts.createSourceFile('__x__.ts', code, ts.ScriptTarget.Latest);
82
84
  const length = jsCode.length;
83
- for (const b of sourceFile.statements) {
84
- if (b.kind === ts.SyntaxKind.ImportDeclaration && b.moduleSpecifier.text.endsWith('.gts')) {
85
- const value = b.moduleSpecifier.text.replace(/\.gts$/, '.mts');
86
- jsCode = replaceRange(jsCode, b.moduleSpecifier.pos + 2, b.moduleSpecifier.end - 1, value);
85
+ function visit(node) {
86
+ if (
87
+ node.kind === ts.SyntaxKind.ImportDeclaration &&
88
+ node.moduleSpecifier.text.endsWith('.gts')
89
+ ) {
90
+ const value = node.moduleSpecifier.text.replace(/\.gts$/, '.mts');
91
+ jsCode = replaceRange(
92
+ jsCode,
93
+ node.moduleSpecifier.pos + 2,
94
+ node.moduleSpecifier.end - 1,
95
+ value
96
+ );
97
+ }
98
+ if (
99
+ node.kind === ts.SyntaxKind.CallExpression &&
100
+ node.expression.kind === ts.SyntaxKind.ImportKeyword
101
+ ) {
102
+ const arg = node.arguments[0];
103
+ if (arg && arg.kind === ts.SyntaxKind.StringLiteral && arg.text.endsWith('.gts')) {
104
+ const value = arg.text.replace(/\.gts$/, '.mts');
105
+ jsCode = replaceRange(jsCode, arg.getStart(sourceFile) + 1, arg.end - 1, value); // +1/-1 to skip surrounding quotes
106
+ }
87
107
  }
108
+ ts.forEachChild(node, visit);
88
109
  }
110
+ visit(sourceFile);
89
111
  if (length !== jsCode.length) {
90
112
  throw new Error('bad replacement');
91
113
  }
@@ -142,9 +164,4 @@ try {
142
164
  syncMtsGtsSourceFiles = () => null;
143
165
  }
144
166
 
145
- module.exports = {
146
- patchTs,
147
- replaceExtensions,
148
- syncMtsGtsSourceFiles,
149
- typescriptParser,
150
- };
167
+ export { patchTs, replaceExtensions, syncMtsGtsSourceFiles, typescriptParser };
@@ -9,24 +9,27 @@
9
9
 
10
10
  const parsedFiles = new Set();
11
11
 
12
- module.exports = {
13
- registerParsedFile(f) {
14
- parsedFiles.add(f);
15
- },
16
- preprocess: undefined,
17
- postprocess: (messages, fileName) => {
18
- const msgs = messages.flat();
19
- if (!parsedFiles.has(fileName)) {
20
- msgs[0] = msgs[0] || {
21
- message: '',
22
- };
23
- msgs[0].message += '\n';
24
- msgs[0].message +=
25
- 'To lint Gjs/Gts files please follow the setup guide at https://github.com/ember-cli/eslint-plugin-ember#gtsgjs' +
26
- '\nNote that this error can also happen if you have multiple versions of eslint-plugin-ember in your node_modules';
27
- }
28
- parsedFiles.delete(fileName); // required for tests
29
- return msgs;
30
- },
31
- supportsAutofix: true,
12
+ export function registerParsedFile(f) {
13
+ parsedFiles.add(f);
14
+ }
15
+
16
+ export const preprocess = undefined;
17
+
18
+ export const postprocess = (messages, fileName) => {
19
+ const msgs = messages.flat();
20
+ if (!parsedFiles.has(fileName)) {
21
+ msgs[0] = msgs[0] || {
22
+ message: '',
23
+ };
24
+ msgs[0].message += '\n';
25
+ msgs[0].message +=
26
+ 'To lint Gjs/Gts files please follow the setup guide at https://github.com/ember-cli/eslint-plugin-ember#gtsgjs' +
27
+ '\nNote that this error can also happen if you have multiple versions of eslint-plugin-ember in your node_modules';
28
+ }
29
+ parsedFiles.delete(fileName); // required for tests
30
+ return msgs;
32
31
  };
32
+
33
+ export const supportsAutofix = true;
34
+
35
+ export default { registerParsedFile, preprocess, postprocess, supportsAutofix };
@@ -1,101 +0,0 @@
1
- /**
2
- * @typedef {{ line: number; column: number }} Position
3
- */
4
-
5
- // Helper class to convert line/column from and to offset
6
- // taken and adapt from https://github.com/typed-ember/glint/blob/main/packages/core/src/language-server/util/position.ts
7
- class DocumentLines {
8
- /**
9
- * @param {string} contents
10
- */
11
- constructor(contents) {
12
- this.lineStarts = computeLineStarts(contents);
13
- }
14
-
15
- /**
16
- * @param {Position} position
17
- * @return {number}
18
- */
19
- positionToOffset(position) {
20
- const { line, column } = position;
21
- return this.lineStarts[line - 1] + column;
22
- }
23
-
24
- /**
25
- *
26
- * @param {number} position
27
- * @return {Position}
28
- */
29
- offsetToPosition(position) {
30
- const lineStarts = this.lineStarts;
31
- let line = 0;
32
- while (line + 1 < lineStarts.length && lineStarts[line + 1] <= position) {
33
- line++;
34
- }
35
- const column = position - lineStarts[line];
36
- return { line: line + 1, column };
37
- }
38
- }
39
-
40
- /**
41
- * @returns {number[]}
42
- * @param {string} text
43
- */
44
- function computeLineStarts(text) {
45
- const result = [];
46
- let pos = 0;
47
- let lineStart = 0;
48
- while (pos < text.length) {
49
- const ch = text.codePointAt(pos);
50
- pos++;
51
- switch (ch) {
52
- case 13 /* carriageReturn */: {
53
- if (text.codePointAt(pos) === 10 /* lineFeed */) {
54
- pos++;
55
- }
56
- }
57
- // falls through
58
- case 10 /* lineFeed */: {
59
- result.push(lineStart);
60
- lineStart = pos;
61
- break;
62
- }
63
- default: {
64
- if (ch > 127 /* maxAsciiCharacter */ && isLineBreak(ch)) {
65
- result.push(lineStart);
66
- lineStart = pos;
67
- }
68
- break;
69
- }
70
- }
71
- }
72
- result.push(lineStart);
73
- return result;
74
- }
75
-
76
- /* istanbul ignore next */
77
- /**
78
- * @param {number} ch
79
- * @return {boolean}
80
- */
81
- function isLineBreak(ch) {
82
- // ES5 7.3:
83
- // The ECMAScript line terminator characters are listed in Table 3.
84
- // Table 3: Line Terminator Characters
85
- // Code Unit Value Name Formal Name
86
- // \u000A Line Feed <LF>
87
- // \u000D Carriage Return <CR>
88
- // \u2028 Line separator <LS>
89
- // \u2029 Paragraph separator <PS>
90
- // Only the characters in Table 3 are treated as line terminators. Other new line or line
91
- // breaking characters are treated as white space but not as line terminators.
92
- return (
93
- ch === 10 /* lineFeed */ ||
94
- ch === 13 /* carriageReturn */ ||
95
- ch === 8232 /* lineSeparator */ ||
96
- ch === 8233 /* paragraphSeparator */
97
- );
98
- }
99
-
100
- module.exports = DocumentLines;
101
- module.exports.isLineBreak = isLineBreak;