From 7cee2a4c38a694e902a2a68a06c9d3cd79ad905c Mon Sep 17 00:00:00 2001 From: Matt Wang Date: Thu, 29 Jun 2023 21:24:00 -0700 Subject: [PATCH] Remove unused modules (from deprecated rules) (#7003) Ref: https://github.com/stylelint/stylelint/pull/6979#issuecomment-1613250886 Re-running the script indicates no newly-unused files: ```sh $ node scripts/find-unused-modules.mjs ``` --- .../__tests__/addEmptyLineAfter.test.mjs | 52 -------------- lib/utils/__tests__/hasEmptyBlock.test.js | 36 ---------- .../isStandardSyntaxMathFunction.test.js | 47 ------------- .../isStandardSyntaxMediaFeature.test.js | 46 ------------- .../__tests__/nextNonCommentNode.test.js | 69 ------------------- .../__tests__/removeEmptyLinesAfter.test.js | 54 --------------- lib/utils/addEmptyLineAfter.js | 28 -------- lib/utils/hasEmptyBlock.js | 13 ---- lib/utils/isStandardSyntaxMathFunction.js | 21 ------ lib/utils/isStandardSyntaxMediaFeature.js | 26 ------- lib/utils/nextNonCommentNode.js | 20 ------ lib/utils/removeEmptyLinesAfter.js | 15 ---- 12 files changed, 427 deletions(-) delete mode 100644 lib/utils/__tests__/addEmptyLineAfter.test.mjs delete mode 100644 lib/utils/__tests__/hasEmptyBlock.test.js delete mode 100644 lib/utils/__tests__/isStandardSyntaxMathFunction.test.js delete mode 100644 lib/utils/__tests__/isStandardSyntaxMediaFeature.test.js delete mode 100644 lib/utils/__tests__/nextNonCommentNode.test.js delete mode 100644 lib/utils/__tests__/removeEmptyLinesAfter.test.js delete mode 100644 lib/utils/addEmptyLineAfter.js delete mode 100644 lib/utils/hasEmptyBlock.js delete mode 100644 lib/utils/isStandardSyntaxMathFunction.js delete mode 100644 lib/utils/isStandardSyntaxMediaFeature.js delete mode 100644 lib/utils/nextNonCommentNode.js delete mode 100644 lib/utils/removeEmptyLinesAfter.js diff --git a/lib/utils/__tests__/addEmptyLineAfter.test.mjs b/lib/utils/__tests__/addEmptyLineAfter.test.mjs deleted file mode 100644 index 56c10fefb6..0000000000 --- a/lib/utils/__tests__/addEmptyLineAfter.test.mjs +++ /dev/null @@ -1,52 +0,0 @@ -import addEmptyLineAfter from '../addEmptyLineAfter.js'; -import postcss from 'postcss'; - -describe('addEmptyLineBefore', () => { - it('adds single newline to the newline at the beginning', () => { - expect(run('a {\n}', '\n')).toBe('a {\n\n}'); - }); - - it('adds single newline to newline at the beginning with CRLF', () => { - expect(run('a {\r\n}', '\r\n')).toBe('a {\r\n\r\n}'); - }); - - it('adds single newline to newline at the end', () => { - expect(run('a {\t\n}', '\n')).toBe('a {\t\n\n}'); - }); - - it('adds single newline to newline at the end with CRLF', () => { - expect(run('a {\t\r\n}', '\r\n')).toBe('a {\t\r\n\r\n}'); - }); - - it('adds single newline to newline in the middle', () => { - expect(run('a { \n\t}', '\n')).toBe('a { \n\n\t}'); - }); - - it('adds single newline to newline in the middle with CRLF', () => { - expect(run('a { \r\n\t}', '\r\n')).toBe('a { \r\n\r\n\t}'); - }); - - it("adds two newlines if there aren't any existing newlines", () => { - expect(run('a { }', '\n')).toBe('a { \n\n}'); - }); - - it("adds two newlines if there aren't any existing newlines with CRLF", () => { - expect(run('a { }', '\r\n')).toBe('a { \r\n\r\n}'); - }); - - it("adds two newlines if there aren't any newlines after semicolon", () => { - expect(run('a {\n;}', '\n')).toBe('a {\n;\n\n}'); - }); - - it("adds two newlines if there aren't any newlines after semicolon with CRLF", () => { - expect(run('a {\r\n;}', '\r\n')).toBe('a {\r\n;\r\n\r\n}'); - }); -}); - -function run(css, lineEnding) { - const root = postcss.parse(css); - - addEmptyLineAfter(root.nodes[0], lineEnding); - - return root.toString(); -} diff --git a/lib/utils/__tests__/hasEmptyBlock.test.js b/lib/utils/__tests__/hasEmptyBlock.test.js deleted file mode 100644 index 9608675122..0000000000 --- a/lib/utils/__tests__/hasEmptyBlock.test.js +++ /dev/null @@ -1,36 +0,0 @@ -'use strict'; - -const hasEmptyBlock = require('../hasEmptyBlock'); -const postcss = require('postcss'); - -it('hasEmptyBlock', () => { - expect(postcssCheck('a {}')).toBeTruthy(); - expect(postcssCheck('a { }')).toBeTruthy(); - expect(postcssCheck('a {\n}')).toBeTruthy(); - expect(postcssCheck('@media print {}')).toBeTruthy(); - expect(postcssCheck('@supports (animation-name: test) {}')).toBeTruthy(); - expect(postcssCheck('@document url(http://www.w3.org/) {}')).toBeTruthy(); - expect(postcssCheck('@page :pseudo-class {}')).toBeTruthy(); - expect(postcssCheck('@font-face {}')).toBeTruthy(); - expect(postcssCheck('@keyframes identifier {}')).toBeTruthy(); - - expect(postcssCheck('a { color: pink; }')).toBeFalsy(); - expect(postcssCheck('@media print { a { color: pink; } }')).toBeFalsy(); - expect(postcssCheck('@supports (animation-name: test) { a { color: pink; } }')).toBeFalsy(); - expect(postcssCheck('@document url(http://www.w3.org/) { a { color: pink; } }')).toBeFalsy(); - expect(postcssCheck('@page :pseudo-class { a { color: pink; } }')).toBeFalsy(); - expect(postcssCheck('@font-face { font-family: sans; }')).toBeFalsy(); - expect(postcssCheck('@keyframes identifier { 0% { top: 0; left:} }')).toBeFalsy(); - expect(postcssCheck('@import url(x.css)')).toBeFalsy(); - expect(postcssCheck("@import 'x.css'")).toBeFalsy(); - expect(postcssCheck('@import "x.css"')).toBeFalsy(); - expect(postcssCheck('@charset "UTF-8"')).toBeFalsy(); - expect(postcssCheck('@namespace url(http://www.w3.org/1999/xhtml)')).toBeFalsy(); - expect(postcssCheck('@namespace svg url(http://www.w3.org/2000/svg)')).toBeFalsy(); -}); - -function postcssCheck(cssString) { - const root = postcss.parse(cssString); - - return hasEmptyBlock(root.first); -} diff --git a/lib/utils/__tests__/isStandardSyntaxMathFunction.test.js b/lib/utils/__tests__/isStandardSyntaxMathFunction.test.js deleted file mode 100644 index 0c0ad449de..0000000000 --- a/lib/utils/__tests__/isStandardSyntaxMathFunction.test.js +++ /dev/null @@ -1,47 +0,0 @@ -'use strict'; - -const isStandardSyntaxMathFunction = require('../isStandardSyntaxMathFunction'); - -describe('isStandardSyntaxMathFunction', () => { - it('standard', () => { - const css = 'calc(10px + 10px)'; - - expect(isStandardSyntaxMathFunction(css)).toBe(true); - }); - - it('standard with custom property', () => { - const css = 'calc(10px + var(--hello))'; - - expect(isStandardSyntaxMathFunction(css)).toBe(true); - }); - - it('SCSS variable without dashes', () => { - const css = 'calc($var * 3)'; - - expect(isStandardSyntaxMathFunction(css)).toBe(false); - }); - - it('SCSS variable with dashes', () => { - const css = 'calc(3 + $my-var)'; - - expect(isStandardSyntaxMathFunction(css)).toBe(false); - }); - - it('SCSS interpolation', () => { - const css = 'calc(3 - ${$my-var})'; - - expect(isStandardSyntaxMathFunction(css)).toBe(false); - }); - - it('Less variable without dashes', () => { - const css = 'calc(@var * 3)'; - - expect(isStandardSyntaxMathFunction(css)).toBe(false); - }); - - it('Less variable with dashes', () => { - const css = 'calc(3 - @my-var)'; - - expect(isStandardSyntaxMathFunction(css)).toBe(false); - }); -}); diff --git a/lib/utils/__tests__/isStandardSyntaxMediaFeature.test.js b/lib/utils/__tests__/isStandardSyntaxMediaFeature.test.js deleted file mode 100644 index d434343bf4..0000000000 --- a/lib/utils/__tests__/isStandardSyntaxMediaFeature.test.js +++ /dev/null @@ -1,46 +0,0 @@ -'use strict'; - -const isStandardSyntaxMediaFeature = require('../isStandardSyntaxMediaFeature'); - -describe('isStandardSyntaxMediaFeature', () => { - it('prefix on range features', () => { - const css = '(min-width: 10px)'; - - expect(isStandardSyntaxMediaFeature(css)).toBeTruthy(); - }); - it('range context', () => { - const css = '(width <= 3rem)'; - - expect(isStandardSyntaxMediaFeature(css)).toBeTruthy(); - }); - it('nested range context', () => { - const css = '(400px < width < 1000px)'; - - expect(isStandardSyntaxMediaFeature(css)).toBeTruthy(); - }); - it('boolean context', () => { - const css = '(color)'; - - expect(isStandardSyntaxMediaFeature(css)).toBeTruthy(); - }); - it('complex value', () => { - const css = '(min-width: calc(100% - 20px))'; - - expect(isStandardSyntaxMediaFeature(css)).toBeFalsy(); - }); - it('complex SCSS value', () => { - const css = '(min-width: ($var - 10px))'; - - expect(isStandardSyntaxMediaFeature(css)).toBeFalsy(); - }); - it('SCSS interpolation', () => { - const css = '(min-width#{$value}: 10px)'; - - expect(isStandardSyntaxMediaFeature(css)).toBeFalsy(); - }); - it('Less interpolation', () => { - const css = '(@{value}min-width : 10px)'; - - expect(isStandardSyntaxMediaFeature(css)).toBeFalsy(); - }); -}); diff --git a/lib/utils/__tests__/nextNonCommentNode.test.js b/lib/utils/__tests__/nextNonCommentNode.test.js deleted file mode 100644 index 37da69421a..0000000000 --- a/lib/utils/__tests__/nextNonCommentNode.test.js +++ /dev/null @@ -1,69 +0,0 @@ -'use strict'; - -const nextNonCommentNode = require('../nextNonCommentNode'); -const postcss = require('postcss'); - -describe('nextNonCommentNode', () => { - let caseA; - let caseB; - let aNode; - let bNode; - let colorNode; - - beforeEach(() => { - aNode = undefined; - bNode = undefined; - colorNode = undefined; - caseA = 'a {} /* x */ b {}'; - caseB = 'a { /* x */ color: pink; /* y */ }'; - }); - - it('next node is a selector preceded by a comment', () => { - postcss.parse(caseA).walkRules((rule) => { - if (rule.selector === 'a') { - aNode = rule; - } - - if (rule.selector === 'b') { - bNode = rule; - } - }); - - expect(nextNonCommentNode(aNode.next())).toBe(bNode); - }); - - it('next node does not exist', () => { - postcss.parse(caseA).walkRules((rule) => { - if (rule.selector === 'a') { - aNode = rule; - } - - if (rule.selector === 'b') { - bNode = rule; - } - }); - - expect(nextNonCommentNode(bNode.next())).toBeNull(); - }); - - it('next node is a declaration preceded by a comment', () => { - const root = postcss.parse(caseB); - - root.walkRules((rule) => { - aNode = rule; - }); - root.walkDecls((rule) => { - colorNode = rule; - }); - - expect(nextNonCommentNode(aNode.first)).toBe(colorNode); - }); - - it('next node is null preceded by a comment', () => { - postcss.parse(caseB).walkDecls((rule) => { - colorNode = rule; - }); - - expect(nextNonCommentNode(colorNode.next())).toBeNull(); - }); -}); diff --git a/lib/utils/__tests__/removeEmptyLinesAfter.test.js b/lib/utils/__tests__/removeEmptyLinesAfter.test.js deleted file mode 100644 index 947d44bcf9..0000000000 --- a/lib/utils/__tests__/removeEmptyLinesAfter.test.js +++ /dev/null @@ -1,54 +0,0 @@ -'use strict'; - -const postcss = require('postcss'); -const removeEmptyLinesAfter = require('../removeEmptyLinesAfter'); - -describe('removeEmptyLineBefore', () => { - it('removes single newline from the newline at the beginning', () => { - expect(run('a {\n\n }', '\n')).toBe('a {\n }'); - }); - - it('removes single newline from newline at the beginning with CRLF', () => { - expect(run('a {\r\n\r\n }', '\r\n')).toBe('a {\r\n }'); - }); - - it('removes single newline from newline at the end', () => { - expect(run('a {\t\n\n}', '\n')).toBe('a {\t\n}'); - }); - - it('removes single newline from newline at the end with CRLF', () => { - expect(run('a {\t\r\n\r\n}', '\r\n')).toBe('a {\t\r\n}'); - }); - - it('removes single newline from newline in the middle', () => { - expect(run('a { \n\n\t}', '\n')).toBe('a { \n\t}'); - }); - - it('removes single newline to newline in the middle with CRLF', () => { - expect(run('a { \r\n\r\n\t}', '\r\n')).toBe('a { \r\n\t}'); - }); - - it('removes two newlines if there are three newlines', () => { - expect(run('a {\n\n\n }', '\n')).toBe('a {\n }'); - }); - - it('removes two newlines if there are three newlines with CRLF', () => { - expect(run('a {\r\n\r\n\r\n }', '\r\n')).toBe('a {\r\n }'); - }); - - it('removes three newlines if there are four newlines', () => { - expect(run('a {\n\n\n\n }', '\n')).toBe('a {\n }'); - }); - - it('removes three newlines if there are four newlines with CRLF', () => { - expect(run('a {\r\n\r\n\r\n\r\n }', '\r\n')).toBe('a {\r\n }'); - }); -}); - -function run(css, lineEnding) { - const root = postcss.parse(css); - - removeEmptyLinesAfter(root.nodes[0], lineEnding); - - return root.toString(); -} diff --git a/lib/utils/addEmptyLineAfter.js b/lib/utils/addEmptyLineAfter.js deleted file mode 100644 index afcd00eb74..0000000000 --- a/lib/utils/addEmptyLineAfter.js +++ /dev/null @@ -1,28 +0,0 @@ -'use strict'; - -/** - * Add an empty line after a node. Mutates the node. - * - * @template {import('postcss').Rule | import('postcss').AtRule} T - * @param {T} node - * @param {string} newline - * @returns {T} - */ -module.exports = function addEmptyLineAfter(node, newline) { - const { raws } = node; - - if (typeof raws.after !== 'string') { - return node; - } - - const spaces = raws.after.split(';'); - const after = spaces[spaces.length - 1] || ''; - - if (!/\r?\n/.test(after)) { - raws.after += newline.repeat(2); - } else { - raws.after = raws.after.replace(/(\r?\n)/, `${newline}$1`); - } - - return node; -}; diff --git a/lib/utils/hasEmptyBlock.js b/lib/utils/hasEmptyBlock.js deleted file mode 100644 index eb7076967d..0000000000 --- a/lib/utils/hasEmptyBlock.js +++ /dev/null @@ -1,13 +0,0 @@ -'use strict'; - -const hasBlock = require('./hasBlock'); - -/** - * Check if a statement has an empty block. - * - * @param {import('postcss').Rule | import('postcss').AtRule} statement - postcss rule or at-rule node - * @return {boolean} True if the statement has a block and it is empty - */ -module.exports = function hasEmptyBlock(statement) { - return hasBlock(statement) && statement.nodes.length === 0; -}; diff --git a/lib/utils/isStandardSyntaxMathFunction.js b/lib/utils/isStandardSyntaxMathFunction.js deleted file mode 100644 index cb13e9bd5d..0000000000 --- a/lib/utils/isStandardSyntaxMathFunction.js +++ /dev/null @@ -1,21 +0,0 @@ -'use strict'; - -/** - * Check whether a math function is standard - * - * @param {string} mathFunction - * @returns {boolean} - */ -module.exports = function isStandardSyntaxMathFunction(mathFunction) { - // SCSS variable - if (mathFunction.includes('$')) { - return false; - } - - // Less variable - if (mathFunction.includes('@')) { - return false; - } - - return true; -}; diff --git a/lib/utils/isStandardSyntaxMediaFeature.js b/lib/utils/isStandardSyntaxMediaFeature.js deleted file mode 100644 index 6b6a867244..0000000000 --- a/lib/utils/isStandardSyntaxMediaFeature.js +++ /dev/null @@ -1,26 +0,0 @@ -'use strict'; - -const hasInterpolation = require('../utils/hasInterpolation'); - -/** - * Check whether a media feature is standard - * - * @param {string} mediaFeature - * @returns {boolean} - */ -module.exports = function isStandardSyntaxMediaFeature(mediaFeature) { - // Remove outside parens - mediaFeature = mediaFeature.slice(1, -1); - - // Parentheticals used for non-standard operations e.g. ($var - 10) - if (mediaFeature.includes('(')) { - return false; - } - - // SCSS or Less interpolation - if (hasInterpolation(mediaFeature)) { - return false; - } - - return true; -}; diff --git a/lib/utils/nextNonCommentNode.js b/lib/utils/nextNonCommentNode.js deleted file mode 100644 index ac4d074014..0000000000 --- a/lib/utils/nextNonCommentNode.js +++ /dev/null @@ -1,20 +0,0 @@ -'use strict'; - -/** @typedef {import('postcss').Node} PostcssNode */ - -/** - * Get the next non-comment node in a PostCSS AST - * at or after a given node. - * - * @param {PostcssNode | void} startNode - * @returns {PostcssNode | null} - */ -module.exports = function nextNonCommentNode(startNode) { - if (!startNode || !startNode.next) return null; - - if (startNode.type === 'comment') { - return nextNonCommentNode(startNode.next()); - } - - return startNode; -}; diff --git a/lib/utils/removeEmptyLinesAfter.js b/lib/utils/removeEmptyLinesAfter.js deleted file mode 100644 index d1e8bdf8db..0000000000 --- a/lib/utils/removeEmptyLinesAfter.js +++ /dev/null @@ -1,15 +0,0 @@ -'use strict'; - -/** - * Remove empty lines before a node. Mutates the node. - * - * @template {import('postcss').Rule | import('postcss').AtRule} T - * @param {T} node - * @param {string} newline - * @returns {T} - */ -module.exports = function removeEmptyLinesAfter(node, newline) { - node.raws.after = node.raws.after ? node.raws.after.replace(/(\r?\n\s*\n)+/g, newline) : ''; - - return node; -};