Skip to content

Commit

Permalink
feat: add support for camelCase
Browse files Browse the repository at this point in the history
Signed-off-by: MBWhite <[email protected]>
  • Loading branch information
mbwhite committed Apr 15, 2024
1 parent 354b0db commit 919a3d2
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions .tektonlintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ rules: # error | warning | off
no-latest-image: warning
prefer-beta: warning
prefer-kebab-case: warning
prefer-camel-kebab-case: off
no-unused-param: warning
no-missing-resource: error
no-undefined-param: error
Expand Down
1 change: 1 addition & 0 deletions src/default-rule-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const defaultRules: RulesConfig = {
'no-latest-image': 'warning',
'prefer-beta': 'warning',
'prefer-kebab-case': 'warning',
'prefer-camel-kebab-case': 'off',
'no-unused-param': 'warning',
'no-missing-resource': 'error',
'no-undefined-param': 'error',
Expand Down
3 changes: 3 additions & 0 deletions src/rule-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ const defaultRules = {
// prefer-kebab-case
'prefer-kebab-case': (await import('./rules/prefer-kebab-case.js')).default,

// prefer-camel-kebab-case
'prefer-camel-kebab-case': (await import('./rules/prefer-camel-kebab-case.js')).default,

// prefer-when-expression
'prefer-when-expression': (await import('./rules/prefer-when-expression.js')).default,

Expand Down
65 changes: 65 additions & 0 deletions src/rules/prefer-camel-kebab-case.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { walk, pathToString } from '../walk.js';

const isValidKebabName = (name) => {
const valid = new RegExp('^[a-z0-9-()$.]*$');
return valid.test(name);
};

const isValidCamelName = (name) => {
const valid = new RegExp('^[a-z_][a-z0-9A-Z()$.]*$');
return valid.test(name);
};

const isValidName = (name) => {
return isValidKebabName(name) || isValidCamelName(name);
};

const naming = (resource, prefix, report) => (node, path, parent) => {
let name = node;
const isNameDefinition = /.name$/.test(path);

if (path.includes('env') && path.includes('name')) return;

if (isNameDefinition && !isValidName(name)) {
report(
`Invalid name for '${name}' at ${pathToString(
path,
)} in '${resource}'. Names should be in lowercase, alphanumeric, kebab-case or camelCase format.`,
parent,
'name',
);
return;
}

const parameterPlacementRx = new RegExp(`\\$\\(${prefix}.(.*?)\\)`);
const m = node && node.toString().match(parameterPlacementRx);

if (m) {
name = m[1];
if (!isValidName(name)) {
report(
`Invalid name for '${name}' at ${pathToString(
path,
)} in '${resource}'. Names should be in lowercase, alphanumeric, kebab-case or camelCase format.`,
parent,
path[path.length - 1],
);
}
}
};

export default (docs, tekton, report) => {
for (const pipeline of Object.values<any>(tekton.pipelines)) {
walk(pipeline.spec.tasks, ['spec', 'tasks'], naming(pipeline.metadata.name, 'params', report));
walk(pipeline.spec.finally, ['spec', 'finally'], naming(pipeline.metadata.name, 'params', report));
}

for (const pipeline of Object.values<any>(tekton.pipelineRuns)) {
walk(pipeline.spec.tasks, ['spec', 'pipelineSpec', 'tasks'], naming(pipeline.metadata.name, 'params', report));
walk(
pipeline.spec.finally,
['spec', 'pipelineSpec', 'finally'],
naming(pipeline.metadata.name, 'params', report),
);
}
};

0 comments on commit 919a3d2

Please sign in to comment.