@angular-eslint/test-utils 17.5.3-alpha.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 James Henry
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # @angular-eslint/test-utils
2
+
3
+ Please see https://github.com/angular-eslint/angular-eslint for full usage instructions and guidance.
4
+
5
+ The `@angular-eslint/test-utils` package is a set of testing utility functions and types which are used across the `@angular-eslint` packages and are helpful when testing your own custom ESLint rules for Angular workspaces.
@@ -0,0 +1,62 @@
1
+ import type { TSESLint } from '@typescript-eslint/utils';
2
+ /**
3
+ * When leveraging the convertAnnotatedSourceToFailureCase() utility, the
4
+ * following characters are eligible to be used in the source code of expected
5
+ * failure cases within ESLint unit tests in order to provide an easy way to
6
+ * annotate where one or more ESLint errors are expected to occur within that
7
+ * source.
8
+ *
9
+ * See the convertAnnotatedSourceToFailureCase() utility itself for more details.
10
+ */
11
+ export declare const SPECIAL_UNDERLINE_CHARS: readonly ["~", "^", "#", "%", "¶", "*", "¨", "@"];
12
+ type MultipleErrorOptions<TMessageIds extends string> = BaseErrorOptions & {
13
+ readonly messages: readonly (Message<TMessageIds> & {
14
+ readonly char: (typeof SPECIAL_UNDERLINE_CHARS)[number];
15
+ })[];
16
+ };
17
+ type BaseErrorOptions = {
18
+ readonly description: string;
19
+ readonly annotatedSource: string;
20
+ readonly options?: readonly unknown[];
21
+ readonly annotatedOutput?: string;
22
+ readonly annotatedOutputs?: readonly string[];
23
+ readonly filename?: string;
24
+ readonly only?: boolean;
25
+ };
26
+ type Message<TMessageIds extends string> = {
27
+ readonly messageId: TMessageIds;
28
+ readonly data?: Record<string, unknown>;
29
+ readonly suggestions?: TSESLint.SuggestionOutput<TMessageIds>[];
30
+ };
31
+ type SingleErrorOptions<TMessageIds extends string> = BaseErrorOptions & Message<TMessageIds>;
32
+ /**
33
+ * convertAnnotatedSourceToFailureCase() provides an ergonomic way to easily write
34
+ * expected failure cases for ESLint rules by allowing you to directly annotate the
35
+ * source code for the case with one or more of the values in `SPECIAL_UNDERLINE_CHARS`.
36
+ *
37
+ * This not only makes the unit tests easier to write because of the time saved in figuring
38
+ * out location data in terms of lines and columns, but also far easier to read, which is
39
+ * arguably much more important.
40
+ *
41
+ * Here is a real-world example of using the utility:
42
+ *
43
+ * ```ts
44
+ * convertAnnotatedSourceToFailureCase({
45
+ * description: 'should fail when Pipe has no prefix ng',
46
+ * annotatedSource: `
47
+ * @Pipe({
48
+ * name: 'foo-bar'
49
+ * ~~~~~~~~~
50
+ * })
51
+ * class Test {}
52
+ * `,
53
+ * messageId: 'pipePrefix,
54
+ * options: [{ prefixes: ['ng'] }],
55
+ * data: { prefixes: '"ng"' },
56
+ * }),
57
+ * ```
58
+ */
59
+ export declare function convertAnnotatedSourceToFailureCase<TMessageIds extends string>(errorOptions: SingleErrorOptions<TMessageIds>): TSESLint.InvalidTestCase<TMessageIds, readonly unknown[]>;
60
+ export declare function convertAnnotatedSourceToFailureCase<TMessageIds extends string>(errorOptions: MultipleErrorOptions<TMessageIds>): TSESLint.InvalidTestCase<TMessageIds, readonly unknown[]>;
61
+ export {};
62
+ //# sourceMappingURL=convert-annotated-source-to-failure-case.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"convert-annotated-source-to-failure-case.d.ts","sourceRoot":"","sources":["../src/convert-annotated-source-to-failure-case.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AAEzD;;;;;;;;GAQG;AACH,eAAO,MAAM,uBAAuB,mDAS1B,CAAC;AAEX,KAAK,oBAAoB,CAAC,WAAW,SAAS,MAAM,IAAI,gBAAgB,GAAG;IACzE,QAAQ,CAAC,QAAQ,EAAE,SAAS,CAAC,OAAO,CAAC,WAAW,CAAC,GAAG;QAClD,QAAQ,CAAC,IAAI,EAAE,CAAC,OAAO,uBAAuB,CAAC,CAAC,MAAM,CAAC,CAAC;KACzD,CAAC,EAAE,CAAC;CACN,CAAC;AAEF,KAAK,gBAAgB,GAAG;IACtB,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;IACjC,QAAQ,CAAC,OAAO,CAAC,EAAE,SAAS,OAAO,EAAE,CAAC;IACtC,QAAQ,CAAC,eAAe,CAAC,EAAE,MAAM,CAAC;IAClC,QAAQ,CAAC,gBAAgB,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAC9C,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC;CACzB,CAAC;AAEF,KAAK,OAAO,CAAC,WAAW,SAAS,MAAM,IAAI;IACzC,QAAQ,CAAC,SAAS,EAAE,WAAW,CAAC;IAChC,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACxC,QAAQ,CAAC,WAAW,CAAC,EAAE,QAAQ,CAAC,gBAAgB,CAAC,WAAW,CAAC,EAAE,CAAC;CACjE,CAAC;AAEF,KAAK,kBAAkB,CAAC,WAAW,SAAS,MAAM,IAAI,gBAAgB,GACpE,OAAO,CAAC,WAAW,CAAC,CAAC;AAEvB;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,mCAAmC,CAAC,WAAW,SAAS,MAAM,EAC5E,YAAY,EAAE,kBAAkB,CAAC,WAAW,CAAC,GAC5C,QAAQ,CAAC,eAAe,CAAC,WAAW,EAAE,SAAS,OAAO,EAAE,CAAC,CAAC;AAC7D,wBAAgB,mCAAmC,CAAC,WAAW,SAAS,MAAM,EAC5E,YAAY,EAAE,oBAAoB,CAAC,WAAW,CAAC,GAC9C,QAAQ,CAAC,eAAe,CAAC,WAAW,EAAE,SAAS,OAAO,EAAE,CAAC,CAAC"}
@@ -0,0 +1,143 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.convertAnnotatedSourceToFailureCase = exports.SPECIAL_UNDERLINE_CHARS = void 0;
4
+ /**
5
+ * When leveraging the convertAnnotatedSourceToFailureCase() utility, the
6
+ * following characters are eligible to be used in the source code of expected
7
+ * failure cases within ESLint unit tests in order to provide an easy way to
8
+ * annotate where one or more ESLint errors are expected to occur within that
9
+ * source.
10
+ *
11
+ * See the convertAnnotatedSourceToFailureCase() utility itself for more details.
12
+ */
13
+ exports.SPECIAL_UNDERLINE_CHARS = [
14
+ '~',
15
+ '^',
16
+ '#',
17
+ '%',
18
+ '¶',
19
+ '*',
20
+ '¨',
21
+ '@',
22
+ ];
23
+ function convertAnnotatedSourceToFailureCase(errorOptions) {
24
+ if (errorOptions.annotatedOutput && errorOptions.annotatedOutputs) {
25
+ throw new Error('Only one of `annotatedOutput` and `annotatedOutputs` should be provided');
26
+ }
27
+ const messages = 'messageId' in errorOptions
28
+ ? [{ ...errorOptions, char: '~' }]
29
+ : errorOptions.messages;
30
+ let parsedSource = '';
31
+ const errors = messages.map(({ char: currentValueChar, data, messageId, suggestions }) => {
32
+ const otherChars = messages
33
+ .map(({ char }) => char)
34
+ .filter((char) => char !== currentValueChar);
35
+ const parsedForChar = parseInvalidSource(errorOptions.annotatedSource, currentValueChar, otherChars);
36
+ const { failure: { endPosition, startPosition }, } = parsedForChar;
37
+ parsedSource = parsedForChar.source;
38
+ if (!endPosition || !startPosition) {
39
+ throw Error(`Char '${currentValueChar}' has been specified in \`messages\`, however it is not present in the source of the failure case`);
40
+ }
41
+ return {
42
+ data,
43
+ messageId,
44
+ line: startPosition.line + 1,
45
+ column: startPosition.character + 1,
46
+ endLine: endPosition.line + 1,
47
+ endColumn: endPosition.character + 1,
48
+ suggestions,
49
+ };
50
+ });
51
+ return {
52
+ name: errorOptions.description,
53
+ code: parsedSource,
54
+ filename: errorOptions.filename,
55
+ options: errorOptions.options ?? [],
56
+ errors,
57
+ only: errorOptions.only ?? false,
58
+ output: errorOptions.annotatedOutputs
59
+ ? errorOptions.annotatedOutputs.map((s) => parseInvalidSource(s).source)
60
+ : errorOptions.annotatedOutput
61
+ ? parseInvalidSource(errorOptions.annotatedOutput).source
62
+ : null,
63
+ };
64
+ }
65
+ exports.convertAnnotatedSourceToFailureCase = convertAnnotatedSourceToFailureCase;
66
+ function escapeRegexp(value) {
67
+ return value.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
68
+ }
69
+ /**
70
+ * When testing a failure, we also test to see if the linter will report the correct place where
71
+ * the source code doesn't match the rule.
72
+ *
73
+ * For example, if you use a private property in your template, the linter should report _where_
74
+ * did it happen. Because it's tedious to supply actual line/column number in the spec, we use
75
+ * some custom syntax with "underlining" the problematic part with tildes:
76
+ *
77
+ * ```
78
+ * template: '{{ foo }}'
79
+ * ~~~
80
+ * ```
81
+ *
82
+ * When giving a spec which we expect to fail, we give it "source code" such as above, with tildes.
83
+ * We call this kind of source code "annotated". This source code cannot be compiled (and thus
84
+ * cannot be linted/tested), so we use this function to get rid of tildes, but maintain the
85
+ * information about where the linter is supposed to catch error.
86
+ *
87
+ * The result of the function contains "cleaned" source (`.source`) and a `.failure` object which
88
+ * contains the `.startPosition` and `.endPosition` of the tildes.
89
+ *
90
+ * @param source The annotated source code with tildes.
91
+ * @param specialChar The character to look for; in the above example that's ~.
92
+ * @param otherChars All other characters which should be ignored. Used when asserting multiple
93
+ * failures where there are multiple invalid characters.
94
+ * @returns {{source: string, failure: {message: string, startPosition: null, endPosition: any}}}
95
+ */
96
+ function parseInvalidSource(source, specialChar = '~', otherChars = []) {
97
+ let replacedSource;
98
+ if (otherChars.length === 0) {
99
+ replacedSource = source;
100
+ }
101
+ else {
102
+ const patternAsStr = `[${otherChars.map(escapeRegexp).join('')}]`;
103
+ const pattern = RegExp(patternAsStr, 'g');
104
+ replacedSource = source.replace(pattern, ' ');
105
+ }
106
+ let col = 0;
107
+ let line = 0;
108
+ let lastCol = 0;
109
+ let lastLine = 0;
110
+ let startPosition;
111
+ for (const currentChar of replacedSource) {
112
+ if (currentChar === '\n') {
113
+ col = 0;
114
+ line++;
115
+ continue;
116
+ }
117
+ col++;
118
+ if (currentChar !== specialChar)
119
+ continue;
120
+ if (!startPosition) {
121
+ startPosition = {
122
+ character: col - 1,
123
+ line: line - 1,
124
+ };
125
+ }
126
+ lastCol = col;
127
+ lastLine = line - 1;
128
+ }
129
+ const endPosition = {
130
+ character: lastCol,
131
+ line: lastLine,
132
+ };
133
+ const newSource = replacedSource.replace(RegExp(escapeRegexp(specialChar), 'g'), '');
134
+ return {
135
+ failure: {
136
+ endPosition,
137
+ message: '',
138
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
139
+ startPosition: startPosition,
140
+ },
141
+ source: newSource,
142
+ };
143
+ }
@@ -0,0 +1,3 @@
1
+ export * from './convert-annotated-source-to-failure-case';
2
+ export * from './rule-tester';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,4CAA4C,CAAC;AAC3D,cAAc,eAAe,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./convert-annotated-source-to-failure-case"), exports);
18
+ __exportStar(require("./rule-tester"), exports);
@@ -0,0 +1,13 @@
1
+ import { RuleTester as TSESLintRuleTester } from '@typescript-eslint/rule-tester';
2
+ import type { TSESLint } from '@typescript-eslint/utils';
3
+ declare const VALID_PARSERS: readonly ["@angular-eslint/template-parser", "@typescript-eslint/parser"];
4
+ type RuleTesterConfig = Omit<TSESLint.RuleTesterConfig, 'parser'> & {
5
+ parser: (typeof VALID_PARSERS)[number];
6
+ };
7
+ export declare class RuleTester extends TSESLintRuleTester {
8
+ private filename?;
9
+ constructor(options: RuleTesterConfig);
10
+ run<TMessageIds extends string, TOptions extends readonly unknown[]>(name: string, rule: TSESLint.RuleModule<TMessageIds, TOptions>, { valid, invalid }: TSESLint.RunTests<TMessageIds, TOptions>): void;
11
+ }
12
+ export {};
13
+ //# sourceMappingURL=rule-tester.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rule-tester.d.ts","sourceRoot":"","sources":["../src/rule-tester.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,IAAI,kBAAkB,EAAE,MAAM,gCAAgC,CAAC;AAClF,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AAGzD,QAAA,MAAM,aAAa,2EAGT,CAAC;AAEX,KAAK,gBAAgB,GAAG,IAAI,CAAC,QAAQ,CAAC,gBAAgB,EAAE,QAAQ,CAAC,GAAG;IAClE,MAAM,EAAE,CAAC,OAAO,aAAa,CAAC,CAAC,MAAM,CAAC,CAAC;CACxC,CAAC;AAYF,qBAAa,UAAW,SAAQ,kBAAkB;IAChD,OAAO,CAAC,QAAQ,CAAC,CAAc;gBAKnB,OAAO,EAAE,gBAAgB;IA8B5B,GAAG,CAAC,WAAW,SAAS,MAAM,EAAE,QAAQ,SAAS,SAAS,OAAO,EAAE,EAC1E,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,QAAQ,CAAC,UAAU,CAAC,WAAW,EAAE,QAAQ,CAAC,EAChD,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE,QAAQ,CAAC,QAAQ,CAAC,WAAW,EAAE,QAAQ,CAAC,GAC3D,IAAI;CA0BR"}
@@ -0,0 +1,93 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.RuleTester = void 0;
27
+ const rule_tester_1 = require("@typescript-eslint/rule-tester");
28
+ const path = __importStar(require("path"));
29
+ const VALID_PARSERS = [
30
+ '@angular-eslint/template-parser',
31
+ '@typescript-eslint/parser',
32
+ ];
33
+ function getFixturesRootDir() {
34
+ return path.join(process.cwd(), 'tests/fixtures/');
35
+ }
36
+ function isValidParser(parser) {
37
+ return VALID_PARSERS.includes(parser);
38
+ }
39
+ class RuleTester extends rule_tester_1.RuleTester {
40
+ filename = '';
41
+ // as of eslint 6 you have to provide an absolute path to the parser
42
+ // but that's not as clean to type, this saves us trying to manually enforce
43
+ // that contributors require.resolve everything
44
+ constructor(options) {
45
+ super({
46
+ ...options,
47
+ parser: require.resolve(options.parser),
48
+ });
49
+ if (options.parserOptions?.project) {
50
+ this.filename = path.join(options.parserOptions?.tsconfigRootDir ?? getFixturesRootDir(), 'file.ts');
51
+ }
52
+ // make sure that the parser doesn't hold onto file handles between tests
53
+ // on linux (i.e. our CI env), there can be very a limited number of watch handles available
54
+ afterAll(() => {
55
+ try {
56
+ // instead of creating a hard dependency, just use a soft require
57
+ // a bit weird, but if they're using this tooling, it'll be installed
58
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
59
+ require(options.parser).clearCaches();
60
+ }
61
+ catch {
62
+ // ignored
63
+ }
64
+ });
65
+ }
66
+ // as of eslint 6 you have to provide an absolute path to the parser
67
+ // If you don't do that at the test level, the test will fail somewhat cryptically...
68
+ // This is a lot more explicit
69
+ run(name, rule, { valid, invalid }) {
70
+ const errorMessage = `Do not set the parser at the test level unless you want to use a parser other than ${VALID_PARSERS.join(', ')}`;
71
+ const parsedTests = {
72
+ valid: valid.map((test) => {
73
+ if (typeof test !== 'string' && isValidParser(test.parser)) {
74
+ throw Error(errorMessage);
75
+ }
76
+ return typeof test === 'string'
77
+ ? { code: test, filename: this.filename }
78
+ : { ...test, filename: test.filename ?? this.filename };
79
+ }),
80
+ invalid: invalid.map((test) => {
81
+ if (isValidParser(test.parser)) {
82
+ throw Error(errorMessage);
83
+ }
84
+ return {
85
+ ...test,
86
+ filename: test.filename ?? this.filename,
87
+ };
88
+ }),
89
+ };
90
+ super.run(name, rule, parsedTests);
91
+ }
92
+ }
93
+ exports.RuleTester = RuleTester;
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@angular-eslint/test-utils",
3
+ "version": "17.5.3-alpha.1",
4
+ "license": "MIT",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/angular-eslint/angular-eslint.git",
10
+ "directory": "packages/test-utils"
11
+ },
12
+ "files": [
13
+ "dist",
14
+ "!**/*.tsbuildinfo",
15
+ "package.json",
16
+ "README.md",
17
+ "LICENSE"
18
+ ],
19
+ "peerDependencies": {
20
+ "eslint": "^8.57.0 || ^9.0.0",
21
+ "typescript": "*"
22
+ },
23
+ "dependencies": {
24
+ "@typescript-eslint/rule-tester": "8.0.0-alpha.20",
25
+ "@typescript-eslint/utils": "8.0.0-alpha.20"
26
+ },
27
+ "publishConfig": {
28
+ "access": "public"
29
+ },
30
+ "gitHead": "e2006e5e9c99e5a943d1a999e0efa5247d29ec24"
31
+ }