Skip to content

Commit d25d1ae

Browse files
fix(use-env-prefix): fix around treating filename (#117)
fix #116
1 parent e09d2b7 commit d25d1ae

File tree

5 files changed

+31
-7
lines changed

5 files changed

+31
-7
lines changed

lib/rules/use-env-prefix.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
import { AST } from "vue-eslint-parser";
77
import minimatch from "minimatch";
8-
import { createRule, Env } from "../utils";
8+
import { createRule, Env, getPathFromProjectRoot } from "../utils";
99
import * as Fs from "fs";
1010

1111
const PREFIX = "GRIDSOME_";
@@ -49,14 +49,14 @@ export = createRule<[Options], MessageIds>({
4949
},
5050
defaultOptions,
5151
create(context) {
52-
const filename = context.getFilename();
52+
const path = getPathFromProjectRoot(context.getFilename(), process.cwd());
5353

5454
const pathsForBrowserfileOption = context.options[0]
5555
?.pathsForBrowserfile || ["src/**/*"];
5656
const envPathOption = context.options[0]?.envPath || ".env";
5757

5858
const isClientfile = pathsForBrowserfileOption.some((clientPath) =>
59-
minimatch(filename, clientPath)
59+
minimatch(path, clientPath)
6060
);
6161

6262
const envSource = Fs.readFileSync(envPathOption, { encoding: "utf8" });

lib/utils/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,8 @@ export * from "./directive";
1818
// env
1919
// ====================================================
2020
export * from "./env";
21+
22+
// ====================================================
23+
// others
24+
// ====================================================
25+
export * from "./path";

lib/utils/path.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const getPathFromProjectRoot = (filename: string, cwd: string) =>
2+
filename.split("/").splice(cwd.split("/").length).join("/");

tests/lib/rules/use-env-prefix.spec.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from "path";
12
import { RuleTester, loadFixtureCreator } from "../../util";
23

34
import rule from "../../../lib/rules/use-env-prefix";
@@ -17,7 +18,7 @@ const loadFixture = loadFixtureCreator(
1718
tester.run("use-env-prefix", rule, {
1819
valid: [
1920
{
20-
filename: "src/components/code.vue",
21+
filename: path.join(process.cwd(), "src/components/code.vue"),
2122
...loadFixture({
2223
fixtureDirectory: "valid/01",
2324
}),
@@ -29,7 +30,7 @@ tester.run("use-env-prefix", rule, {
2930
],
3031
},
3132
{
32-
filename: "src/client.js",
33+
filename: path.join(process.cwd(), "src/client.js"),
3334
...loadFixture({
3435
fixtureDirectory: "valid/02",
3536
filenames: {
@@ -46,7 +47,7 @@ tester.run("use-env-prefix", rule, {
4647
],
4748
invalid: [
4849
{
49-
filename: "src/components/code.vue",
50+
filename: path.join(process.cwd(), "src/components/code.vue"),
5051
...loadFixture({
5152
fixtureDirectory: "invalid/01",
5253
}),
@@ -63,7 +64,7 @@ tester.run("use-env-prefix", rule, {
6364
],
6465
},
6566
{
66-
filename: "src/client.js",
67+
filename: path.join(process.cwd(), "src/client.js"),
6768
...loadFixture({
6869
fixtureDirectory: "invalid/02",
6970
filenames: {

tests/lib/utils/path.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { getPathFromProjectRoot } from "../../../lib/utils/path";
2+
3+
import { expect } from "chai";
4+
5+
describe("path", () => {
6+
describe("getPathFromProjectRoot", () => {
7+
it("when call getPathFromProjectRoot with context.getFilename() and process.cwd(), return path from project root", () => {
8+
const filename = "/User/path/to/project/src/index.js";
9+
const cwd = "/User/path/to/project";
10+
11+
const result = getPathFromProjectRoot(filename, cwd);
12+
13+
expect(result).to.deep.equal("src/index.js");
14+
});
15+
});
16+
});

0 commit comments

Comments
 (0)