@astrojs/ts-plugin 0.1.1 → 0.3.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.
@@ -8,116 +8,100 @@ export type FileMapping = LineMapping[];
8
8
  type LineMapping = CharacterMapping[]; // FileMapping[generated_line_index] = LineMapping
9
9
 
10
10
  type CharacterMapping = [
11
- number, // generated character
12
- number, // original file
13
- number, // original line
14
- number // original index
11
+ number, // generated character
12
+ number, // original file
13
+ number, // original line
14
+ number // original index
15
15
  ];
16
16
 
17
- type ReorderedChar = [
18
- original_character: number,
19
- generated_line: number,
20
- generated_character: number
21
- ];
17
+ type ReorderedChar = [original_character: number, generated_line: number, generated_character: number];
22
18
 
23
19
  interface ReorderedMap {
24
- [original_line: number]: ReorderedChar[];
20
+ [original_line: number]: ReorderedChar[];
25
21
  }
26
22
 
27
23
  function binaryInsert(array: number[], value: number): void;
28
- function binaryInsert<T extends Record<any, number> | number[]>(
29
- array: T[],
30
- value: T,
31
- key: keyof T
32
- ): void;
24
+ function binaryInsert<T extends Record<any, number> | number[]>(array: T[], value: T, key: keyof T): void;
33
25
  function binaryInsert<A extends Array<Record<any, number>> | number[]>(
34
- array: A,
35
- value: A[any],
36
- key?: keyof (A[any] & object)
26
+ array: A,
27
+ value: A[any],
28
+ key?: keyof (A[any] & object)
37
29
  ) {
38
- if (0 === key) key = '0' as keyof A[any];
39
- const index = 1 + binarySearch(array, (key ? value[key] : value) as number, key);
40
- let i = array.length;
41
- while (index !== i--) array[1 + i] = array[i];
42
- array[index] = value;
30
+ if (0 === key) key = '0' as keyof A[any];
31
+ const index = 1 + binarySearch(array, (key ? value[key] : value) as number, key);
32
+ let i = array.length;
33
+ while (index !== i--) array[1 + i] = array[i];
34
+ array[index] = value;
43
35
  }
44
36
 
