eslint-plugin-cvsdk-rules 0.23.0-test-publish-2
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/config/file.ts +1 -0
- package/config/tsconfig.json +11 -0
- package/index.js +5 -0
- package/jest.config.js +2 -0
- package/package.json +17 -0
- package/rules/prefer-self-over-window.js +41 -0
- package/rules/prefer-self-over-window.spec.js +40 -0
package/config/file.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
// See: https://typescript-eslint.io/packages/rule-tester/#type-aware-testing
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
// https://typescript-eslint.io/packages/rule-tester/#type-aware-testing
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
// Otherwise TS will complain it'll override the base project's output
|
|
5
|
+
"outDir": "./",
|
|
6
|
+
"strict": true
|
|
7
|
+
},
|
|
8
|
+
"exclude": [],
|
|
9
|
+
// See: https://typescript-eslint.io/packages/rule-tester/#type-aware-testing
|
|
10
|
+
"include": ["file.ts"]
|
|
11
|
+
}
|
package/index.js
ADDED
package/jest.config.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "eslint-plugin-cvsdk-rules",
|
|
3
|
+
"version": "0.23.0-test-publish-2",
|
|
4
|
+
"main": "index.js",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"test": "jest",
|
|
7
|
+
"test:nocoverage": "jest"
|
|
8
|
+
},
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"@typescript-eslint/utils": "6.21.0"
|
|
11
|
+
},
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"@typescript-eslint/rule-tester": "6.21.0",
|
|
14
|
+
"jest": "29.7.0"
|
|
15
|
+
},
|
|
16
|
+
"gitHead": "8d6ce5e078751d3c44add1c8fbaa893207adb1b7"
|
|
17
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
const { ESLintUtils } = require('@typescript-eslint/utils');
|
|
2
|
+
|
|
3
|
+
const meta = {
|
|
4
|
+
docs: {
|
|
5
|
+
description: 'Disallow window access.',
|
|
6
|
+
},
|
|
7
|
+
messages: {
|
|
8
|
+
forbiddenWindowAccess: '`window` cannot be accessed inside some containers, we prefer using `self` as it has more compatibility.',
|
|
9
|
+
},
|
|
10
|
+
schema: [], // no options
|
|
11
|
+
type: 'problem',
|
|
12
|
+
fixable: 'code',
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
function create(context) {
|
|
16
|
+
const parserServices = ESLintUtils.getParserServices(context);
|
|
17
|
+
|
|
18
|
+
function reportWindowError(node) {
|
|
19
|
+
if (node.parent.type !== 'VariableDeclarator') {
|
|
20
|
+
const nodeType = parserServices.getTypeAtLocation(node);
|
|
21
|
+
if (Object.prototype.hasOwnProperty.call(nodeType, 'aliasSymbol')) {
|
|
22
|
+
context.report({
|
|
23
|
+
messageId: 'forbiddenWindowAccess',
|
|
24
|
+
node,
|
|
25
|
+
fix: function (fixer) {
|
|
26
|
+
return fixer.replaceText(node, 'self');
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
"Identifier[name='window']": reportWindowError,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
module.exports = {
|
|
39
|
+
create,
|
|
40
|
+
meta,
|
|
41
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
const { RuleTester } = require('@typescript-eslint/rule-tester');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
|
|
4
|
+
const rule = require('./prefer-self-over-window');
|
|
5
|
+
|
|
6
|
+
const ruleName = 'prefer-self-over-window';
|
|
7
|
+
|
|
8
|
+
// https://typescript-eslint.io/packages/rule-tester/#type-aware-testing
|
|
9
|
+
const ruleTester = new RuleTester({
|
|
10
|
+
parser: require.resolve('@typescript-eslint/parser'),
|
|
11
|
+
parserOptions: {
|
|
12
|
+
project: true,
|
|
13
|
+
tsconfigRootDir: path.join(__dirname, '../config'),
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
describe('Disallow window access', () => {
|
|
18
|
+
ruleTester.run(ruleName, rule, {
|
|
19
|
+
invalid: [
|
|
20
|
+
{
|
|
21
|
+
code: 'window;',
|
|
22
|
+
output: 'self;',
|
|
23
|
+
errors: [{ messageId: 'forbiddenWindowAccess' }],
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
code: 'declare let window: any; window;',
|
|
27
|
+
output: 'declare let window: any; self;',
|
|
28
|
+
errors: [{ messageId: 'forbiddenWindowAccess' }],
|
|
29
|
+
},
|
|
30
|
+
],
|
|
31
|
+
valid: [
|
|
32
|
+
'const window = 1;',
|
|
33
|
+
'function someFunc() { const window = 1; window; }',
|
|
34
|
+
'function someFunc() { let window: number; window; };',
|
|
35
|
+
'function someFunc() { let window: unknown; window; };',
|
|
36
|
+
'function someFunc() { let window: any; window; };',
|
|
37
|
+
'function someFunc() { let window; window; };',
|
|
38
|
+
],
|
|
39
|
+
});
|
|
40
|
+
});
|