Skip to content

Commit 0bb137d

Browse files
committed
fix(jsx): Protect against already removed nodes in babel transformer
1 parent 77d6398 commit 0bb137d

File tree

4 files changed

+830
-41
lines changed

4 files changed

+830
-41
lines changed

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"private": true,
3-
"devDependencies": {
4-
"lerna": "^2.5.1",
5-
},
3+
"devDependencies": {
4+
"lerna": "^2.5.1"
5+
},
66
"workspaces": [
77
"packages/*"
88
]

packages/jsx/src/transformer/babel.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ import {
2727
import isBlockFilename from '../utils/isBlockFilename';
2828
import { classnamesHelper as generateClassName, HELPER_FN_NAME } from './classNameGenerator';
2929
// import { TemplateAnalysisError } from '../utils/Errors';
30+
import * as debugGenerator from 'debug';
31+
32+
const debug = debugGenerator('css-blocks:jsx');
33+
3034
let { parse } = require('path');
3135

3236
export interface CssBlocksVisitor {
@@ -57,6 +61,7 @@ export default function mkTransform(tranformOpts: { rewriter: Rewriter }): () =>
5761
this.importsToRemove = new Array<NodePath<ImportDeclaration>>();
5862
this.statementsToRemove = new Array<NodePath<Statement>>();
5963
this.filename = file.opts.filename;
64+
6065
this.mapping = rewriter.blocks[this.filename];
6166
if (this.mapping && this.mapping.analyses) {
6267
let a = this.mapping.analyses.find(a => a.template.identifier === this.filename);
@@ -66,13 +71,15 @@ export default function mkTransform(tranformOpts: { rewriter: Rewriter }): () =>
6671
}
6772
let ext = parse(this.filename).ext;
6873
this.shouldProcess = CAN_PARSE_EXTENSIONS[ext] && this.analysis && this.analysis.blockCount() > 0;
74+
6975
if (this.shouldProcess) {
76+
debug(`Rewriting discovered dependency ${this.filename}`);
7077
this.elementAnalyzer = new JSXElementAnalyzer(this.analysis.blocks, this.filename);
7178
}
7279
},
7380
post(state: any) {
7481
for (let nodePath of this.statementsToRemove) {
75-
if (nodePath.removed) continue;
82+
if (nodePath.removed) { continue; }
7683
nodePath.remove();
7784
}
7885
if (this.dynamicStylesFound) {
@@ -84,6 +91,7 @@ export default function mkTransform(tranformOpts: { rewriter: Rewriter }): () =>
8491
firstImport.replaceWith(importDecl);
8592
}
8693
for (let nodePath of this.importsToRemove) {
94+
if (nodePath.removed) { continue; }
8795
detectStrayReferenceToImport(nodePath, this.filename);
8896
nodePath.remove();
8997
}
@@ -125,8 +133,7 @@ export default function mkTransform(tranformOpts: { rewriter: Rewriter }): () =>
125133
},
126134

127135
JSXOpeningElement(path: NodePath<JSXOpeningElement>, state: any): void {
128-
if (!this.shouldProcess) return;
129-
136+
if (!this.shouldProcess) { return; }
130137
let elementAnalysis = this.elementAnalyzer.analyzeJSXElement(path);
131138
if (elementAnalysis) {
132139
elementAnalysis.seal();
@@ -177,6 +184,7 @@ function detectStrayReferenceToImport(
177184
importDeclPath: NodePath<ImportDeclaration>,
178185
filename: string
179186
): void {
187+
if (!importDeclPath || !importDeclPath.node) { return; }
180188
for (let specifier of importDeclPath.node.specifiers) {
181189
let binding = importDeclPath.scope.getBinding(specifier.local.name);
182190
if (binding) {

packages/jsx/src/transformer/webpackLoader.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { StyleMapping, PluginOptionsReader, Block } from 'css-blocks';
2+
import * as debugGenerator from 'debug';
23

4+
const debug = debugGenerator('css-blocks:jsx');
35
const loaderUtils = require('loader-utils');
46

57
type LoaderContext = {
@@ -49,6 +51,7 @@ export default function CSSBlocksWebpackAdapter(this: any, source: any, map: any
4951
metaMappingPromises.push(this.cssBlocks.mappings[filename]);
5052
});
5153

54+
debug(`Waiting for ${metaMappingPromises.length} block compilations to complete...`);
5255
Promise.all(metaMappingPromises)
5356

5457
.then((mappings: StyleMapping[]) => {
@@ -65,6 +68,8 @@ export default function CSSBlocksWebpackAdapter(this: any, source: any, map: any
6568
rewriter.blocks[path] = styleMapping;
6669
});
6770

71+
debug(`Completed ${metaMappingPromises.length} block compilations!`);
72+
6873
callback(null, source, map);
6974
})
7075

0 commit comments

Comments
 (0)