45
- function binarySearch<T extends object | number>(
46
- array: T[],
47
- target: number,
48
- key?: keyof (T & object)
49
- ) {
50
- if (!array || 0 === array.length) return -1;
51
- if (0 === key) key = '0' as keyof T;
52
- let low = 0;
53
- let high = array.length - 1;
54
- while (low <= high) {
55
- const i = low + ((high - low) >> 1);
56
- const item = undefined === key ? array[i] : array[i][key];
57
- if (item === target) return i;
58
- if (item < target) low = i + 1;
59
- else high = i - 1;
60
- }
61
- if ((low = ~low) < 0) low = ~low - 1;
62
- return low;
37
+ function binarySearch<T extends object | number>(array: T[], target: number, key?: keyof (T & object)) {
38
+ if (!array || 0 === array.length) return -1;
39
+ if (0 === key) key = '0' as keyof T;
40
+ let low = 0;
41
+ let high = array.length - 1;
42
+ while (low <= high) {
43
+ const i = low + ((high - low) >> 1);
44
+ const item = undefined === key ? array[i] : array[i][key];
45
+ if (item === target) return i;
46
+ if (item < target) low = i + 1;
47
+ else high = i - 1;
48
+ }
49
+ if ((low = ~low) < 0) low = ~low - 1;
50
+ return low;
63
51
  }
64
52
 
65
53
  export class SourceMapper {
66
- private mappings: FileMapping;
67
- private reverseMappings?: ReorderedMap;
68
-
69
- constructor(mappings: FileMapping | string) {
70
- if (typeof mappings === 'string') this.mappings = decode(mappings) as FileMapping;
71
- else this.mappings = mappings;
72
- }
73
-
74
- getOriginalPosition(position: LineChar): LineChar {
75
- const lineMap = this.mappings[position.line];
76
- if (!lineMap) {
77
- return { line: -1, character: -1 };
78
- }
79
-
80
- const closestMatch = binarySearch(lineMap, position.character, 0);
81
- const match = lineMap[closestMatch];
82
- if (!match) {
83
- return { line: -1, character: -1 };
84
- }
85
-
86
- const { 2: line, 3: character } = match;
87
- return { line, character };
88
- }
89
-
90
- getGeneratedPosition(position: LineChar): LineChar {
91
- if (!this.reverseMappings) this.computeReversed();
92
- const lineMap = this.reverseMappings![position.line];
93
- if (!lineMap) {
94
- return { line: -1, character: -1 };
95
- }
96
-
97
- const closestMatch = binarySearch(lineMap, position.character, 0);
98
- const match = lineMap[closestMatch];
99
- if (!match) {
100
- return { line: -1, character: -1 };
101
- }
102
-
103
- const { 1: line, 2: character } = match;
104
- return { line, character };
105
- }
106
-
107
- private computeReversed() {
108
- this.reverseMappings = {} as ReorderedMap;
109
- for (let generated_line = 0; generated_line !== this.mappings.length; generated_line++) {
110
- for (const { 0: generated_index, 2: original_line, 3: original_character_index } of this
111
- .mappings[generated_line]) {
112
- const reordered_char: ReorderedChar = [
113
- original_character_index,
114
- generated_line,
115
- generated_index
116
- ];
117
- if (original_line in this.reverseMappings)
118
- binaryInsert(this.reverseMappings[original_line], reordered_char, 0);
119
- else this.reverseMappings[original_line] = [reordered_char];
120
- }
121
- }
122
- }
123
- }
54
+ private mappings: FileMapping;
55
+ private reverseMappings?: ReorderedMap;
56
+
57
+ constructor(mappings: FileMapping | string) {
58
+ if (typeof mappings === 'string') this.mappings = decode(mappings) as FileMapping;
59
+ else this.mappings = mappings;
60
+ }
61
+
62
+ getOriginalPosition(position: LineChar): LineChar {
63
+ const lineMap = this.mappings[position.line];
64
+ if (!lineMap) {
65
+ return { line: -1, character: -1 };
66
+ }
67
+
68
+ const closestMatch = binarySearch(lineMap, position.character, 0);
69
+ const match = lineMap[closestMatch];
70
+ if (!match) {
71
+ return { line: -1, character: -1 };
72
+ }
73
+
74
+ const { 2: line, 3: character } = match;
75
+ return { line, character };
76
+ }
77
+
78
+ getGeneratedPosition(position: LineChar): LineChar {
79
+ if (!this.reverseMappings) this.computeReversed();
80
+ const lineMap = this.reverseMappings![position.line];
81
+ if (!lineMap) {
82
+ return { line: -1, character: -1 };
83
+ }
84
+
85
+ const closestMatch = binarySearch(lineMap, position.character, 0);
86
+ const match = lineMap[closestMatch];
87
+ if (!match) {
88
+ return { line: -1, character: -1 };
89
+ }
90
+
91
+ const { 1: line, 2: character } = match;
92
+ return { line, character };
93
+ }
94
+
95
+ private computeReversed() {
96
+ this.reverseMappings = {} as ReorderedMap;
97
+ for (let generated_line = 0; generated_line !== this.mappings.length; generated_line++) {
98
+ for (const { 0: generated_index, 2: original_line, 3: original_character_index } of this.mappings[
99
+ generated_line
100
+ ]) {
101
+ const reordered_char: ReorderedChar = [original_character_index, generated_line, generated_index];
102
+ if (original_line in this.reverseMappings) binaryInsert(this.reverseMappings[original_line], reordered_char, 0);
103
+ else this.reverseMappings[original_line] = [reordered_char];
104
+ }
105
+ }
106
+ }
107
+ }
package/src/utils.ts CHANGED
@@ -1,66 +1,66 @@
1
1
  export function isAstroFilePath(filePath: string) {
2
- return filePath.endsWith('.astro');
2
+ return filePath.endsWith('.astro');
3
3
  }
4
4
 
5
5
  export function isVirtualAstroFilePath(filePath: string) {
6
- return filePath.endsWith('.astro.ts');
6
+ return filePath.endsWith('.astro.ts');
7
7
  }
8
8
 
9
9
  export function toRealAstroFilePath(filePath: string) {
10
- return filePath.slice(0, -'.ts'.length);
10
+ return filePath.slice(0, -'.ts'.length);
11
11
  }
12
12
 
13
13
  export function ensureRealAstroFilePath(filePath: string) {
14
- return isVirtualAstroFilePath(filePath) ? toRealAstroFilePath(filePath) : filePath;
14
+ return isVirtualAstroFilePath(filePath) ? toRealAstroFilePath(filePath) : filePath;
15
15
  }
16
16
 
17
17
  export function isNotNullOrUndefined<T>(val: T | undefined | null): val is T {
18
- return val !== undefined && val !== null;
18
+ return val !== undefined && val !== null;
19
19
  }
20
20
 
