-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
63b3284
commit 309ca9b
Showing
23 changed files
with
578 additions
and
453 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
'@sourcegraph/eslint-plugin-sourcegraph': patch | ||
'@sourcegraph/stylelint-plugin-sourcegraph': patch | ||
'@sourcegraph/codemod-toolkit-css': patch | ||
'@sourcegraph/codemod-transforms': patch | ||
--- | ||
|
||
Upgraded `stylelint` version. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# stylelint-plugin-sourcegraph | ||
|
||
Recommended Stylelint rules for the Sourcegraph repo. | ||
|
||
## Setup | ||
|
||
Update your `.stylelintrc.json` file to add the following configuration: | ||
|
||
```json | ||
{ | ||
"plugins": ["@sourcegraph/stylelint-plugin-sourcegraph"], | ||
"rules": { | ||
"@sourcegraph/filenames-match-regex": [ | ||
2, | ||
{ | ||
"regexp": "^.+\\.module(\\.scss)$" | ||
} | ||
], | ||
"@sourcegraph/no-restricted-imports": [ | ||
2, | ||
{ | ||
"paths": ["bootstrap*", "reactstrap/styles.css"] | ||
} | ||
] | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* eslint-disable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires */ | ||
import type { InitialOptionsTsJest } from 'ts-jest' | ||
|
||
import baseConfig from '../../jest.config.base' | ||
|
||
const stylelintPreset = require('jest-preset-stylelint/jest-preset') | ||
const tsPreset = require('ts-jest/jest-preset') | ||
|
||
const config: InitialOptionsTsJest = { | ||
...baseConfig, | ||
...tsPreset, | ||
...stylelintPreset, | ||
displayName: 'stylelint-plugin', | ||
rootDir: __dirname, | ||
setupFiles: ['./jest.setup.ts'], | ||
} | ||
|
||
// eslint-disable-next-line import/no-default-export | ||
export default config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import path from 'path' | ||
|
||
import { getTestRule } from 'jest-preset-stylelint' | ||
|
||
global.testRule = getTestRule({ plugins: [path.join(__dirname, './src')] }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
{ | ||
"name": "@sourcegraph/stylelint-plugin-sourcegraph", | ||
"version": "1.0.0", | ||
"description": "Custom stylelint rules for Sourcegraph", | ||
"keywords": [ | ||
"stylelint", | ||
"stylelintplugin", | ||
"stylelint-plugin", | ||
"sourcegraph", | ||
"typescript" | ||
], | ||
"files": [ | ||
"dist", | ||
"docs", | ||
"index.d.ts", | ||
"package.json", | ||
"README.md", | ||
"LICENSE" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/sourcegraph/codemod.git", | ||
"directory": "packages/stylelint-plugin" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/sourcegraph/codemod/issues" | ||
}, | ||
"license": "MIT", | ||
"main": "dist/index.js", | ||
"types": "index.d.ts", | ||
"scripts": { | ||
"build": "tsc --build ./tsconfig.build.json", | ||
"build:watch": "tsc --build ./tsconfig.build.json --watch", | ||
"build:clean": "tsc --build --clean ./tsconfig.build.json && rimraf dist ./*.tsbuildinfo", | ||
"typecheck": "tsc --noEmit", | ||
"test": "jest", | ||
"format": "prettier --write \"./**/*.{ts,js,json,md}\" --ignore-path ../../.prettierignore", | ||
"generate:configs": "ts-node --files --transpile-only scripts/generate-configs.ts", | ||
"lint": "eslint . --ext .js,.ts --ignore-path ../../.eslintignore" | ||
}, | ||
"dependencies": { | ||
"@manypkg/find-root": "^1.1.0", | ||
"ignore": "^5.1.8", | ||
"postcss-value-parser": "^4.2.0" | ||
}, | ||
"devDependencies": { | ||
"jest-preset-stylelint": "^5.0.3" | ||
}, | ||
"peerDependencies": { | ||
"stylelint": "^14.3.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { filenameMatchRegexRule } from './rules/filenames-match-regex/filenames-match-regex' | ||
import { noRestrictedImports } from './rules/no-restricted-imports/no-restricted-imports' | ||
|
||
// eslint-disable-next-line import/no-default-export | ||
export default [filenameMatchRegexRule, noRestrictedImports] |
28 changes: 28 additions & 0 deletions
28
.../stylelint-plugin/src/rules/filenames-match-regex/__tests__/filenames-match-regex.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { messages, ruleName } from '../filenames-match-regex' | ||
|
||
const ruleConfig = { regexp: '^.+\\.module(\\.css)$' } | ||
|
||
testRule({ | ||
ruleName, | ||
config: [true, ruleConfig], | ||
codeFilename: 'test.module.css', | ||
accept: [ | ||
{ | ||
code: 'div {}', | ||
description: 'CSS module file', | ||
}, | ||
], | ||
}) | ||
|
||
testRule({ | ||
ruleName, | ||
config: [true, ruleConfig], | ||
codeFilename: 'test.css', | ||
reject: [ | ||
{ | ||
code: 'div {}', | ||
description: 'global CSS file', | ||
message: messages.expected('test.css'), | ||
}, | ||
], | ||
}) |
70 changes: 70 additions & 0 deletions
70
packages/stylelint-plugin/src/rules/filenames-match-regex/filenames-match-regex.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import path from 'path' | ||
|
||
import stylelint from 'stylelint' | ||
|
||
import { createPlugin, isString } from '../../utils' | ||
|
||
export const ruleName = '@sourcegraph/filenames-match-regex' | ||
export const messages = stylelint.utils.ruleMessages(ruleName, { | ||
expected: fileName => { | ||
return `Filename ${String(fileName)} does not match the regular expression.` | ||
}, | ||
}) | ||
|
||
interface RuleConfig { | ||
regexp: string | ||
} | ||
|
||
export const filenameMatchRegexRule = createPlugin<RuleConfig>(ruleName, (isEnabled, config) => { | ||
if (!config) { | ||
throw new Error(`The ${ruleName} rule configuration is not provided!`) | ||
} | ||
|
||
const regexp = new RegExp(config.regexp) | ||
|
||
return function (postcssRoot, postcssResult) { | ||
if (!isEnabled) { | ||
return | ||
} | ||
|
||
const areOptionsValid = stylelint.utils.validateOptions( | ||
postcssResult, | ||
ruleName, | ||
{ | ||
actual: isEnabled, | ||
optional: false, | ||
possible: [true, false], | ||
}, | ||
{ | ||
actual: config, | ||
optional: false, | ||
possible: { | ||
regexp: [isString], | ||
}, | ||
} | ||
) | ||
|
||
if (!areOptionsValid) { | ||
return | ||
} | ||
|
||
const filePath = postcssRoot.source?.input.from | ||
|
||
if (!filePath || !postcssRoot.first) { | ||
return | ||
} | ||
|
||
const fileName = path.basename(filePath) | ||
const isRegexMatch = regexp.test(fileName) | ||
|
||
if (!isRegexMatch) { | ||
stylelint.utils.report({ | ||
message: messages.expected(fileName), | ||
ruleName, | ||
node: postcssRoot.first, | ||
result: postcssResult, | ||
line: 1, | ||
}) | ||
} | ||
} | ||
}) |
1 change: 1 addition & 0 deletions
1
packages/stylelint-plugin/src/rules/filenames-match-regex/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './filenames-match-regex' |
38 changes: 38 additions & 0 deletions
38
.../stylelint-plugin/src/rules/no-restricted-imports/__tests__/no-restricted-imports.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { messages, ruleName } from '../no-restricted-imports' | ||
|
||
testRule({ | ||
ruleName, | ||
config: [true, { paths: ['bootstrap*', 'reactstrap/index.css'] }], | ||
codeFilename: 'packages/stylelint-plugin/test.css', | ||
accept: [ | ||
{ | ||
code: '@import "reactstrap/not-banned.css"', | ||
description: 'Absolute import', | ||
}, | ||
{ | ||
code: '@import "./branded/src/global-styles/index.scss";', | ||
description: 'Relative import', | ||
}, | ||
{ | ||
code: '@import "../../branded/src/global-styles/index.scss";', | ||
description: 'Deep relative import', | ||
}, | ||
], | ||
reject: [ | ||
{ | ||
code: '@import "reactstrap/index.css";', | ||
description: 'Banned import exact path', | ||
message: messages.rejected('reactstrap/index.css'), | ||
}, | ||
{ | ||
code: '@import "bootstrap";', | ||
description: 'Banned import via glob', | ||
message: messages.rejected('bootstrap'), | ||
}, | ||
{ | ||
code: '@import "bootstrap/scss/functions";', | ||
description: 'Banned nested import via glob', | ||
message: messages.rejected('bootstrap/scss/functions'), | ||
}, | ||
], | ||
}) |
1 change: 1 addition & 0 deletions
1
packages/stylelint-plugin/src/rules/no-restricted-imports/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './no-restricted-imports' |
Oops, something went wrong.