@griffel/eslint-plugin
Advanced tools
Comparing version
{ | ||
"name": "@griffel/eslint-plugin", | ||
"version": "1.6.0", | ||
"version": "1.6.1", | ||
"description": "ESLint plugin with lint rules for Griffel", | ||
@@ -18,5 +18,4 @@ "license": "MIT", | ||
}, | ||
"module": "./src/index.js", | ||
"type": "module", | ||
"main": "./src/index.js" | ||
"main": "./src/index.js", | ||
"type": "commonjs" | ||
} |
@@ -1,2 +0,5 @@ | ||
export const recommendedConfig = { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.recommendedConfig = void 0; | ||
exports.recommendedConfig = { | ||
plugins: ['@griffel'], | ||
@@ -3,0 +6,0 @@ rules: { |
@@ -1,21 +0,23 @@ | ||
import { recommendedConfig } from './configs/recommended'; | ||
import { hookNamingRule } from './rules/hook-naming'; | ||
import { noInvalidShorthandArgumentRule } from './rules/no-invalid-shorthand-argument'; | ||
import { noShorthandsRule } from './rules/no-shorthands'; | ||
import { pseudoElementNamingRule } from './rules/pseudo-element-naming'; | ||
import { stylesFileRule } from './rules/styles-file'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const recommended_1 = require("./configs/recommended"); | ||
const hook_naming_1 = require("./rules/hook-naming"); | ||
const no_invalid_shorthand_argument_1 = require("./rules/no-invalid-shorthand-argument"); | ||
const no_shorthands_1 = require("./rules/no-shorthands"); | ||
const pseudo_element_naming_1 = require("./rules/pseudo-element-naming"); | ||
const styles_file_1 = require("./rules/styles-file"); | ||
const rules = { | ||
'hook-naming': hookNamingRule, | ||
'no-invalid-shorthand-argument': noInvalidShorthandArgumentRule, | ||
'no-shorthands': noShorthandsRule, | ||
'styles-file': stylesFileRule, | ||
'pseudo-element-naming': pseudoElementNamingRule, | ||
'hook-naming': hook_naming_1.hookNamingRule, | ||
'no-invalid-shorthand-argument': no_invalid_shorthand_argument_1.noInvalidShorthandArgumentRule, | ||
'no-shorthands': no_shorthands_1.noShorthandsRule, | ||
'styles-file': styles_file_1.stylesFileRule, | ||
'pseudo-element-naming': pseudo_element_naming_1.pseudoElementNamingRule, | ||
}; | ||
const plugin = { | ||
configs: { | ||
recommended: recommendedConfig, | ||
recommended: recommended_1.recommendedConfig, | ||
}, | ||
rules, | ||
}; | ||
export default plugin; | ||
exports.default = plugin; | ||
//# sourceMappingURL=index.js.map |
@@ -1,7 +0,10 @@ | ||
import { ESLintUtils } from '@typescript-eslint/utils'; | ||
import { getDocsUrl } from '../utils/getDocsUrl'; | ||
import { isIdentifier, isMakeStylesCallExpression } from '../utils/helpers'; | ||
export const RULE_NAME = 'hook-naming'; | ||
export const hookNamingRule = ESLintUtils.RuleCreator(getDocsUrl)({ | ||
name: RULE_NAME, | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.hookNamingRule = exports.RULE_NAME = void 0; | ||
const utils_1 = require("@typescript-eslint/utils"); | ||
const getDocsUrl_1 = require("../utils/getDocsUrl"); | ||
const helpers_1 = require("../utils/helpers"); | ||
exports.RULE_NAME = 'hook-naming'; | ||
exports.hookNamingRule = utils_1.ESLintUtils.RuleCreator(getDocsUrl_1.getDocsUrl)({ | ||
name: exports.RULE_NAME, | ||
meta: { | ||
@@ -28,5 +31,5 @@ type: 'problem', | ||
node.init.type === 'CallExpression' && | ||
isMakeStylesCallExpression(node.init, 'makeStyles', 'makeStaticStyles', 'makeResetStyles')) { | ||
(0, helpers_1.isMakeStylesCallExpression)(node.init, 'makeStyles', 'makeStaticStyles', 'makeResetStyles')) { | ||
const { id } = node; | ||
if (isIdentifier(id) && !id.name.startsWith('use')) { | ||
if ((0, helpers_1.isIdentifier)(id) && !id.name.startsWith('use')) { | ||
context.report({ | ||
@@ -33,0 +36,0 @@ node: id, |
@@ -1,7 +0,10 @@ | ||
import { ESLintUtils } from '@typescript-eslint/utils'; | ||
import { getDocsUrl } from '../utils/getDocsUrl'; | ||
import { isIdentifier, isMemberExpression } from '../utils/helpers'; | ||
import { getShorthandValue, joinShorthandArguments } from '../utils/getShorthandValue'; | ||
import { shorthandToArguments } from '../utils/shorthandToArguments'; | ||
export const RULE_NAME = 'no-invalid-shorthand-argument'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.noInvalidShorthandArgumentRule = exports.RULE_NAME = void 0; | ||
const utils_1 = require("@typescript-eslint/utils"); | ||
const getDocsUrl_1 = require("../utils/getDocsUrl"); | ||
const helpers_1 = require("../utils/helpers"); | ||
const getShorthandValue_1 = require("../utils/getShorthandValue"); | ||
const shorthandToArguments_1 = require("../utils/shorthandToArguments"); | ||
exports.RULE_NAME = 'no-invalid-shorthand-argument'; | ||
/** | ||
@@ -11,5 +14,5 @@ * Check if the node represents is a shorthand and return the name. | ||
function getShorthandName(node) { | ||
if (isMemberExpression(node) && | ||
isIdentifier(node.object) && | ||
isIdentifier(node.property) && | ||
if ((0, helpers_1.isMemberExpression)(node) && | ||
(0, helpers_1.isIdentifier)(node.object) && | ||
(0, helpers_1.isIdentifier)(node.property) && | ||
node.object.name === 'shorthands') { | ||
@@ -20,4 +23,4 @@ return node.property.name; | ||
} | ||
export const noInvalidShorthandArgumentRule = ESLintUtils.RuleCreator(getDocsUrl)({ | ||
name: RULE_NAME, | ||
exports.noInvalidShorthandArgumentRule = utils_1.ESLintUtils.RuleCreator(getDocsUrl_1.getDocsUrl)({ | ||
name: exports.RULE_NAME, | ||
meta: { | ||
@@ -42,5 +45,5 @@ type: 'problem', | ||
if (shorthandName && node.arguments.length === 1) { | ||
const shorthandLiteral = getShorthandValue(node.arguments[0], sourceCode); | ||
const shorthandLiteral = (0, getShorthandValue_1.getShorthandValue)(node.arguments[0], sourceCode); | ||
if (shorthandLiteral) { | ||
const autoFixArguments = shorthandToArguments(shorthandName, shorthandLiteral.value); | ||
const autoFixArguments = (0, shorthandToArguments_1.shorthandToArguments)(shorthandName, shorthandLiteral.value); | ||
if (autoFixArguments != null && autoFixArguments.length > 1) { | ||
@@ -50,3 +53,3 @@ context.report({ | ||
messageId: 'invalidShorthandArgument', | ||
fix: fixer => fixer.replaceText(node.arguments[0], joinShorthandArguments(autoFixArguments, shorthandLiteral.quote)), | ||
fix: fixer => fixer.replaceText(node.arguments[0], (0, getShorthandValue_1.joinShorthandArguments)(autoFixArguments, shorthandLiteral.quote)), | ||
}); | ||
@@ -53,0 +56,0 @@ } |
@@ -1,16 +0,19 @@ | ||
import { ESLintUtils } from '@typescript-eslint/utils'; | ||
import { isIdentifier, isMakeStylesCallExpression, isObjectExpression, isProperty } from '../utils/helpers'; | ||
import { UNSUPPORTED_CSS_PROPERTIES, shorthandToArguments } from '../utils/shorthandToArguments'; | ||
import { getDocsUrl } from '../utils/getDocsUrl'; | ||
import { getShorthandValue, joinShorthandArguments } from '../utils/getShorthandValue'; | ||
export const RULE_NAME = 'no-shorthands'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.noShorthandsRule = exports.RULE_NAME = void 0; | ||
const utils_1 = require("@typescript-eslint/utils"); | ||
const helpers_1 = require("../utils/helpers"); | ||
const shorthandToArguments_1 = require("../utils/shorthandToArguments"); | ||
const getDocsUrl_1 = require("../utils/getDocsUrl"); | ||
const getShorthandValue_1 = require("../utils/getShorthandValue"); | ||
exports.RULE_NAME = 'no-shorthands'; | ||
function findShorthandProperties(node, isRoot = false, result = []) { | ||
for (const propertyNode of node.properties) { | ||
if (isProperty(propertyNode)) { | ||
if (isIdentifier(propertyNode.key) && !isRoot) { | ||
if (Object.prototype.hasOwnProperty.call(UNSUPPORTED_CSS_PROPERTIES, propertyNode.key.name)) { | ||
if ((0, helpers_1.isProperty)(propertyNode)) { | ||
if ((0, helpers_1.isIdentifier)(propertyNode.key) && !isRoot) { | ||
if (Object.prototype.hasOwnProperty.call(shorthandToArguments_1.UNSUPPORTED_CSS_PROPERTIES, propertyNode.key.name)) { | ||
result.push(propertyNode.key); | ||
} | ||
} | ||
if (isObjectExpression(propertyNode.value)) { | ||
if ((0, helpers_1.isObjectExpression)(propertyNode.value)) { | ||
findShorthandProperties(propertyNode.value, false, result); | ||
@@ -22,4 +25,4 @@ } | ||
} | ||
export const noShorthandsRule = ESLintUtils.RuleCreator(getDocsUrl)({ | ||
name: RULE_NAME, | ||
exports.noShorthandsRule = utils_1.ESLintUtils.RuleCreator(getDocsUrl_1.getDocsUrl)({ | ||
name: exports.RULE_NAME, | ||
meta: { | ||
@@ -42,16 +45,16 @@ type: 'problem', | ||
CallExpression(node) { | ||
if (isMakeStylesCallExpression(node, 'makeStyles')) { | ||
if ((0, helpers_1.isMakeStylesCallExpression)(node, 'makeStyles')) { | ||
const argument = node.arguments[0]; | ||
if (isObjectExpression(argument)) { | ||
if ((0, helpers_1.isObjectExpression)(argument)) { | ||
const shorthandProperties = findShorthandProperties(argument, true); | ||
shorthandProperties.forEach(shorthandProperty => { | ||
const propertyNode = shorthandProperty.parent; | ||
const shorthandLiteral = getShorthandValue(propertyNode.value, sourceCode); | ||
const shorthandLiteral = (0, getShorthandValue_1.getShorthandValue)(propertyNode.value, sourceCode); | ||
// Try to create an auto-fixer for the shorthand property. | ||
let fix; | ||
if (shorthandLiteral) { | ||
const autoFixArguments = shorthandToArguments(shorthandProperty.name, shorthandLiteral.value); | ||
const autoFixArguments = (0, shorthandToArguments_1.shorthandToArguments)(shorthandProperty.name, shorthandLiteral.value); | ||
if (autoFixArguments != null) { | ||
fix = fixer => { | ||
const args = joinShorthandArguments(autoFixArguments, shorthandLiteral.quote); | ||
const args = (0, getShorthandValue_1.joinShorthandArguments)(autoFixArguments, shorthandLiteral.quote); | ||
return fixer.replaceText(propertyNode, `...shorthands.${shorthandProperty.name}(${args})`); | ||
@@ -58,0 +61,0 @@ }; |
@@ -1,10 +0,13 @@ | ||
import { ESLintUtils } from '@typescript-eslint/utils'; | ||
import { getDocsUrl } from '../utils/getDocsUrl'; | ||
import { isMakeStylesCallExpression, isObjectExpression, isProperty, isStringLiteral } from '../utils/helpers'; | ||
export const RULE_NAME = 'pseudo-element-naming'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.pseudoElementNamingRule = exports.RULE_NAME = void 0; | ||
const utils_1 = require("@typescript-eslint/utils"); | ||
const getDocsUrl_1 = require("../utils/getDocsUrl"); | ||
const helpers_1 = require("../utils/helpers"); | ||
exports.RULE_NAME = 'pseudo-element-naming'; | ||
const PSEUDO_ELEMENTS = [':before', ':after']; | ||
function findInvalidPseudoElementProperties(node, isRoot = false, result = []) { | ||
for (const propertyNode of node.properties) { | ||
if (isProperty(propertyNode)) { | ||
if (isStringLiteral(propertyNode.key) && !isRoot) { | ||
if ((0, helpers_1.isProperty)(propertyNode)) { | ||
if ((0, helpers_1.isStringLiteral)(propertyNode.key) && !isRoot) { | ||
if (PSEUDO_ELEMENTS.includes(propertyNode.key.value)) { | ||
@@ -14,3 +17,3 @@ result.push(propertyNode.key); | ||
} | ||
if (isObjectExpression(propertyNode.value)) { | ||
if ((0, helpers_1.isObjectExpression)(propertyNode.value)) { | ||
findInvalidPseudoElementProperties(propertyNode.value, false, result); | ||
@@ -22,4 +25,4 @@ } | ||
} | ||
export const pseudoElementNamingRule = ESLintUtils.RuleCreator(getDocsUrl)({ | ||
name: RULE_NAME, | ||
exports.pseudoElementNamingRule = utils_1.ESLintUtils.RuleCreator(getDocsUrl_1.getDocsUrl)({ | ||
name: exports.RULE_NAME, | ||
meta: { | ||
@@ -46,5 +49,5 @@ fixable: 'code', | ||
const argument = node.arguments[0]; | ||
if (isObjectExpression(argument)) { | ||
const isMakeStyles = isMakeStylesCallExpression(node, 'makeStyles'); | ||
if (isMakeStyles || isMakeStylesCallExpression(node, 'makeResetStyles')) { | ||
if ((0, helpers_1.isObjectExpression)(argument)) { | ||
const isMakeStyles = (0, helpers_1.isMakeStylesCallExpression)(node, 'makeStyles'); | ||
if (isMakeStyles || (0, helpers_1.isMakeStylesCallExpression)(node, 'makeResetStyles')) { | ||
const invalidPseudoElementProperties = findInvalidPseudoElementProperties(argument, isMakeStyles); | ||
@@ -51,0 +54,0 @@ invalidPseudoElementProperties.forEach(invalidPseudoElementProperty => { |
@@ -1,4 +0,7 @@ | ||
import { ESLintUtils } from '@typescript-eslint/utils'; | ||
import { isMakeStylesCallExpression } from '../utils/helpers'; | ||
import { getDocsUrl } from '../utils/getDocsUrl'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.stylesFileRule = exports.RULE_NAME = void 0; | ||
const utils_1 = require("@typescript-eslint/utils"); | ||
const helpers_1 = require("../utils/helpers"); | ||
const getDocsUrl_1 = require("../utils/getDocsUrl"); | ||
const MATCHING_PACKAGES = new Set(['@fluentui/react-components', '@griffel/core', '@griffel/react']); | ||
@@ -32,5 +35,5 @@ const STYLES_FILE_PATTERN = /^.*\.(styles)\.[j|t]s$/; | ||
} | ||
export const RULE_NAME = 'styles-file'; | ||
export const stylesFileRule = ESLintUtils.RuleCreator(getDocsUrl)({ | ||
name: RULE_NAME, | ||
exports.RULE_NAME = 'styles-file'; | ||
exports.stylesFileRule = utils_1.ESLintUtils.RuleCreator(getDocsUrl_1.getDocsUrl)({ | ||
name: exports.RULE_NAME, | ||
meta: { | ||
@@ -60,3 +63,3 @@ docs: { | ||
if (isMakeStylesImported && | ||
isMakeStylesCallExpression(node, 'makeStyles', 'makeStaticStyles', 'makeResetStyles')) { | ||
(0, helpers_1.isMakeStylesCallExpression)(node, 'makeStyles', 'makeStaticStyles', 'makeResetStyles')) { | ||
if (!isStylesFile(fileName)) { | ||
@@ -77,3 +80,3 @@ context.report({ | ||
((_a = declaration.init) === null || _a === void 0 ? void 0 : _a.type) === 'CallExpression' && | ||
isMakeStylesCallExpression(declaration.init, 'makeStyles', 'makeStaticStyles', 'makeResetStyles')) { | ||
(0, helpers_1.isMakeStylesCallExpression)(declaration.init, 'makeStyles', 'makeStaticStyles', 'makeResetStyles')) { | ||
if (declaration.id.type === 'Identifier') { | ||
@@ -80,0 +83,0 @@ makeStylesDeclarations.push(declaration.id.name); |
@@ -1,2 +0,6 @@ | ||
export const getDocsUrl = (ruleName) => `https://github.com/microsoft/griffel/tree/main/packages/eslint-plugin/src/rules/${ruleName}.md`; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getDocsUrl = void 0; | ||
const getDocsUrl = (ruleName) => `https://github.com/microsoft/griffel/tree/main/packages/eslint-plugin/src/rules/${ruleName}.md`; | ||
exports.getDocsUrl = getDocsUrl; | ||
//# sourceMappingURL=getDocsUrl.js.map |
@@ -1,4 +0,7 @@ | ||
import { isLiteral, isTemplateLiteral } from './helpers'; | ||
export function getShorthandValue(node, sourceCode) { | ||
if (isLiteral(node)) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.joinShorthandArguments = exports.getShorthandValue = void 0; | ||
const helpers_1 = require("./helpers"); | ||
function getShorthandValue(node, sourceCode) { | ||
if ((0, helpers_1.isLiteral)(node)) { | ||
switch (typeof node.value) { | ||
@@ -13,3 +16,3 @@ case 'string': | ||
} | ||
else if (isTemplateLiteral(node)) { | ||
else if ((0, helpers_1.isTemplateLiteral)(node)) { | ||
const value = sourceCode.getText(node); | ||
@@ -23,5 +26,7 @@ return { | ||
} | ||
export function joinShorthandArguments(args, quote) { | ||
exports.getShorthandValue = getShorthandValue; | ||
function joinShorthandArguments(args, quote) { | ||
return args.map(arg => quote + arg + quote).join(', '); | ||
} | ||
exports.joinShorthandArguments = joinShorthandArguments; | ||
//# sourceMappingURL=getShorthandValue.js.map |
@@ -1,11 +0,15 @@ | ||
import { AST_NODE_TYPES, ASTUtils } from '@typescript-eslint/utils'; | ||
export const isIdentifier = ASTUtils.isIdentifier; | ||
export const isLiteral = ASTUtils.isNodeOfType(AST_NODE_TYPES.Literal); | ||
export const isMemberExpression = ASTUtils.isNodeOfType(AST_NODE_TYPES.MemberExpression); | ||
export const isObjectExpression = ASTUtils.isNodeOfType(AST_NODE_TYPES.ObjectExpression); | ||
export const isProperty = ASTUtils.isNodeOfType(AST_NODE_TYPES.Property); | ||
export const isTemplateLiteral = ASTUtils.isNodeOfType(AST_NODE_TYPES.TemplateLiteral); | ||
export function isStringLiteral(node) { | ||
return isLiteral(node) && typeof node.value === 'string'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isMakeStylesCallExpression = exports.isStringLiteral = exports.isTemplateLiteral = exports.isProperty = exports.isObjectExpression = exports.isMemberExpression = exports.isLiteral = exports.isIdentifier = void 0; | ||
const utils_1 = require("@typescript-eslint/utils"); | ||
exports.isIdentifier = utils_1.ASTUtils.isIdentifier; | ||
exports.isLiteral = utils_1.ASTUtils.isNodeOfType(utils_1.AST_NODE_TYPES.Literal); | ||
exports.isMemberExpression = utils_1.ASTUtils.isNodeOfType(utils_1.AST_NODE_TYPES.MemberExpression); | ||
exports.isObjectExpression = utils_1.ASTUtils.isNodeOfType(utils_1.AST_NODE_TYPES.ObjectExpression); | ||
exports.isProperty = utils_1.ASTUtils.isNodeOfType(utils_1.AST_NODE_TYPES.Property); | ||
exports.isTemplateLiteral = utils_1.ASTUtils.isNodeOfType(utils_1.AST_NODE_TYPES.TemplateLiteral); | ||
function isStringLiteral(node) { | ||
return (0, exports.isLiteral)(node) && typeof node.value === 'string'; | ||
} | ||
exports.isStringLiteral = isStringLiteral; | ||
/** | ||
@@ -17,13 +21,13 @@ * Checks if the given node is a `makeStyles` call expression. | ||
*/ | ||
export function isMakeStylesCallExpression(node, ...functionNames) { | ||
function isMakeStylesCallExpression(node, ...functionNames) { | ||
const { callee } = node; | ||
const names = new Set(functionNames); | ||
if (isIdentifier(callee)) { | ||
if ((0, exports.isIdentifier)(callee)) { | ||
// makeStyles({}) | ||
return names.has(callee.name); | ||
} | ||
else if (isMemberExpression(callee)) { | ||
else if ((0, exports.isMemberExpression)(callee)) { | ||
return ( | ||
// something.makeStyles({}) | ||
(isIdentifier(callee.property) && names.has(callee.property.name)) || | ||
((0, exports.isIdentifier)(callee.property) && names.has(callee.property.name)) || | ||
// something['makeStyles']({}) | ||
@@ -36,2 +40,3 @@ (isStringLiteral(callee.property) && names.has(callee.property.value))); | ||
} | ||
exports.isMakeStylesCallExpression = isMakeStylesCallExpression; | ||
//# sourceMappingURL=helpers.js.map |
@@ -0,1 +1,4 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.shorthandToArguments = exports.UNSUPPORTED_CSS_PROPERTIES = exports.buildShorthandSplitter = void 0; | ||
/** | ||
@@ -6,3 +9,3 @@ * Returns a function to split a string into an array of CSS functions and values. | ||
*/ | ||
export function buildShorthandSplitter(options = {}) { | ||
function buildShorthandSplitter(options = {}) { | ||
const { separator = ' ', numberUnit = '' } = options; | ||
@@ -64,5 +67,6 @@ return function split(value) { | ||
} | ||
exports.buildShorthandSplitter = buildShorthandSplitter; | ||
// This collection is a map simply for faster access when checking if a CSS property is unsupported | ||
// @griffel/core has the same definition, but ESLint plugin should not depend on it | ||
export const UNSUPPORTED_CSS_PROPERTIES = { | ||
exports.UNSUPPORTED_CSS_PROPERTIES = { | ||
all: true, | ||
@@ -169,6 +173,7 @@ animation: true, | ||
*/ | ||
export function shorthandToArguments(shorthandName, value) { | ||
function shorthandToArguments(shorthandName, value) { | ||
var _a; | ||
return (_a = SHORTHAND_FUNCTIONS[shorthandName]) === null || _a === void 0 ? void 0 : _a.call(SHORTHAND_FUNCTIONS, value); | ||
} | ||
exports.shorthandToArguments = shorthandToArguments; | ||
//# sourceMappingURL=shorthandToArguments.js.map |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
49884
3.11%708
5.83%No
NaN