Skip to content

Commit

Permalink
Merge pull request #1 from storybookjs/feature/add-auto-formatting
Browse files Browse the repository at this point in the history
Add auto formatting with prettier + husky + lint-staged
  • Loading branch information
yannbf authored Oct 28, 2021
2 parents ff435fd + da70a53 commit c8be8de
Show file tree
Hide file tree
Showing 30 changed files with 573 additions and 161 deletions.
18 changes: 18 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# https://editorconfig.org
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
max_line_length = 80
trim_trailing_whitespace = true

[*.md]
max_line_length = 0
trim_trailing_whitespace = false

[COMMIT_EDITMSG]
max_line_length = 0
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ build
# don't lint nyc coverage output
coverage

tools
tools
10 changes: 9 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

module.exports = {
root: true,
extends: ['eslint:recommended', 'plugin:eslint-plugin/recommended', 'plugin:node/recommended'],
extends: [
'eslint:recommended',
'plugin:eslint-plugin/recommended',
'plugin:node/recommended',
'plugin:prettier/recommended',
],
env: {
node: true,
},
Expand All @@ -13,6 +18,9 @@ module.exports = {
jsx: true,
},
},
rules: {
'prettier/prettier': 'warn',
},
overrides: [
{
files: ['tests/**/*.js'],
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
.env.test.local
.env.production.local

.eslintcache

npm-debug.log*
yarn-debug.log*
yarn-error.log*
1 change: 1 addition & 0 deletions .husky/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx lint-staged
5 changes: 5 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.github
.husky
build
dist
node_modules
8 changes: 8 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
trailingComma: 'es5',
tabWidth: 2,
semi: false,
singleQuote: true,
printWidth: 100,
bracketSpacing: true,
}
24 changes: 12 additions & 12 deletions lib/configs/csf-strict.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
* in order to update it's content execute "yarn update-all"
*/
module.exports = {
extends: require.resolve("./csf"),
extends: require.resolve('./csf'),
rules: {
"import/no-anonymous-default-export": "off",
"storybook/csf-component": "warn",
"storybook/default-exports": "error",
"storybook/hierarchy-separator": "warn",
"storybook/meta-inline-properties": [
"error",
'import/no-anonymous-default-export': 'off',
'storybook/csf-component': 'warn',
'storybook/default-exports': 'error',
'storybook/hierarchy-separator': 'warn',
'storybook/meta-inline-properties': [
'error',
{
csfVersion: 3,
},
],
"storybook/no-redundant-story-name": "warn",
"storybook/prefer-pascal-case": "warn",
"storybook/no-stories-of": "error",
"storybook/no-title-property-in-meta": "error",
'storybook/no-redundant-story-name': 'warn',
'storybook/prefer-pascal-case': 'warn',
'storybook/no-stories-of': 'error',
'storybook/no-title-property-in-meta': 'error',
},
};
}
20 changes: 10 additions & 10 deletions lib/configs/csf.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
* in order to update it's content execute "yarn update-all"
*/
module.exports = {
plugins: ["storybook"],
plugins: ['storybook'],
rules: {
"import/no-anonymous-default-export": "off",
"storybook/csf-component": "warn",
"storybook/default-exports": "error",
"storybook/hierarchy-separator": "warn",
"storybook/meta-inline-properties": [
"error",
'import/no-anonymous-default-export': 'off',
'storybook/csf-component': 'warn',
'storybook/default-exports': 'error',
'storybook/hierarchy-separator': 'warn',
'storybook/meta-inline-properties': [
'error',
{
csfVersion: 3,
},
],
"storybook/no-redundant-story-name": "warn",
"storybook/prefer-pascal-case": "warn",
'storybook/no-redundant-story-name': 'warn',
'storybook/prefer-pascal-case': 'warn',
},
};
}
11 changes: 5 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@
* @fileoverview Best practice rules for Storybook
* @author Yann Braga
*/
"use strict";
'use strict'

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const requireIndex = require("requireindex");
const requireIndex = require('requireindex')

//------------------------------------------------------------------------------
// Plugin Definition
//------------------------------------------------------------------------------


// import all rules in lib/rules
module.exports = {
rules: requireIndex(__dirname + "/rules"),
configs: requireIndex(__dirname + "/configs")
};
rules: requireIndex(__dirname + '/rules'),
configs: requireIndex(__dirname + '/configs'),
}
14 changes: 7 additions & 7 deletions lib/rules/csf-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* @fileoverview Component property should be set
* @author Yann Braga
*/
"use strict";
'use strict'

const { docsUrl } = require("../utils");
const { docsUrl } = require('../utils')

