@aquera/nile-lint 0.0.0-stage → 0.1.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/README.md +227 -2
- package/SETUP.md +71 -0
- package/bin/nile-lint.js +169 -0
- package/eslint/index.js +137 -0
- package/index.d.ts +120 -0
- package/index.js +159 -0
- package/lib/angular.js +138 -0
- package/lib/baseline.js +69 -0
- package/lib/color.js +232 -0
- package/lib/config.js +85 -0
- package/lib/css-rules.js +466 -0
- package/lib/engine.js +453 -0
- package/lib/format.js +132 -0
- package/lib/manifest.js +76 -0
- package/lib/markup-rules.js +475 -0
- package/lib/markup.js +141 -0
- package/lib/rules.js +159 -0
- package/lib/suggest.js +153 -0
- package/lib/version.js +84 -0
- package/manifest.json +32238 -0
- package/package.json +70 -4
- package/stylelint/cascade.js +4 -0
- package/stylelint/index.js +129 -0
- package/stylelint/recommended.js +4 -0
- package/stylelint/strict.js +4 -0
package/index.d.ts
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
export type Severity = 'error' | 'warn' | 'off';
|
|
2
|
+
export type Profile = 'recommended' | 'cascade' | 'strict';
|
|
3
|
+
export type RuleEntry = Severity | 0 | 1 | 2 | [Severity | 0 | 1 | 2, Record<string, unknown>?];
|
|
4
|
+
|
|
5
|
+
export interface Fix {
|
|
6
|
+
start: number;
|
|
7
|
+
end: number;
|
|
8
|
+
text: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface Diagnostic {
|
|
12
|
+
ruleId: string;
|
|
13
|
+
severity: 'error' | 'warn';
|
|
14
|
+
message: string;
|
|
15
|
+
/** One line on how to fix this rule's violations. */
|
|
16
|
+
help?: string;
|
|
17
|
+
line: number;
|
|
18
|
+
column: number;
|
|
19
|
+
endLine: number;
|
|
20
|
+
endColumn: number;
|
|
21
|
+
/** [start, end) offsets into the linted text. */
|
|
22
|
+
range: [number, number];
|
|
23
|
+
/** Present only for exact-match fixes. */
|
|
24
|
+
fix?: Fix;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface FileResult {
|
|
28
|
+
filePath: string;
|
|
29
|
+
messages: Diagnostic[];
|
|
30
|
+
fixed?: number;
|
|
31
|
+
/** Problems hidden because the baseline already records them. */
|
|
32
|
+
baselined?: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface Config {
|
|
36
|
+
profile?: Profile;
|
|
37
|
+
rules?: Record<string, RuleEntry>;
|
|
38
|
+
ignore?: string[];
|
|
39
|
+
manifest?: string;
|
|
40
|
+
overrides?: { files: string | string[]; profile?: Profile; rules?: Record<string, RuleEntry> }[];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface LintFilesOptions {
|
|
44
|
+
cwd?: string;
|
|
45
|
+
profile?: Profile;
|
|
46
|
+
rules?: Record<string, RuleEntry>;
|
|
47
|
+
fix?: boolean;
|
|
48
|
+
/** Explicit config file, or false to ignore config files. */
|
|
49
|
+
config?: string | false;
|
|
50
|
+
manifest?: string;
|
|
51
|
+
ignore?: string[];
|
|
52
|
+
/** Baseline to subtract (default: .nile-lint-baseline.json in cwd when present; false = none). */
|
|
53
|
+
baseline?: string | false;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface RuleMeta {
|
|
57
|
+
kind: 'css' | 'markup' | 'both';
|
|
58
|
+
description: string;
|
|
59
|
+
help: string;
|
|
60
|
+
fixable?: boolean;
|
|
61
|
+
profiles: { recommended?: Severity; cascade?: Severity };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function lintText(
|
|
65
|
+
text: string,
|
|
66
|
+
options?: {
|
|
67
|
+
filename?: string;
|
|
68
|
+
profile?: Profile;
|
|
69
|
+
rules?: Record<string, RuleEntry>;
|
|
70
|
+
manifest?: string;
|
|
71
|
+
}
|
|
72
|
+
): Diagnostic[];
|
|
73
|
+
|
|
74
|
+
export function lintFiles(patterns: string[], options?: LintFilesOptions): Promise<FileResult[]>;
|
|
75
|
+
|
|
76
|
+
export function applyFixes(
|
|
77
|
+
text: string,
|
|
78
|
+
diagnostics: Diagnostic[]
|
|
79
|
+
): { text: string; applied: number };
|
|
80
|
+
|
|
81
|
+
export const RULES: Record<string, RuleMeta>;
|
|
82
|
+
export const PROFILES: Profile[];
|
|
83
|
+
|
|
84
|
+
/** nile-lint's version and the Nile versions the bundled manifest was built from. */
|
|
85
|
+
export interface VersionInfo {
|
|
86
|
+
version: string;
|
|
87
|
+
nile?: string;
|
|
88
|
+
nileElements?: string;
|
|
89
|
+
nileGlyph?: string;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** When given, pretty prints it as a header and agent puts it in the summary line. */
|
|
93
|
+
export type FormatMeta = VersionInfo & { profile?: string };
|
|
94
|
+
|
|
95
|
+
export const version: string;
|
|
96
|
+
|
|
97
|
+
export interface Baseline {
|
|
98
|
+
note: string;
|
|
99
|
+
/** file (relative to cwd) → "ruleId | first sentence of the message" → count */
|
|
100
|
+
files: Record<string, Record<string, number>>;
|
|
101
|
+
}
|
|
102
|
+
export function createBaseline(results: FileResult[], cwd?: string): Baseline;
|
|
103
|
+
/** Removes recorded problems from `results` in place; returns how many were hidden. */
|
|
104
|
+
export function applyBaseline(results: FileResult[], baseline: Baseline, cwd?: string): number;
|
|
105
|
+
export function versionInfo(): VersionInfo;
|
|
106
|
+
|
|
107
|
+
export const format: {
|
|
108
|
+
pretty(results: FileResult[], options?: { cwd?: string; meta?: FormatMeta }): string;
|
|
109
|
+
agent(
|
|
110
|
+
results: FileResult[],
|
|
111
|
+
options?: { cwd?: string; command?: string; profile?: string; meta?: FormatMeta }
|
|
112
|
+
): string;
|
|
113
|
+
json(results: FileResult[]): string;
|
|
114
|
+
counts(results: FileResult[]): {
|
|
115
|
+
errors: number;
|
|
116
|
+
warnings: number;
|
|
117
|
+
fixable: number;
|
|
118
|
+
files: number;
|
|
119
|
+
};
|
|
120
|
+
};
|
package/index.js
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @aquera/nile-lint — agent-first linter for the Nile design system.
|
|
5
|
+
*
|
|
6
|
+
* const { lintFiles, lintText } = require('@aquera/nile-lint');
|
|
7
|
+
* const results = await lintFiles(['src'], { profile: 'recommended', fix: false });
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
const fs = require('fs');
|
|
11
|
+
const path = require('path');
|
|
12
|
+
const fg = require('fast-glob');
|
|
13
|
+
const picomatch = require('picomatch');
|
|
14
|
+
const engine = require('./lib/engine');
|
|
15
|
+
const { findConfig, resolveRules, DEFAULT_IGNORE } = require('./lib/config');
|
|
16
|
+
const { RULES, PROFILES } = require('./lib/rules');
|
|
17
|
+
const { loadManifest } = require('./lib/manifest');
|
|
18
|
+
const format = require('./lib/format');
|
|
19
|
+
const { versionInfo } = require('./lib/version');
|
|
20
|
+
const { collectDirectiveInputs, collectLibraryDirectiveInputs } = require('./lib/angular');
|
|
21
|
+
const baseline = require('./lib/baseline');
|
|
22
|
+
const { version } = require('./package.json');
|
|
23
|
+
|
|
24
|
+
async function expand(patterns, cwd, ignore) {
|
|
25
|
+
const globs = [];
|
|
26
|
+
for (const p of patterns.length ? patterns : ['.']) {
|
|
27
|
+
const abs = path.resolve(cwd, p);
|
|
28
|
+
if (fs.existsSync(abs) && fs.statSync(abs).isDirectory()) {
|
|
29
|
+
globs.push(
|
|
30
|
+
path.posix.join(
|
|
31
|
+
fg.escapePath(path.relative(cwd, abs).split(path.sep).join('/') || '.'),
|
|
32
|
+
'**/*'
|
|
33
|
+
)
|
|
34
|
+
);
|
|
35
|
+
} else {
|
|
36
|
+
globs.push(p);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const files = await fg(globs, {
|
|
40
|
+
cwd,
|
|
41
|
+
ignore,
|
|
42
|
+
absolute: true,
|
|
43
|
+
dot: false,
|
|
44
|
+
onlyFiles: true,
|
|
45
|
+
followSymbolicLinks: false,
|
|
46
|
+
});
|
|
47
|
+
return [...new Set(files)].filter((f) => engine.LINTABLE.test(f)).sort();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Resolve profile + rules for one file, honouring config `overrides`. */
|
|
51
|
+
function configFor(file, base, cwd) {
|
|
52
|
+
let profile = base.profile || 'recommended';
|
|
53
|
+
let overrides = { ...(base.rules || {}) };
|
|
54
|
+
const rel = path.relative(cwd, file).split(path.sep).join('/');
|
|
55
|
+
for (const o of base.overrides || []) {
|
|
56
|
+
if ([].concat(o.files || []).some((g) => picomatch.isMatch(rel, g, { dot: true }))) {
|
|
57
|
+
if (o.profile) profile = o.profile;
|
|
58
|
+
overrides = { ...overrides, ...(o.rules || {}) };
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return { profile, rules: resolveRules(profile, overrides) };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* @param {string[]} patterns files, directories or globs
|
|
66
|
+
* @param {{ cwd?: string, profile?: string, rules?: object, fix?: boolean, config?: string|false, manifest?: string, ignore?: string[], baseline?: string|false }} opts
|
|
67
|
+
* baseline: a baseline file to subtract (default: .nile-lint-baseline.json in cwd when present; false = none)
|
|
68
|
+
* @returns {Promise<{ filePath: string, messages: object[], output?: string, fixed?: number, baselined?: number }[]>}
|
|
69
|
+
*/
|
|
70
|
+
async function lintFiles(patterns, opts = {}) {
|
|
71
|
+
const cwd = path.resolve(opts.cwd || process.cwd());
|
|
72
|
+
const found =
|
|
73
|
+
opts.config === false
|
|
74
|
+
? { file: null, config: {} }
|
|
75
|
+
: opts.config
|
|
76
|
+
? { file: path.resolve(opts.config), config: readExplicit(opts.config) }
|
|
77
|
+
: findConfig(cwd);
|
|
78
|
+
const cfg = { ...found.config };
|
|
79
|
+
if (opts.profile) cfg.profile = opts.profile;
|
|
80
|
+
cfg.rules = { ...(cfg.rules || {}), ...(opts.rules || {}) };
|
|
81
|
+
const manifestPath =
|
|
82
|
+
opts.manifest ||
|
|
83
|
+
(cfg.manifest && found.file ? path.resolve(path.dirname(found.file), cfg.manifest) : undefined);
|
|
84
|
+
const manifest = loadManifest(manifestPath);
|
|
85
|
+
const ignore = [...DEFAULT_IGNORE, ...(cfg.ignore || []), ...(opts.ignore || [])];
|
|
86
|
+
const files = await expand(patterns, cwd, ignore);
|
|
87
|
+
// The app's own Angular directives bind on nile-* hosts too; learn their inputs first.
|
|
88
|
+
const directiveFiles = await fg('**/*.directive.ts', { cwd, ignore, absolute: true });
|
|
89
|
+
const directiveInputs = collectDirectiveInputs([
|
|
90
|
+
...new Set([...files.filter((f) => /\.[cm]?ts$/.test(f)), ...directiveFiles]),
|
|
91
|
+
]);
|
|
92
|
+
for (const n of collectLibraryDirectiveInputs(cwd)) directiveInputs.add(n);
|
|
93
|
+
const lint = (text, file, profile, rules) =>
|
|
94
|
+
engine.lintText(text, { filename: file, rules, profile, manifest, directiveInputs });
|
|
95
|
+
|
|
96
|
+
const results = [];
|
|
97
|
+
for (const file of files) {
|
|
98
|
+
const { profile, rules } = configFor(file, cfg, cwd);
|
|
99
|
+
let text = fs.readFileSync(file, 'utf8');
|
|
100
|
+
let messages = lint(text, file, profile, rules);
|
|
101
|
+
let fixed = 0;
|
|
102
|
+
if (opts.fix) {
|
|
103
|
+
// Fixes can unlock or overlap each other; a few passes settle it.
|
|
104
|
+
for (let pass = 0; pass < 5 && messages.some((d) => d.fix); pass++) {
|
|
105
|
+
const res = engine.applyFixes(text, messages);
|
|
106
|
+
if (!res.applied) break;
|
|
107
|
+
text = res.text;
|
|
108
|
+
fixed += res.applied;
|
|
109
|
+
messages = lint(text, file, profile, rules);
|
|
110
|
+
}
|
|
111
|
+
if (fixed) fs.writeFileSync(file, text);
|
|
112
|
+
}
|
|
113
|
+
results.push({ filePath: file, messages, fixed });
|
|
114
|
+
}
|
|
115
|
+
const baselineFile =
|
|
116
|
+
opts.baseline === false
|
|
117
|
+
? null
|
|
118
|
+
: opts.baseline
|
|
119
|
+
? path.resolve(cwd, opts.baseline)
|
|
120
|
+
: path.join(cwd, baseline.DEFAULT_BASELINE);
|
|
121
|
+
if (baselineFile && (opts.baseline || fs.existsSync(baselineFile)))
|
|
122
|
+
baseline.applyBaseline(results, baseline.readBaseline(baselineFile), cwd);
|
|
123
|
+
return results;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function readExplicit(file) {
|
|
127
|
+
const abs = path.resolve(file);
|
|
128
|
+
if (/package\.json$/.test(abs)) return JSON.parse(fs.readFileSync(abs, 'utf8')).nileLint || {};
|
|
129
|
+
return /\.c?js$/.test(abs) ? require(abs) : JSON.parse(fs.readFileSync(abs, 'utf8'));
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Lint a string. `filename` picks the parser (.css/.scss/.html/.component.html/.ts/.js/.vue). */
|
|
133
|
+
function lintText(
|
|
134
|
+
text,
|
|
135
|
+
{ filename = 'input.html', profile = 'recommended', rules, manifest } = {}
|
|
136
|
+
) {
|
|
137
|
+
return engine.lintText(text, {
|
|
138
|
+
filename,
|
|
139
|
+
profile,
|
|
140
|
+
rules: resolveRules(profile, rules),
|
|
141
|
+
manifest: loadManifest(manifest),
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
module.exports = {
|
|
146
|
+
lintFiles,
|
|
147
|
+
lintText,
|
|
148
|
+
applyFixes: engine.applyFixes,
|
|
149
|
+
RULES,
|
|
150
|
+
PROFILES,
|
|
151
|
+
format,
|
|
152
|
+
loadManifest,
|
|
153
|
+
resolveRules,
|
|
154
|
+
version,
|
|
155
|
+
createBaseline: baseline.createBaseline,
|
|
156
|
+
applyBaseline: baseline.applyBaseline,
|
|
157
|
+
/** nile-lint's version plus the Nile versions the bundled manifest was built from. */
|
|
158
|
+
versionInfo: () => versionInfo(loadManifest().data),
|
|
159
|
+
};
|
package/lib/angular.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const { nodeModulesDirs } = require('./version');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The binding names a project's own Angular directives accept — attribute selectors
|
|
9
|
+
* (`[appTooltip]`) and inputs (`@Input() x`, `x = input()`, `inputs: ['x']`, aliases). On a
|
|
10
|
+
* nile-* host these are the directive's, not the component's, so no-unknown-attributes must
|
|
11
|
+
* not report them.
|
|
12
|
+
*
|
|
13
|
+
* @param {string[]} files .ts files to read (only those declaring a directive are parsed)
|
|
14
|
+
* @returns {Set<string>}
|
|
15
|
+
*/
|
|
16
|
+
function collectDirectiveInputs(files) {
|
|
17
|
+
const names = new Set();
|
|
18
|
+
for (const file of files) {
|
|
19
|
+
let src;
|
|
20
|
+
try {
|
|
21
|
+
src = fs.readFileSync(file, 'utf8');
|
|
22
|
+
} catch {
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
const isDirective = src.includes('@Directive(');
|
|
26
|
+
// Components with an attribute selector (selector: '[appFoo]') also attach to host elements.
|
|
27
|
+
const attrComponent = /@Component\(\s*\{[^}]*selector\s*:\s*['"`][^'"`]*\[/.test(src);
|
|
28
|
+
if (!isDirective && !attrComponent) continue;
|
|
29
|
+
|
|
30
|
+
for (const m of src.matchAll(/selector\s*:\s*['"`]([^'"`]+)['"`]/g))
|
|
31
|
+
for (const a of m[1].matchAll(/\[([\w-]+)(?:[~|^$*]?=[^\]]*)?\]/g)) names.add(a[1]);
|
|
32
|
+
if (!isDirective) continue;
|
|
33
|
+
|
|
34
|
+
// @Input() x / @Input('alias') x / @Input({ alias: 'a' }) set x(…)
|
|
35
|
+
for (const m of src.matchAll(
|
|
36
|
+
/@Input\(\s*(?:['"]([^'"]+)['"]|\{[^)]*?alias\s*:\s*['"]([^'"]+)['"][^)]*\}|\{[^)]*\})?\s*\)\s*(?:(?:public|private|protected|readonly|override)\s+)*(?:set\s+)?(\w+)/g
|
|
37
|
+
))
|
|
38
|
+
names.add(m[1] || m[2] || m[3]);
|
|
39
|
+
// x = input(…) / input.required(…) / model(…), with optional { alias: 'a' }
|
|
40
|
+
for (const m of src.matchAll(
|
|
41
|
+
/(\w+)\s*=\s*(?:input|model)(?:\.required)?\s*(?:<[^>]*>)?\s*\(([^)]*)\)/g
|
|
42
|
+
)) {
|
|
43
|
+
const alias = /alias\s*:\s*['"]([^'"]+)['"]/.exec(m[2]);
|
|
44
|
+
names.add(alias ? alias[1] : m[1]);
|
|
45
|
+
}
|
|
46
|
+
// inputs: ['x', 'prop: alias']
|
|
47
|
+
for (const m of src.matchAll(/inputs\s*:\s*\[([^\]]*)\]/g))
|
|
48
|
+
for (const s of m[1].matchAll(/['"]([^'"]+)['"]/g)) {
|
|
49
|
+
const parts = s[1].split(':').map((p) => p.trim());
|
|
50
|
+
names.add(parts[1] || parts[0]);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return names;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const SKIP_SCOPES = /^@(angular|aquera\/nile|types|nx|babel|ngrx|ngx-translate\/core$)/;
|
|
57
|
+
|
|
58
|
+
function walkDts(dir, out, depth = 0) {
|
|
59
|
+
if (depth > 6) return out;
|
|
60
|
+
let ents;
|
|
61
|
+
try {
|
|
62
|
+
ents = fs.readdirSync(dir, { withFileTypes: true });
|
|
63
|
+
} catch {
|
|
64
|
+
return out;
|
|
65
|
+
}
|
|
66
|
+
for (const e of ents) {
|
|
67
|
+
if (e.name === 'node_modules' || e.name.startsWith('.')) continue;
|
|
68
|
+
const p = path.join(dir, e.name);
|
|
69
|
+
if (e.isDirectory()) walkDts(p, out, depth + 1);
|
|
70
|
+
else if (e.name.endsWith('.d.ts')) out.push(p);
|
|
71
|
+
}
|
|
72
|
+
return out;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Directive inputs declared by the project's Angular library dependencies. Compiled Angular
|
|
77
|
+
* libraries carry `ɵɵDirectiveDeclaration<Cls, "[selector]", exportAs, { inputs }, …>` in their
|
|
78
|
+
* typings; that's read for every dependency that peer-depends on @angular/core (Angular's own
|
|
79
|
+
* packages and Nile are skipped — their bindings are known already).
|
|
80
|
+
*
|
|
81
|
+
* @param {string} cwd project folder (its nearest package.json lists the dependencies)
|
|
82
|
+
* @returns {Set<string>}
|
|
83
|
+
*/
|
|
84
|
+
function collectLibraryDirectiveInputs(cwd) {
|
|
85
|
+
const names = new Set();
|
|
86
|
+
let pkgFile = null;
|
|
87
|
+
for (let d = path.resolve(cwd); ; d = path.dirname(d)) {
|
|
88
|
+
if (fs.existsSync(path.join(d, 'package.json'))) {
|
|
89
|
+
pkgFile = path.join(d, 'package.json');
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
if (path.dirname(d) === d) break;
|
|
93
|
+
}
|
|
94
|
+
if (!pkgFile) return names;
|
|
95
|
+
let deps;
|
|
96
|
+
try {
|
|
97
|
+
const pkg = JSON.parse(fs.readFileSync(pkgFile, 'utf8'));
|
|
98
|
+
deps = Object.keys({ ...pkg.dependencies, ...pkg.devDependencies });
|
|
99
|
+
} catch {
|
|
100
|
+
return names;
|
|
101
|
+
}
|
|
102
|
+
const bases = nodeModulesDirs(path.dirname(pkgFile));
|
|
103
|
+
for (const dep of deps) {
|
|
104
|
+
if (SKIP_SCOPES.test(dep)) continue;
|
|
105
|
+
const dir = bases
|
|
106
|
+
.map((b) => path.join(b, dep))
|
|
107
|
+
.find((d) => fs.existsSync(path.join(d, 'package.json')));
|
|
108
|
+
if (!dir) continue;
|
|
109
|
+
let meta;
|
|
110
|
+
try {
|
|
111
|
+
meta = JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf8'));
|
|
112
|
+
} catch {
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
const angular = { ...meta.peerDependencies, ...meta.dependencies }['@angular/core'];
|
|
116
|
+
if (!angular) continue;
|
|
117
|
+
for (const file of walkDts(dir, [])) {
|
|
118
|
+
const src = fs.readFileSync(file, 'utf8');
|
|
119
|
+
if (!src.includes('DirectiveDeclaration') && !src.includes('ComponentDeclaration')) continue;
|
|
120
|
+
for (const m of src.matchAll(
|
|
121
|
+
/ɵɵ(Directive|Component)Declaration<\s*[\w.]+\s*,\s*"([^"]*)"\s*,\s*[^,]+,\s*(\{[^>]*?\})\s*,/g
|
|
122
|
+
)) {
|
|
123
|
+
const attrSelector = /\[/.test(m[2]);
|
|
124
|
+
for (const a of m[2].matchAll(/\[([\w-]+)(?:[~|^$*]?=[^\]]*)?\]/g)) names.add(a[1]);
|
|
125
|
+
// Component inputs only matter when the component attaches by attribute.
|
|
126
|
+
if (m[1] === 'Component' && !attrSelector) continue;
|
|
127
|
+
// { "name": "alias"; } (Angular ≤15) or { "name": { "alias": "alias"; … }; } (16+)
|
|
128
|
+
for (const i of m[3].matchAll(
|
|
129
|
+
/"([\w$]+)"\s*:\s*(?:"([\w$]+)"|\{\s*"alias"\s*:\s*"([\w$]+)")/g
|
|
130
|
+
))
|
|
131
|
+
names.add(i[2] || i[3] || i[1]);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return names;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
module.exports = { collectDirectiveInputs, collectLibraryDirectiveInputs };
|
package/lib/baseline.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
|
|
6
|
+
const DEFAULT_BASELINE = '.nile-lint-baseline.json';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Problems are matched by file + rule + the message's first sentence ("<nile-button> has no
|
|
10
|
+
* attribute "size"."), never by line: edits above a known problem don't resurface it, and a
|
|
11
|
+
* manifest update that changes the suggestion text doesn't either.
|
|
12
|
+
*/
|
|
13
|
+
function keyOf(d) {
|
|
14
|
+
const first = d.message.split(/(?<=[.?!])\s/)[0];
|
|
15
|
+
return `${d.ruleId} | ${first}`;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const relPath = (file, cwd) => path.relative(cwd, file).split(path.sep).join('/');
|
|
19
|
+
|
|
20
|
+
/** A baseline object recording every current problem, counted per file + key. */
|
|
21
|
+
function createBaseline(results, cwd = process.cwd()) {
|
|
22
|
+
const files = {};
|
|
23
|
+
for (const r of results) {
|
|
24
|
+
if (!r.messages.length) continue;
|
|
25
|
+
const counts = (files[relPath(r.filePath, cwd)] = {});
|
|
26
|
+
for (const d of r.messages) counts[keyOf(d)] = (counts[keyOf(d)] || 0) + 1;
|
|
27
|
+
}
|
|
28
|
+
const sorted = {};
|
|
29
|
+
for (const f of Object.keys(files).sort()) {
|
|
30
|
+
sorted[f] = {};
|
|
31
|
+
for (const k of Object.keys(files[f]).sort()) sorted[f][k] = files[f][k];
|
|
32
|
+
}
|
|
33
|
+
return {
|
|
34
|
+
note: 'nile-lint baseline: existing problems that are not reported. Regenerate with `nile-lint --update-baseline`; shrink it, never grow it by hand.',
|
|
35
|
+
files: sorted,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function readBaseline(file) {
|
|
40
|
+
const data = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
41
|
+
if (!data || typeof data.files !== 'object')
|
|
42
|
+
throw new Error(`${file} is not a nile-lint baseline`);
|
|
43
|
+
return data;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Drop the problems the baseline already records (up to its count per file + key). Mutates
|
|
48
|
+
* `results`: each file gets `baselined` (how many were hidden). Returns the total hidden.
|
|
49
|
+
*/
|
|
50
|
+
function applyBaseline(results, baseline, cwd = process.cwd()) {
|
|
51
|
+
let hidden = 0;
|
|
52
|
+
for (const r of results) {
|
|
53
|
+
const known = { ...(baseline.files[relPath(r.filePath, cwd)] || {}) };
|
|
54
|
+
const before = r.messages.length;
|
|
55
|
+
r.messages = r.messages.filter((d) => {
|
|
56
|
+
const k = keyOf(d);
|
|
57
|
+
if (known[k] > 0) {
|
|
58
|
+
known[k]--;
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
return true;
|
|
62
|
+
});
|
|
63
|
+
r.baselined = before - r.messages.length;
|
|
64
|
+
hidden += r.baselined;
|
|
65
|
+
}
|
|
66
|
+
return hidden;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
module.exports = { DEFAULT_BASELINE, createBaseline, readBaseline, applyBaseline, keyOf };
|