21
21
  /**
22
- * Checks if this a section that should be completely ignored
23
- * because it's purely generated.
24
- */
22
+ * Checks if this a section that should be completely ignored
23
+ * because it's purely generated.
24
+ */
25
25
  export function isInGeneratedCode(text: string, start: number, end: number) {
26
- const lineStart = text.lastIndexOf('\n', start);
27
- const lineEnd = text.indexOf('\n', end);
28
- const lastStart = text.substring(lineStart, start).lastIndexOf('/*Ωignore_startΩ*/');
29
- const lastEnd = text.substring(lineStart, start).lastIndexOf('/*Ωignore_endΩ*/');
30
- return lastStart > lastEnd && text.substring(end, lineEnd).includes('/*Ωignore_endΩ*/');
26
+ const lineStart = text.lastIndexOf('\n', start);
27
+ const lineEnd = text.indexOf('\n', end);
28
+ const lastStart = text.substring(lineStart, start).lastIndexOf('/*Ωignore_startΩ*/');
29
+ const lastEnd = text.substring(lineStart, start).lastIndexOf('/*Ωignore_endΩ*/');
30
+ return lastStart > lastEnd && text.substring(end, lineEnd).includes('/*Ωignore_endΩ*/');
31
31
  }
32
32
 
33
33
  /**
34
- * Checks that this isn't a text span that should be completely ignored
35
- * because it's purely generated.
36
- */
34
+ * Checks that this isn't a text span that should be completely ignored
35
+ * because it's purely generated.
36
+ */
37
37
  export function isNoTextSpanInGeneratedCode(text: string, span: ts.TextSpan) {
38
- return !isInGeneratedCode(text, span.start, span.start + span.length);
38
+ return !isInGeneratedCode(text, span.start, span.start + span.length);
39
39
  }
40
40
 
41
41
  /**
42
- * Replace all occurrences of a string within an object with another string,
43
- */
42
+ * Replace all occurrences of a string within an object with another string,
43
+ */
44
44
  export function replaceDeep<T extends Record<string, any>>(
45
- obj: T,
46
- searchStr: string | RegExp,
47
- replacementStr: string
45
+ obj: T,
46
+ searchStr: string | RegExp,
47
+ replacementStr: string
48
48
  ): T {
49
- return _replaceDeep(obj);
49
+ return _replaceDeep(obj);
50
50
 
51
- function _replaceDeep(_obj: any): any {
52
- if (typeof _obj === 'string') {
53
- return _obj.replace(searchStr, replacementStr);
54
- }
55
- if (Array.isArray(_obj)) {
56
- return _obj.map((entry) => _replaceDeep(entry));
57
- }
58
- if (typeof _obj === 'object') {
59
- return Object.keys(_obj).reduce((_o, key) => {
60
- _o[key] = _replaceDeep(_obj[key]);
61
- return _o;
62
- }, {} as any);
63
- }
64
- return _obj;
65
- }
66
- }
51
+ function _replaceDeep(_obj: any): any {
52
+ if (typeof _obj === 'string') {
53
+ return _obj.replace(searchStr, replacementStr);
54
+ }
55
+ if (Array.isArray(_obj)) {
56
+ return _obj.map((entry) => _replaceDeep(entry));
57
+ }
58
+ if (typeof _obj === 'object') {
59
+ return Object.keys(_obj).reduce((_o, key) => {
60
+ _o[key] = _replaceDeep(_obj[key]);
61
+ return _o;
62
+ }, {} as any);
63
+ }
64
+ return _obj;
65
+ }
66
+ }
package/astro.d.ts DELETED
@@ -1,29 +0,0 @@
1
- type AstroRenderedHTML = string;
2
-
3
- type FetchContentResult<ContentFrontmatter extends Record<string, any> = Record<string, any>> = {
4
- astro: {
5
- headers: string[];
6
- source: string;
7
- html: AstroRenderedHTML;
8
- };
9
- url: URL;
10
- } & ContentFrontmatter;
11
-
12
- interface AstroPageRequest {
13
- url: URL;
14
- canonicalURL: URL;
15
- }
16
-
17
- interface Astro {
18
- isPage: boolean;
19
- fetchContent<ContentFrontmatter>(globStr: string): FetchContentResult<ContentFrontmatter>[];
20
- props: Record<string, number | string | any>;
21
- request: AstroPageRequest;
22
- site: URL;
23
- }
24
-
25
- declare var Astro: Astro;
26
- declare var Fragment: string;
27
-
28
- void Astro;
29
- void Fragment;