//------------------------------------------------------------------------------
// Rule Definition
Expand All @@ -14,14 +14,14 @@ module.exports = {
meta: {
type: 'suggestion',
docs: {
description: "The component property should be set",
description: 'The component property should be set',
categories: ['csf'],
recommended: false,
recommendedConfig: 'warn',
url: docsUrl('csf-component'), // URL to the documentation page for this rule
},
messages: {
description: 'Missing component property.'
description: 'Missing component property.',
},
fixable: null, // Or `code` or `whitespace`
schema: [], // Add a schema if the rule has options
Expand Down Expand Up @@ -54,10 +54,10 @@ module.exports = {
if (!component) {
context.report({
node,
messageId: 'description'
messageId: 'description',
})
}
},
};
}
},
};
}
4 changes: 2 additions & 2 deletions lib/rules/default-exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
'use strict'

const { docsUrl } = require("../utils")
const { docsUrl } = require('../utils')

//------------------------------------------------------------------------------
// Rule Definition
Expand All @@ -21,7 +21,7 @@ module.exports = {
url: docsUrl('default-exports'), // URL to the documentation page for this rule
},
messages: {
description: 'The file should have a default export.'
description: 'The file should have a default export.',
},
},

Expand Down
12 changes: 5 additions & 7 deletions lib/rules/hierarchy-separator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
'use strict'

const { docsUrl } = require("../utils")
const { docsUrl } = require('../utils')

//------------------------------------------------------------------------------
// Rule Definition
Expand All @@ -23,7 +23,8 @@ module.exports = {
},
messages: {
fixSuggestion: 'Use correct separators',
deprecatedHierarchySeparator: 'Deprecated hierachy separator in title property: {{metaTitle}}.',
deprecatedHierarchySeparator:
'Deprecated hierachy separator in title property: {{metaTitle}}.',
},
},
create: function (context) {
Expand All @@ -39,7 +40,7 @@ module.exports = {
const titleNode = metaProperties.find((prop) => prop.key.name === 'title')

if (!titleNode) {
return;
return
}

const metaTitle = titleNode.value.raw
Expand All @@ -51,10 +52,7 @@ module.exports = {
data: { metaTitle },
// In case we want this to be auto fixed by --fix
fix: function (fixer) {
return fixer.replaceTextRange(
titleNode.value.range,
metaTitle.replace(/\|/g, '/')
)
return fixer.replaceTextRange(titleNode.value.range, metaTitle.replace(/\|/g, '/'))
},
suggest: [
{
Expand Down
45 changes: 22 additions & 23 deletions lib/rules/meta-inline-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* @fileoverview Meta should have inline properties
* @author Yann Braga
*/
"use strict";
'use strict'

const { docsUrl } = require("../utils");
const { docsUrl } = require('../utils')

//------------------------------------------------------------------------------
// Rule Definition
Expand All @@ -14,31 +14,31 @@ module.exports = {
meta: {
type: 'problem',
docs: {
description: "Meta should only have inline properties",
category: "csf",
description: 'Meta should only have inline properties',
category: 'csf',
recommended: true,
recommendedConfig: ['error', { csfVersion: 3 }],
url: docsUrl('meta-inline-properties'), // URL to the documentation page for this rule
},
messages: {
description: 'Meta should only have inline properties: {{properties}}'
description: 'Meta should only have inline properties: {{properties}}',
},
schema: [
{
type: 'object',
properties: {
csfVersion: {
type: 'number'
}
type: 'number',
},
},
additionalProperties: false
}
additionalProperties: false,
},
],
},

create(context) {
// variables should be defined here

// In case we need to get options defined in the rule schema
// const options = context.options[0] || {}
// const csfVersion = options.csfVersion
Expand All @@ -64,14 +64,13 @@ module.exports = {
const ruleProperties = ['title', 'args']
let dynamicProperties = []

const metaNodes = metaProperties.filter(
(prop) => ruleProperties.includes(prop.key.name)
);
const metaNodes = metaProperties.filter((prop) => ruleProperties.includes(prop.key.name))

metaNodes.forEach(metaNode => {
const isDynamic = metaNode.shorthand
|| metaNode.value.type === 'BinaryExpression'
|| metaNode.value.type === 'TemplateLiteral' && metaNode.value.expressions.length > 0
metaNodes.forEach((metaNode) => {
const isDynamic =
metaNode.shorthand ||
metaNode.value.type === 'BinaryExpression' ||
(metaNode.value.type === 'TemplateLiteral' && metaNode.value.expressions.length > 0)

if (isDynamic) {
dynamicProperties.push(metaNode.key.name)
Expand All @@ -84,11 +83,11 @@ module.exports = {
loc: node.loc,
messageId: 'description',
data: {
properties: dynamicProperties.join(', ')
}
});
properties: dynamicProperties.join(', '),
},
})
}
}
};
},
}
},
};
}
Loading

0 comments on commit c8be8de

Please sign in to comment.