Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix line and col numbers in plugin error messages #10

Merged
merged 1 commit into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}