-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(stylelint-plugin-meteor): add no primitive token rule
- Loading branch information
Showing
11 changed files
with
284 additions
and
16 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,5 @@ | ||
--- | ||
"@shopware-ag/stylelint-plugin-meteor": minor | ||
--- | ||
|
||
Add no-primitive-token rule |
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 |
---|---|---|
@@ -1,5 +1,11 @@ | ||
import preferSizingTokens from "./rules/prefer-sizing-token/index.js"; | ||
import preferBackgroundToken from "./rules/prefer-background-token/index.js"; | ||
import preferColorToken from "./prefer-color-token/index.js"; | ||
import noPrimitiveToken from "./rules/no-primitive-token/index.js"; | ||
|
||
export default [preferSizingTokens, preferColorToken, preferBackgroundToken]; | ||
export default [ | ||
preferSizingTokens, | ||
preferColorToken, | ||
preferBackgroundToken, | ||
noPrimitiveToken, | ||
]; |
3 changes: 3 additions & 0 deletions
3
packages/stylelint-plugin-meteor/src/rules/no-primitive-token/application/TokenGateway.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,3 @@ | ||
export interface TokenGateway { | ||
getTokens(): Promise<string[]>; | ||
} |
72 changes: 72 additions & 0 deletions
72
packages/stylelint-plugin-meteor/src/rules/no-primitive-token/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,72 @@ | ||
import stylelint, { Rule } from "stylelint"; | ||
import { TokenGateway } from "./application/TokenGateway.js"; | ||
import { TokenGatewayUsingImport } from "./infrastructure/TokenGatewayUsingImport.js"; | ||
|
||
const { | ||
createPlugin, | ||
utils: { report, ruleMessages, validateOptions }, | ||
} = stylelint; | ||
|
||
const ruleName = "meteor/no-primitive-token"; | ||
|
||
const messages = ruleMessages(ruleName, { | ||
rejected: (token, property) => | ||
`Unexpected primitve token "${token}" for ${property}, use a semantic token instead`, | ||
}); | ||
|
||
const meta = { | ||
url: "", | ||
}; | ||
|
||
export function makeNoPrimitiveRule(dependencies: { | ||
tokenGateway: TokenGateway; | ||
}) { | ||
const ruleFunction: Rule = (primary) => { | ||
return async (root, result) => { | ||
const validOptions = validateOptions(result, ruleName, { | ||
actual: primary, | ||
possible: [true], | ||
}); | ||
|
||
if (!validOptions) return; | ||
|
||
const tokens = await dependencies.tokenGateway.getTokens(); | ||
|
||
root.walkDecls((ruleNode) => { | ||
const values = ruleNode.value.split(" "); | ||
|
||
values.forEach((value) => { | ||
const usingACustomProperty = /^var\(.*\)$/.test(value); | ||
|
||
if (!usingACustomProperty) return; | ||
|
||
const token = value.replace(/^var\(--/, "").replace(/\)$/, ""); | ||
const usingAPrimitiveToken = tokens.includes(token); | ||
const isScaleToken = /^scale\-size\-\d+$/.test(token); | ||
|
||
if (usingAPrimitiveToken && !isScaleToken) { | ||
report({ | ||
message: messages.rejected(token, ruleNode.prop), | ||
node: ruleNode, | ||
result, | ||
ruleName, | ||
}); | ||
} | ||
}); | ||
}); | ||
}; | ||
}; | ||
|
||
ruleFunction.ruleName = ruleName; | ||
ruleFunction.messages = messages; | ||
ruleFunction.meta = meta; | ||
|
||
return ruleFunction; | ||
} | ||
|
||
export default createPlugin( | ||
ruleName, | ||
makeNoPrimitiveRule({ | ||
tokenGateway: new TokenGatewayUsingImport(), | ||
}) | ||
); |
9 changes: 9 additions & 0 deletions
9
...ylelint-plugin-meteor/src/rules/no-primitive-token/infrastructure/InMemoryTokenGateway.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,9 @@ | ||
import { TokenGateway } from "../application/TokenGateway.js"; | ||
|
||
export class InMemoryTokenGateway implements TokenGateway { | ||
constructor(public tokens: string[]) {} | ||
|
||
async getTokens(): Promise<string[]> { | ||
return this.tokens; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...lint-plugin-meteor/src/rules/no-primitive-token/infrastructure/TokenGatewayUsingImport.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,30 @@ | ||
import { TokenGateway } from "../application/TokenGateway.js"; | ||
|
||
const extractTokens = (obj: any, path: string[] = []): string[] => { | ||
let tokens: string[] = []; | ||
|
||
for (const [key, value] of Object.entries(obj)) { | ||
if (typeof value === "object" && value !== null && !("$value" in value)) { | ||
tokens = tokens.concat(extractTokens(value, [...path, key])); | ||
} else if ( | ||
typeof value === "object" && | ||
value !== null && | ||
"$value" in value | ||
) { | ||
tokens.push([...path, key].join("-")); | ||
} | ||
} | ||
|
||
return tokens; | ||
}; | ||
|
||
export class TokenGatewayUsingImport implements TokenGateway { | ||
async getTokens(): Promise<string[]> { | ||
const dictionary = await import( | ||
"@shopware-ag/meteor-tokens/foundation/primitives.json", | ||
{ with: { type: "json" } } | ||
); | ||
|
||
return extractTokens(dictionary.default); | ||
} | ||
} |
136 changes: 136 additions & 0 deletions
136
packages/stylelint-plugin-meteor/src/rules/no-primitive-token/no-primitive-token.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,136 @@ | ||
import { testRule } from "stylelint-test-rule-node"; | ||
import stylelint from "stylelint"; | ||
import { makeNoPrimitiveRule } from "./index.js"; | ||
import { InMemoryTokenGateway } from "./infrastructure/InMemoryTokenGateway.js"; | ||
|
||
const { createPlugin } = stylelint; | ||
|
||
const rule = makeNoPrimitiveRule({ | ||
tokenGateway: new InMemoryTokenGateway([ | ||
"green-500", | ||
"slate-300", | ||
"scale-size-4", | ||
]), | ||
}); | ||
|
||
const plugin = createPlugin(rule.ruleName, rule); | ||
|
||
testRule({ | ||
plugins: [plugin], | ||
ruleName: rule.ruleName, | ||
config: true, | ||
|
||
accept: [ | ||
{ | ||
code: "a { background-color: var(--color-interaction-primary-default); }", | ||
}, | ||
{ | ||
code: "a { background-color: currentcolor; }", | ||
}, | ||
{ | ||
code: "a { background-color: unset; }", | ||
}, | ||
{ | ||
code: "a { background-color: initial; }", | ||
}, | ||
{ | ||
code: "a { background-color: transparent; }", | ||
}, | ||
{ | ||
code: "a { background-color: red; }", | ||
}, | ||
{ | ||
code: "a { background-color: #fff; }", | ||
}, | ||
{ | ||
code: "a { background-color: rgb(1, 56, 56); }", | ||
}, | ||
{ | ||
code: "a { background-color: hsl(254, 45%, 60%); }", | ||
}, | ||
{ | ||
code: "a { background-color: var(--some-custom-property); }", | ||
}, | ||
{ | ||
code: "a { background-color: var(--scale-size-4); }", | ||
}, | ||
{ | ||
code: "a { margin: var(--scale-size-4) var(--scale-size-8); }", | ||
}, | ||
{ | ||
code: "a { border: 1px solid var(--color-border-brand-selected); }", | ||
}, | ||
{ | ||
code: "a { --foo: var(--scale-size-4); }", | ||
}, | ||
], | ||
|
||
reject: [ | ||
{ | ||
code: "a { background: var(--green-500); }", | ||
message: | ||
'Unexpected primitve token "green-500" for background, use a semantic token instead (meteor/no-primitive-token)', | ||
line: 1, | ||
endLine: 1, | ||
column: 5, | ||
endColumn: 34, | ||
}, | ||
{ | ||
code: "a { color: var(--slate-300); }", | ||
message: | ||
'Unexpected primitve token "slate-300" for color, use a semantic token instead (meteor/no-primitive-token)', | ||
line: 1, | ||
endLine: 1, | ||
column: 5, | ||
endColumn: 29, | ||
}, | ||
{ | ||
code: "a { border: 1px solid var(--green-500); }", | ||
message: | ||
'Unexpected primitve token "green-500" for border, use a semantic token instead (meteor/no-primitive-token)', | ||
line: 1, | ||
endLine: 1, | ||
column: 5, | ||
endColumn: 40, | ||
}, | ||
{ | ||
code: "a { margin: var(--scale-size-4) var(--green-500); }", | ||
message: | ||
'Unexpected primitve token "green-500" for margin, use a semantic token instead (meteor/no-primitive-token)', | ||
line: 1, | ||
endLine: 1, | ||
column: 5, | ||
endColumn: 50, | ||
}, | ||
{ | ||
code: "a { padding: var(--slate-300) var(--green-500); }", | ||
warnings: [ | ||
{ | ||
message: | ||
'Unexpected primitve token "slate-300" for padding, use a semantic token instead (meteor/no-primitive-token)', | ||
line: 1, | ||
column: 5, | ||
endLine: 1, | ||
endColumn: 48, | ||
}, | ||
{ | ||
message: | ||
'Unexpected primitve token "green-500" for padding, use a semantic token instead (meteor/no-primitive-token)', | ||
line: 1, | ||
column: 5, | ||
endLine: 1, | ||
endColumn: 48, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: "a { --foo: var(--green-500); }", | ||
message: | ||
'Unexpected primitve token "green-500" for --foo, use a semantic token instead (meteor/no-primitive-token)', | ||
line: 1, | ||
endLine: 1, | ||
column: 5, | ||
endColumn: 29, | ||
}, | ||
], | ||
}); |
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
Oops, something went wrong.