Skip to content

Commit

Permalink
[stylelint-plugin] chore: checkImportExists auto-selects extension (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kmark authored Oct 10, 2022
1 parent 909f4b9 commit 0dd1c3a
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 20 deletions.
9 changes: 1 addition & 8 deletions packages/stylelint-plugin/src/rules/no-color-literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
BpSassNamespace,
BpVariableImportMap,
BpVariablePrefixMap,
CssExtensionMap,
CssSyntax,
getCssSyntax,
isCssSyntaxToStringMap,
Expand Down Expand Up @@ -81,14 +80,8 @@ const ruleImpl =
let hasBpVariablesImport: boolean | undefined; // undefined means not checked yet
function assertBpVariablesImportExists(cssSyntaxType: CssSyntax.SASS | CssSyntax.LESS) {
const importPath = options?.variablesImportPath?.[cssSyntaxType] ?? BpVariableImportMap[cssSyntaxType];
const extension = CssExtensionMap[cssSyntaxType];
if (hasBpVariablesImport == null) {
hasBpVariablesImport = checkImportExists(
cssSyntaxType,
root,
[importPath, `${importPath}.${extension}`],
BpSassNamespace,
);
hasBpVariablesImport = checkImportExists(cssSyntaxType, root, importPath, BpSassNamespace);
}
if (!hasBpVariablesImport) {
insertImport(cssSyntaxType, root, context, importPath, BpSassNamespace);
Expand Down
9 changes: 1 addition & 8 deletions packages/stylelint-plugin/src/rules/no-prefix-literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
BpPrefixVariableMap,
BpSassNamespace,
BpVariableImportMap,
CssExtensionMap,
CssSyntax,
getCssSyntax,
isCssSyntaxToStringMap,
Expand Down Expand Up @@ -81,14 +80,8 @@ const ruleImpl =
let hasBpVariablesImport: boolean | undefined; // undefined means not checked yet
function assertBpVariablesImportExists(cssSyntaxType: CssSyntax.SASS | CssSyntax.LESS) {
const importPath = options?.variablesImportPath?.[cssSyntaxType] ?? BpVariableImportMap[cssSyntaxType];
const extension = CssExtensionMap[cssSyntaxType];
if (hasBpVariablesImport == null) {
hasBpVariablesImport = checkImportExists(
cssSyntaxType,
root,
[importPath, `${importPath}.${extension}`],
BpSassNamespace,
);
hasBpVariablesImport = checkImportExists(cssSyntaxType, root, importPath, BpSassNamespace);
}
if (!hasBpVariablesImport) {
insertImport(cssSyntaxType, root, context, importPath, BpSassNamespace);
Expand Down
6 changes: 3 additions & 3 deletions packages/stylelint-plugin/src/utils/checkImportExists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import type { Root } from "postcss";

import { CssSyntax } from "./cssSyntax";
import { CssExtensionMap, CssSyntax } from "./cssSyntax";

/**
* Returns true if the given import exists in the file, otherwise returns false.
Expand All @@ -24,13 +24,13 @@ import { CssSyntax } from "./cssSyntax";
export function checkImportExists(
cssSyntaxType: CssSyntax.SASS | CssSyntax.LESS,
root: Root,
importPath: string | string[],
importPath: string,
namespace?: string,
): boolean {
let hasBpVarsImport = false;
const walkRegex = cssSyntaxType === CssSyntax.LESS ? /^import$/i : /^use$/i;
root.walkAtRules(walkRegex, atRule => {
for (const path of typeof importPath === "string" ? [importPath] : importPath) {
for (const path of [importPath, `${importPath}.${CssExtensionMap[cssSyntaxType]}`]) {
if (stripQuotes(stripLessReference(atRule.params)) === path) {
hasBpVarsImport = true;
return false; // Stop the iteration
Expand Down
2 changes: 1 addition & 1 deletion packages/stylelint-plugin/test/checkImportExists.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe("checkImportExists", () => {
width: 10px;
}
`);
expect(checkImportExists(CssSyntax.SASS, root, ["some_path", "some_path.scss"])).to.be.true;
expect(checkImportExists(CssSyntax.SASS, root, "some_path")).to.be.true;
});

it("Handles less references", () => {
Expand Down

1 comment on commit 0dd1c3a

@blueprint-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[stylelint-plugin] chore: checkImportExists auto-selects extension (#5654)

Previews: documentation | landing | table | demo

Please sign in to comment.