Skip to content

Commit 3bddf41

Browse files
committed
chore: ensure that template names are in the css block errors.
1 parent 6f65119 commit 3bddf41

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

packages/jsx-analyzer/src/analyzer/JSXElementAnalyzer.ts

+32-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { Block } from 'css-blocks';
22
import { ObjectDictionary, } from '@opticss/util';
3+
import {
4+
SourceLocation as TemplateSourceLocation,
5+
SourcePosition as TemplateSourcePosition
6+
} from '@opticss/template-api';
37
import { NodePath, Binding } from 'babel-traverse';
48
import {
59
CallExpression,
@@ -14,9 +18,10 @@ import {
1418
isVariableDeclarator,
1519
JSXAttribute,
1620
Identifier,
21+
SourceLocation,
1722
} from 'babel-types';
1823

19-
import { MalformedBlockPath, TemplateAnalysisError, ErrorLocation } from '../utils/Errors';
24+
import { MalformedBlockPath, TemplateAnalysisError } from '../utils/Errors';
2025
import { isConsoleLogStatement } from '../utils/isConsoleLogStatement';
2126

2227
import { JSXElementAnalysis, Flags, newJSXElementAnalysis } from './types';
@@ -54,21 +59,19 @@ export class JSXElementAnalyzer {
5459
return found;
5560
}
5661

57-
analyze(filename: string, path: NodePath<JSXOpeningElement>): JSXElementAnalysis | undefined {
62+
analyze(path: NodePath<JSXOpeningElement>): JSXElementAnalysis | undefined {
5863
let el = path.node;
5964

6065
// We don't care about elements with no attributes;
6166
if (!el.attributes || el.attributes.length === 0) {
6267
return;
6368
}
6469

65-
let loc = { filename, ...path.node.loc };
66-
6770
let classAttrs = this.classAttributePaths(path);
6871
// If/When we add state attributes, we should throw an error if those are set before exiting.
6972
if (classAttrs.length === 0) return;
7073

71-
let element = newJSXElementAnalysis(loc, htmlTagName(el));
74+
let element = newJSXElementAnalysis(this.location(path), htmlTagName(el));
7275

7376
for (let classAttr of classAttrs) {
7477
this.analyzeClassAttribute(classAttr, element);
@@ -83,9 +86,23 @@ export class JSXElementAnalyzer {
8386
return element;
8487
}
8588

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;
89106
}
90107

91108
styleVariableBinding(path: NodePath<JSXAttribute>): Binding | undefined {
@@ -232,6 +249,13 @@ function htmlTagName(el: JSXOpeningElement): string | undefined {
232249
return;
233250
}
234251

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+
}
235259
function isNodePath(n: object): n is NodePath {
236260
if ((<NodePath>n).node) {
237261
return true;

packages/jsx-analyzer/src/analyzer/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default function visitors(analysis: Analysis): object {
2020
* @param path The JSXOpeningElement Babylon path we are processing.
2121
*/
2222
JSXOpeningElement(path: NodePath<JSXOpeningElement>): void {
23-
let element = elementAnalyzer.analyze(analysis.template.identifier, path);
23+
let element = elementAnalyzer.analyze(path);
2424
if (element) analysis.addElement(element);
2525
}
2626
};

packages/jsx-analyzer/src/transformer/babel.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export default function mkTransform(tranformOpts: { rewriter: Rewriter }): () =>
9797
JSXOpeningElement(path: NodePath<JSXOpeningElement>, state: any): void {
9898
if (!this.shouldProcess) return;
9999

100-
let elementAnalysis = this.elementAnalyzer.analyze(this.filename, path);
100+
let elementAnalysis = this.elementAnalyzer.analyze(path);
101101
if (elementAnalysis) {
102102
let classMapping = this.mapping.simpleRewriteMapping(elementAnalysis);
103103
let attributeValue: JSXAttribute['value'] | undefined = undefined;

0 commit comments

Comments
 (0)