1
1
import { Block } from 'css-blocks' ;
2
2
import { ObjectDictionary , } from '@opticss/util' ;
3
+ import {
4
+ SourceLocation as TemplateSourceLocation ,
5
+ SourcePosition as TemplateSourcePosition
6
+ } from '@opticss/template-api' ;
3
7
import { NodePath , Binding } from 'babel-traverse' ;
4
8
import {
5
9
CallExpression ,
@@ -14,9 +18,10 @@ import {
14
18
isVariableDeclarator ,
15
19
JSXAttribute ,
16
20
Identifier ,
21
+ SourceLocation ,
17
22
} from 'babel-types' ;
18
23
19
- import { MalformedBlockPath , TemplateAnalysisError , ErrorLocation } from '../utils/Errors' ;
24
+ import { MalformedBlockPath , TemplateAnalysisError } from '../utils/Errors' ;
20
25
import { isConsoleLogStatement } from '../utils/isConsoleLogStatement' ;
21
26
22
27
import { JSXElementAnalysis , Flags , newJSXElementAnalysis } from './types' ;
@@ -54,21 +59,19 @@ export class JSXElementAnalyzer {
54
59
return found ;
55
60
}
56
61
57
- analyze ( filename : string , path : NodePath < JSXOpeningElement > ) : JSXElementAnalysis | undefined {
62
+ analyze ( path : NodePath < JSXOpeningElement > ) : JSXElementAnalysis | undefined {
58
63
let el = path . node ;
59
64
60
65
// We don't care about elements with no attributes;
61
66
if ( ! el . attributes || el . attributes . length === 0 ) {
62
67
return ;
63
68
}
64
69
65
- let loc = { filename, ...path . node . loc } ;
66
-
67
70
let classAttrs = this . classAttributePaths ( path ) ;
68
71
// If/When we add state attributes, we should throw an error if those are set before exiting.
69
72
if ( classAttrs . length === 0 ) return ;
70
73
71
- let element = newJSXElementAnalysis ( loc , htmlTagName ( el ) ) ;
74
+ let element = newJSXElementAnalysis ( this . location ( path ) , htmlTagName ( el ) ) ;
72
75
73
76
for ( let classAttr of classAttrs ) {
74
77
this . analyzeClassAttribute ( classAttr , element ) ;
@@ -83,9 +86,23 @@ export class JSXElementAnalyzer {
83
86
return element ;
84
87
}
85
88
86
- private nodeLoc ( node : Node | NodePath < Node > ) : ErrorLocation {
87
- let n = isNodePath ( node ) ? node . node : node ;
88
- return { filename : this . filename , ...n . loc . start } ;
89
+ private location ( loc : SourceLocation | Node | NodePath < Node > ) : TemplateSourceLocation {
90
+ if ( isNodePath ( loc ) ) {
91
+ loc = loc . node . loc ;
92
+ } else if ( ! isLocation ( loc ) ) {
93
+ loc = loc . loc ;
94
+ }
95
+ let location : TemplateSourceLocation = {
96
+ start : { ...loc . start } ,
97
+ end : { ...loc . end } ,
98
+ } ;
99
+ location . start . filename = this . filename ;
100
+ location . end ! . filename = this . filename ;
101
+ return location ;
102
+ }
103
+
104
+ private nodeLoc ( node : Node | NodePath < Node > ) : TemplateSourcePosition {
105
+ return this . location ( node ) . start ;
89
106
}
90
107
91
108
styleVariableBinding ( path : NodePath < JSXAttribute > ) : Binding | undefined {
@@ -232,6 +249,13 @@ function htmlTagName(el: JSXOpeningElement): string | undefined {
232
249
return ;
233
250
}
234
251
252
+ function isLocation ( n : object ) : n is SourceLocation {
253
+ if ( ( < SourceLocation > n ) . start && ( < SourceLocation > n ) . end && typeof ( < SourceLocation > n ) . start . line === 'number' ) {
254
+ return true ;
255
+ } else {
256
+ return false ;
257
+ }
258
+ }
235
259
function isNodePath ( n : object ) : n is NodePath {
236
260
if ( ( < NodePath > n ) . node ) {
237
261
return true ;
0 commit comments