From d159bd0a8b13e3a65cb4c96636d842182fca0525 Mon Sep 17 00:00:00 2001 From: lucasferreira Date: Fri, 28 Apr 2023 12:29:07 -0300 Subject: [PATCH] First version of this plugin with new option --- README.md | 13 ++++++++++++- index.js | 5 ++++- lib/parse-statements.js | 10 ++++++---- package.json | 2 +- test/lint.js | 29 ++++++++++++++++++++++++++++- 5 files changed, 51 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index ee57fd09..b2d4a662 100644 --- a/README.md +++ b/README.md @@ -113,7 +113,18 @@ Checkout the [tests](test) for more examples. ### Options -### `filter` +#### `anywhereImport` + +Type: `Boolean` +Default: `true` + +Allow this PostCSS plugin to consider any `@import` mentions and using anywhere/anyplace +inside your .css files. If you want to regret to default version of this plugin +like [postcss-import](https://github.com/postcss/postcss-import) turn this `anywhereImport` +option to `false`. + +#### `filter` + Type: `Function` Default: `() => true` diff --git a/index.js b/index.js index d324a7e0..14c4e9f8 100755 --- a/index.js +++ b/index.js @@ -16,6 +16,7 @@ function AtImport(options) { options = { root: process.cwd(), path: [], + anywhereImport: true, skipDuplicates: true, resolve: resolveId, load: loadContent, @@ -218,7 +219,9 @@ function AtImport(options) { } function parseStyles(result, styles, options, state, media, layer) { - const statements = parseStatements(result, styles) + const statements = parseStatements(result, styles, { + anywhereImport: options.anywhereImport || false, + }) return Promise.resolve(statements) .then(stmts => { diff --git a/lib/parse-statements.js b/lib/parse-statements.js index 0c94e5a4..e7f71dda 100644 --- a/lib/parse-statements.js +++ b/lib/parse-statements.js @@ -20,14 +20,14 @@ function split(params, start) { return list } -module.exports = function (result, styles) { +module.exports = function (result, styles, options) { const statements = [] let nodes = [] styles.each(node => { let stmt if (node.type === "atrule") { - if (node.name === "import") stmt = parseImport(result, node) + if (node.name === "import") stmt = parseImport(result, node, options) else if (node.name === "media") stmt = parseMedia(result, node) else if (node.name === "charset") stmt = parseCharset(result, node) } @@ -82,9 +82,11 @@ function parseCharset(result, atRule) { } } -function parseImport(result, atRule) { +function parseImport(result, atRule, options) { + options = { anywhereImport: true, ...options } + let prev = atRule.prev() - if (prev) { + if (!options.anywhereImport && prev) { do { if ( prev.type !== "comment" && diff --git a/package.json b/package.json index e72d4bd1..d207d039 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ "ci": "eslint . && ava", "lint": "eslint . --fix", "pretest": "npm run lint", - "test": "ava" + "test": "ava --verbose" }, "eslintConfig": { "extends": "eslint-config-problems", diff --git a/test/lint.js b/test/lint.js index 71a7c3b5..973cbd61 100644 --- a/test/lint.js +++ b/test/lint.js @@ -6,7 +6,11 @@ const postcss = require("postcss") // plugin const atImport = require("..") -const processor = postcss().use(atImport()) +const processor = postcss().use( + atImport({ + anywhereImport: false, + }) +) test("should warn when not @charset and not @import statement before", t => { return Promise.all([ @@ -45,6 +49,29 @@ test("should warn about all imports after some other CSS declaration", t => { }) }) +test("should NOT warn about all imports after some other CSS declaration [anywhereImport = true]", t => { + return postcss() + .use( + atImport({ + anywhereImport: true, + }) + ) + .process( + ` + x {} + z {} + + @import "bar.css"; + @import "foo.css"; + `, + { from: "test/fixtures/imports/baz.css" } + ) + .then(result => { + const warnings = result.warnings() + t.is(warnings.length, 0) + }) +}) + test("should warn if non-empty @layer before @import", t => { return processor .process(`@layer { a {} } @import "a.css";`, { from: undefined })