@pragmatiks/lint 0.11.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -0
- package/README.md +17 -0
- package/dist/eslint-config/next/index.d.ts +8 -1
- package/dist/eslint-config/next/index.js +104 -24
- package/dist/eslint-config/next/index.js.map +1 -1
- package/dist/eslint-config/unit-test/index.d.ts +51 -0
- package/dist/eslint-config/unit-test/index.js +77 -0
- package/dist/eslint-config/unit-test/index.js.map +1 -0
- package/package.json +5 -1
- package/src/semgrep-rules/pra_no_underscore_members.yml +33 -0
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Linter } from 'eslint';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The `@pragmatiks/lint/eslint-config/unit-test` entry: bans on mocks, fakes and I/O in unit tests.
|
|
5
|
+
*
|
|
6
|
+
* @packageDocumentation
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
type RestrictedImportPath = {
|
|
10
|
+
name: string;
|
|
11
|
+
message: string;
|
|
12
|
+
importNames?: string[];
|
|
13
|
+
};
|
|
14
|
+
type RestrictedImportPattern = {
|
|
15
|
+
group: string[];
|
|
16
|
+
message: string;
|
|
17
|
+
};
|
|
18
|
+
type RestrictedProperty = {
|
|
19
|
+
object: string;
|
|
20
|
+
property: string;
|
|
21
|
+
message: string;
|
|
22
|
+
};
|
|
23
|
+
type RestrictedSyntax = {
|
|
24
|
+
selector: string;
|
|
25
|
+
message: string;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* The restricted lists of the base config a consumer uses, which the unit-test config keeps.
|
|
29
|
+
*/
|
|
30
|
+
interface InheritedBans {
|
|
31
|
+
readonly imports?: {
|
|
32
|
+
paths: RestrictedImportPath[];
|
|
33
|
+
patterns: RestrictedImportPattern[];
|
|
34
|
+
};
|
|
35
|
+
readonly properties?: RestrictedProperty[];
|
|
36
|
+
readonly syntax?: RestrictedSyntax[];
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* The files the unit-test config applies to by default.
|
|
40
|
+
*/
|
|
41
|
+
declare const PRAGMATIKS_UNIT_TEST_FILES: string[];
|
|
42
|
+
/**
|
|
43
|
+
* Builds the flat config object that bans vi mock, stub and fake-timer helpers, msw,
|
|
44
|
+
* Node I/O modules (static, `require()` and `import()`) and network globals in unit tests.
|
|
45
|
+
*
|
|
46
|
+
* @param inherited - The base config's restricted lists to keep, since a later flat config replaces a rule's options; `pragmatiksNextUnitTestConfig` passes the Next ones.
|
|
47
|
+
* @returns The config object with the `no-restricted-*` rules set.
|
|
48
|
+
*/
|
|
49
|
+
declare function pragmatiksUnitTestConfig(inherited?: InheritedBans): Linter.Config;
|
|
50
|
+
|
|
51
|
+
export { type InheritedBans, PRAGMATIKS_UNIT_TEST_FILES, pragmatiksUnitTestConfig as default, pragmatiksUnitTestConfig };
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// src/eslint-config/unit-test/index.ts
|
|
2
|
+
var PRAGMATIKS_UNIT_TEST_FILES = ["tests/unit/**"];
|
|
3
|
+
var MOCK_MESSAGE = "Unit tests use no mocks, stubs or fakes; test a pure decision with real inputs.";
|
|
4
|
+
var IO_MESSAGE = "Unit tests perform no I/O; test a pure decision with real inputs.";
|
|
5
|
+
var BANNED_VI_PROPERTIES = [
|
|
6
|
+
"mock",
|
|
7
|
+
"doMock",
|
|
8
|
+
"spyOn",
|
|
9
|
+
"fn",
|
|
10
|
+
"stubGlobal",
|
|
11
|
+
"stubEnv",
|
|
12
|
+
"useFakeTimers",
|
|
13
|
+
"setSystemTime"
|
|
14
|
+
];
|
|
15
|
+
var BANNED_NODE_MODULES = [
|
|
16
|
+
"fs",
|
|
17
|
+
"fs/promises",
|
|
18
|
+
"child_process",
|
|
19
|
+
"net",
|
|
20
|
+
"http",
|
|
21
|
+
"https",
|
|
22
|
+
"dgram",
|
|
23
|
+
"dns",
|
|
24
|
+
"dns/promises",
|
|
25
|
+
"http2",
|
|
26
|
+
"tls",
|
|
27
|
+
"process",
|
|
28
|
+
"worker_threads"
|
|
29
|
+
];
|
|
30
|
+
var BANNED_GLOBALS = ["fetch", "XMLHttpRequest", "WebSocket", "EventSource", "process"];
|
|
31
|
+
var GLOBAL_OBJECTS = ["globalThis", "window", "self"];
|
|
32
|
+
var SLASH = String.raw`\u002F`;
|
|
33
|
+
var MODULE_PATTERN = `/^((node:)?(${BANNED_NODE_MODULES.map((module) => module.replace("/", SLASH)).join("|")})|msw(${SLASH}.*)?)$/`;
|
|
34
|
+
var DYNAMIC_LOAD_SYNTAX = [
|
|
35
|
+
{
|
|
36
|
+
selector: `CallExpression[callee.name="require"] > Literal.arguments[value=${MODULE_PATTERN}]`,
|
|
37
|
+
message: IO_MESSAGE
|
|
38
|
+
},
|
|
39
|
+
{ selector: `ImportExpression > Literal.source[value=${MODULE_PATTERN}]`, message: IO_MESSAGE }
|
|
40
|
+
];
|
|
41
|
+
function pragmatiksUnitTestConfig(inherited = {}) {
|
|
42
|
+
return {
|
|
43
|
+
name: "pragmatiks/unit-test",
|
|
44
|
+
files: PRAGMATIKS_UNIT_TEST_FILES,
|
|
45
|
+
rules: {
|
|
46
|
+
"no-restricted-properties": [
|
|
47
|
+
"error",
|
|
48
|
+
...inherited.properties ?? [],
|
|
49
|
+
...BANNED_VI_PROPERTIES.map((property) => ({ object: "vi", property, message: MOCK_MESSAGE })),
|
|
50
|
+
...GLOBAL_OBJECTS.flatMap(
|
|
51
|
+
(object) => BANNED_GLOBALS.map((property) => ({ object, property, message: IO_MESSAGE }))
|
|
52
|
+
)
|
|
53
|
+
],
|
|
54
|
+
"no-restricted-imports": [
|
|
55
|
+
"error",
|
|
56
|
+
{
|
|
57
|
+
paths: [
|
|
58
|
+
...inherited.imports?.paths ?? [],
|
|
59
|
+
{ name: "msw", message: MOCK_MESSAGE },
|
|
60
|
+
...BANNED_NODE_MODULES.flatMap((module) => [
|
|
61
|
+
{ name: module, message: IO_MESSAGE },
|
|
62
|
+
{ name: `node:${module}`, message: IO_MESSAGE }
|
|
63
|
+
])
|
|
64
|
+
],
|
|
65
|
+
patterns: [...inherited.imports?.patterns ?? [], { group: ["msw/*"], message: MOCK_MESSAGE }]
|
|
66
|
+
}
|
|
67
|
+
],
|
|
68
|
+
"no-restricted-syntax": ["error", ...inherited.syntax ?? [], ...DYNAMIC_LOAD_SYNTAX],
|
|
69
|
+
"no-restricted-globals": ["error", ...BANNED_GLOBALS.map((name) => ({ name, message: IO_MESSAGE }))]
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
var unit_test_default = pragmatiksUnitTestConfig;
|
|
74
|
+
|
|
75
|
+
export { PRAGMATIKS_UNIT_TEST_FILES, unit_test_default as default, pragmatiksUnitTestConfig };
|
|
76
|
+
//# sourceMappingURL=index.js.map
|
|
77
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/eslint-config/unit-test/index.ts"],"names":[],"mappings":";AA2BO,IAAM,0BAAA,GAA6B,CAAC,eAAe;AAE1D,IAAM,YAAA,GAAe,iFAAA;AAErB,IAAM,UAAA,GAAa,mEAAA;AAEnB,IAAM,oBAAA,GAAuB;AAAA,EAC3B,MAAA;AAAA,EACA,QAAA;AAAA,EACA,OAAA;AAAA,EACA,IAAA;AAAA,EACA,YAAA;AAAA,EACA,SAAA;AAAA,EACA,eAAA;AAAA,EACA;AACF,CAAA;AAEA,IAAM,mBAAA,GAAsB;AAAA,EAC1B,IAAA;AAAA,EACA,aAAA;AAAA,EACA,eAAA;AAAA,EACA,KAAA;AAAA,EACA,MAAA;AAAA,EACA,OAAA;AAAA,EACA,OAAA;AAAA,EACA,KAAA;AAAA,EACA,cAAA;AAAA,EACA,OAAA;AAAA,EACA,KAAA;AAAA,EACA,SAAA;AAAA,EACA;AACF,CAAA;AAEA,IAAM,iBAAiB,CAAC,OAAA,EAAS,gBAAA,EAAkB,WAAA,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AAA,YACL,GAAI,SAAA,CAAU,OAAA,EAAS,KAAA,IAAS,EAAC;AAAA,YACjC,EAAE,IAAA,EAAM,KAAA,EAAO,OAAA,EAAS,YAAA,EAAa;AAAA,YACrC,GAAG,mBAAA,CAAoB,OAAA,CAAQ,CAAC,MAAA,KAAW;AAAA,cACzC,EAAE,IAAA,EAAM,MAAA,EAAQ,OAAA,EAAS,UAAA,EAAW;AAAA,cACpC,EAAE,IAAA,EAAM,CAAA,KAAA,EAAQ,MAAM,CAAA,CAAA,EAAI,SAAS,UAAA;AAAW,aAC/C;AAAA,WACH;AAAA,UACA,QAAA,EAAU,CAAC,GAAI,SAAA,CAAU,SAAS,QAAA,IAAY,EAAC,EAAI,EAAE,OAAO,CAAC,OAAO,CAAA,EAAG,OAAA,EAAS,cAAc;AAAA;AAChG,OACF;AAAA,MACA,sBAAA,EAAwB,CAAC,OAAA,EAAS,GAAI,UAAU,MAAA,IAAU,EAAC,EAAI,GAAG,mBAAmB,CAAA;AAAA,MACrF,uBAAA,EAAyB,CAAC,OAAA,EAAS,GAAG,cAAA,CAAe,GAAA,CAAI,CAAC,IAAA,MAAU,EAAE,IAAA,EAAM,OAAA,EAAS,UAAA,GAAa,CAAC;AAAA;AACrG,GACF;AACF;AAEA,IAAO,iBAAA,GAAQ","file":"index.js","sourcesContent":["/**\n * The `@pragmatiks/lint/eslint-config/unit-test` entry: bans on mocks, fakes and I/O in unit tests.\n *\n * @packageDocumentation\n */\nimport type { Linter } from \"eslint\";\n\ntype RestrictedImportPath = { name: string; message: string; importNames?: string[] };\n\ntype RestrictedImportPattern = { group: string[]; message: string };\n\ntype RestrictedProperty = { object: string; property: string; message: string };\n\ntype RestrictedSyntax = { selector: string; message: string };\n\n/**\n * The restricted lists of the base config a consumer uses, which the unit-test config keeps.\n */\nexport interface InheritedBans {\n readonly imports?: { paths: RestrictedImportPath[]; patterns: RestrictedImportPattern[] };\n readonly properties?: RestrictedProperty[];\n readonly syntax?: RestrictedSyntax[];\n}\n\n/**\n * The files the unit-test config applies to by default.\n */\nexport const PRAGMATIKS_UNIT_TEST_FILES = [\"tests/unit/**\"];\n\nconst MOCK_MESSAGE = \"Unit tests use no mocks, stubs or fakes; test a pure decision with real inputs.\";\n\nconst IO_MESSAGE = \"Unit tests perform no I/O; test a pure decision with real inputs.\";\n\nconst BANNED_VI_PROPERTIES = [\n \"mock\",\n \"doMock\",\n \"spyOn\",\n \"fn\",\n \"stubGlobal\",\n \"stubEnv\",\n \"useFakeTimers\",\n \"setSystemTime\",\n];\n\nconst BANNED_NODE_MODULES = [\n \"fs\",\n \"fs/promises\",\n \"child_process\",\n \"net\",\n \"http\",\n \"https\",\n \"dgram\",\n \"dns\",\n \"dns/promises\",\n \"http2\",\n \"tls\",\n \"process\",\n \"worker_threads\",\n];\n\nconst BANNED_GLOBALS = [\"fetch\", \"XMLHttpRequest\", \"WebSocket\", \"EventSource\", \"process\"];\n\nconst GLOBAL_OBJECTS = [\"globalThis\", \"window\", \"self\"];\n\nconst SLASH = String.raw`\\u002F`;\n\nconst MODULE_PATTERN = `/^((node:)?(${BANNED_NODE_MODULES.map((module) => module.replace(\"/\", SLASH)).join(\"|\")})|msw(${SLASH}.*)?)$/`;\n\nconst DYNAMIC_LOAD_SYNTAX = [\n {\n selector: `CallExpression[callee.name=\"require\"] > Literal.arguments[value=${MODULE_PATTERN}]`,\n message: IO_MESSAGE,\n },\n { selector: `ImportExpression > Literal.source[value=${MODULE_PATTERN}]`, message: IO_MESSAGE },\n];\n\n/**\n * Builds the flat config object that bans vi mock, stub and fake-timer helpers, msw,\n * Node I/O modules (static, `require()` and `import()`) and network globals in unit tests.\n *\n * @param inherited - The base config's restricted lists to keep, since a later flat config replaces a rule's options; `pragmatiksNextUnitTestConfig` passes the Next ones.\n * @returns The config object with the `no-restricted-*` rules set.\n */\nexport function pragmatiksUnitTestConfig(inherited: InheritedBans = {}): Linter.Config {\n return {\n name: \"pragmatiks/unit-test\",\n files: PRAGMATIKS_UNIT_TEST_FILES,\n rules: {\n \"no-restricted-properties\": [\n \"error\",\n ...(inherited.properties ?? []),\n ...BANNED_VI_PROPERTIES.map((property) => ({ object: \"vi\", property, message: MOCK_MESSAGE })),\n ...GLOBAL_OBJECTS.flatMap((object) =>\n BANNED_GLOBALS.map((property) => ({ object, property, message: IO_MESSAGE })),\n ),\n ],\n \"no-restricted-imports\": [\n \"error\",\n {\n paths: [\n ...(inherited.imports?.paths ?? []),\n { name: \"msw\", message: MOCK_MESSAGE },\n ...BANNED_NODE_MODULES.flatMap((module) => [\n { name: module, message: IO_MESSAGE },\n { name: `node:${module}`, message: IO_MESSAGE },\n ]),\n ],\n patterns: [...(inherited.imports?.patterns ?? []), { group: [\"msw/*\"], message: MOCK_MESSAGE }],\n },\n ],\n \"no-restricted-syntax\": [\"error\", ...(inherited.syntax ?? []), ...DYNAMIC_LOAD_SYNTAX],\n \"no-restricted-globals\": [\"error\", ...BANNED_GLOBALS.map((name) => ({ name, message: IO_MESSAGE }))],\n },\n };\n}\n\nexport default pragmatiksUnitTestConfig;\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pragmatiks/lint",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"description": "Pragmatiks engineering principles eslint config and bundled semgrep rules.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -22,6 +22,10 @@
|
|
|
22
22
|
"./eslint-config/css": {
|
|
23
23
|
"types": "./dist/eslint-config/css/index.d.ts",
|
|
24
24
|
"import": "./dist/eslint-config/css/index.js"
|
|
25
|
+
},
|
|
26
|
+
"./eslint-config/unit-test": {
|
|
27
|
+
"types": "./dist/eslint-config/unit-test/index.d.ts",
|
|
28
|
+
"import": "./dist/eslint-config/unit-test/index.js"
|
|
25
29
|
}
|
|
26
30
|
},
|
|
27
31
|
"files": [
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
rules:
|
|
2
|
+
- id: pra-no-underscore-members
|
|
3
|
+
pattern-either:
|
|
4
|
+
- patterns:
|
|
5
|
+
- patterns:
|
|
6
|
+
- pattern-either:
|
|
7
|
+
- pattern: $NAME = $VALUE
|
|
8
|
+
- pattern: "..., $NAME, ... = $VALUE"
|
|
9
|
+
- pattern: "[..., $NAME, ...] = $VALUE"
|
|
10
|
+
- pattern: "$NAME: $TYPE = $VALUE"
|
|
11
|
+
- pattern: "$NAME: $TYPE"
|
|
12
|
+
- pattern: $OBJECT.$NAME = $VALUE
|
|
13
|
+
- pattern: "..., $OBJECT.$NAME, ... = $VALUE"
|
|
14
|
+
- pattern: "$OBJECT.$NAME: $TYPE = $VALUE"
|
|
15
|
+
- metavariable-regex:
|
|
16
|
+
metavariable: $NAME
|
|
17
|
+
regex: '^(?!.*[^_]_$)_(?P<STEM>[a-z]\w*)$'
|
|
18
|
+
- pattern-not-inside: |
|
|
19
|
+
class $CLASS:
|
|
20
|
+
...
|
|
21
|
+
def $STEM(...): ...
|
|
22
|
+
- patterns:
|
|
23
|
+
- pattern-either:
|
|
24
|
+
- pattern: "def $NAME(...): ..."
|
|
25
|
+
- pattern: "async def $NAME(...): ..."
|
|
26
|
+
- metavariable-regex:
|
|
27
|
+
metavariable: $NAME
|
|
28
|
+
regex: '^(?!.*[^_]_$)_[a-z]\w*$'
|
|
29
|
+
message: "principle #13 leading underscore with no same-named public property or accessor in the class. Use the plain public name; keep the underscore only on a backing field behind a same-named property."
|
|
30
|
+
severity: WARNING
|
|
31
|
+
languages: [python]
|
|
32
|
+
metadata:
|
|
33
|
+
note: "A field target is allowlisted when its class defines a method named like it without the underscore (a property, setter or accessor). Parameters, dunders, sunders such as Enum's _missing_, self, cls and the throwaway _ are never flagged; a field backing a property inherited from a base class is flagged and needs reviewer judgment."
|