Skip to content

Commit

Permalink
fix line and col numbers in plugin error messages (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharsnx authored Jul 7, 2024
1 parent 9a8dbc5 commit 66b0ceb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 22 deletions.
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ function unpluginFactory(userOpts: PluginOptions): UnpluginOptions {
},

transform(code, id) {
CSSModuleError.filename = id;

let plugins: PluginItem[] = ["@babel/plugin-syntax-jsx", Plugin];

const isTSX = /\.tsx$/i.test(id);
Expand All @@ -33,7 +35,7 @@ function unpluginFactory(userOpts: PluginOptions): UnpluginOptions {
});

if (!result || !result.code) {
throw new CSSModuleError(`Could not transform ${id}`);
throw new CSSModuleError("Failed to transform " + id);
}

return { code: result.code, map: result.map };
Expand Down
12 changes: 6 additions & 6 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import { transformClassNames, transformImport } from "./transforms.js";
import { CSSModuleError } from "./utils.js";

function ImportDeclaration(path: NodePath<t.ImportDeclaration>, { pluginState }: PluginPass) {
// saving node for error messages
CSSModuleError.node = path.node;

// we're only interested in scss/sass/css imports
if (!/.module.(s[ac]ss|css)(:.*)?$/iu.test(path.node.source.value)) {
return;
}

// saving path for error messages
CSSModuleError.path = path;

// 1. Transform import declaration
const idGenerator = (hint: string) => path.scope.generateUidIdentifier(hint);
const res = transformImport(path.node, idGenerator);
Expand Down Expand Up @@ -44,6 +44,9 @@ function ImportDeclaration(path: NodePath<t.ImportDeclaration>, { pluginState }:
}

function JSXAttribute(path: NodePath<t.JSXAttribute>, { pluginState }: PluginPass) {
// saving node for error messages
CSSModuleError.node = path.node;

const firstNamedModule = getFirstNamedModule(pluginState.modules.namedModules);

// we only support className attribute having a string value
Expand All @@ -56,9 +59,6 @@ function JSXAttribute(path: NodePath<t.JSXAttribute>, { pluginState }: PluginPas
return;
}

// saving path for error messages
CSSModuleError.path = path;

// if no default modules is available, make the first modules as default
if (!pluginState.modules.defaultModule) {
if (firstNamedModule) {
Expand Down
20 changes: 5 additions & 15 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
import { NodePath } from "@babel/core";
import type { Node } from "@babel/core";
import chalk from "chalk";

export class CSSModuleError extends Error {
errorMessage: string;
static path: NodePath | undefined;
static filename?: string;
static node?: Node;

constructor(errorMessage: string) {
super();
this.errorMessage = errorMessage;
this.name = chalk.red("CSSModuleError");
this.message = `at (${CSSModuleError.path?.node.loc?.start.line}:${CSSModuleError.path?.node.loc?.start.column}):
${this.errorMessage.replace(/ +/g, " ")}
`.replace(/ +/g, " ");
}

static cls(cls: string) {
return `'${chalk.cyan(cls)}'`;
}

static mod(mod: string) {
return `'${chalk.cyan(mod)}'`;
this.message = errorMessage;
this.message += "\n at " + (CSSModuleError.filename ?? "<anonymous>") + ":" + CSSModuleError.node?.loc?.start.line + ":" + CSSModuleError.node?.loc?.start.column;
}
}

0 comments on commit 66b0ceb

Please sign in to comment.