Skip to content

Commit

Permalink
awslint
Browse files Browse the repository at this point in the history
  • Loading branch information
Niranjan Jayakar committed Sep 22, 2020
1 parent b262cfc commit aba1dd8
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-s3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
"node": ">= 10.13.0 <13 || >=13.7.0"
},
"awslint": {
"flag": [ "use-base-constructs" ],
"exclude": [
"props-physical-name:@aws-cdk/aws-s3.BucketPolicyProps",
"no-unused-type:@aws-cdk/aws-s3.Location",
Expand Down
2 changes: 2 additions & 0 deletions packages/awslint/bin/awslint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ async function main() {
.command('list', 'list all available rules')
.option('include', { alias: 'i', type: 'array', desc: 'evaluate only this rule(s)', default: ['*'] })
.option('exclude', { alias: 'x', type: 'array', desc: 'do not evaludate these rules (takes priority over --include)', default: [] })
.option('flag', { type: 'array', desc: 'flags to apply when passing rules', default: [] })
.option('save', { type: 'boolean', desc: 'updates package.json with "exclude" statements for all failing rules', default: false })
.option('verbose', { alias: 'v', type: 'boolean', desc: 'verbose output (prints all assertions)', default: false })
.option('quiet', { alias: 'q', type: 'boolean', desc: 'quiet mode - shows only errors', default: false })
Expand Down Expand Up @@ -110,6 +111,7 @@ async function main() {
results.push(...ALL_RULES_LINTER.eval(assembly, {
include: args.include,
exclude: args.exclude,
flags: args.flag,
}));

// Sort errors to the top (highest severity first)
Expand Down
5 changes: 5 additions & 0 deletions packages/awslint/lib/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ export interface LinterOptions {
* @default none
*/
exclude?: string[];

/**
* List of flags to be passed down to the linters
*/
flags?: string[];
}

export abstract class LinterBase {
Expand Down
9 changes: 8 additions & 1 deletion packages/awslint/lib/rules/construct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class ConstructReflection {
return typeRef.fqn;
}

/** @deprecated */
public readonly ROOT_CLASS: reflect.ClassType; // cdk.Construct

public readonly fqn: string;
Expand Down Expand Up @@ -93,9 +94,15 @@ constructLinter.add({

const expectedParams = new Array<MethodSignatureParameterExpectation>();

let baseType;
if (e.options.flags?.includes('use-base-constructs') && !initializer.parentType.name.startsWith('Cfn')) {
baseType = e.ctx.core.baseConstructClass;
} else {
baseType = e.ctx.core.constructClass;
}
expectedParams.push({
name: 'scope',
type: e.ctx.core.constructClass.fqn,
type: baseType.fqn,
});

expectedParams.push({
Expand Down
27 changes: 24 additions & 3 deletions packages/awslint/lib/rules/core-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ import { getDocTag } from './util';
const CORE_MODULE = '@aws-cdk/core';
enum CoreTypesFqn {
CfnResource = '@aws-cdk/core.CfnResource',
Construct = '@aws-cdk/core.Construct',
ConstructInterface = '@aws-cdk/core.IConstruct',
Resource = '@aws-cdk/core.Resource',
ResourceInterface = '@aws-cdk/core.IResource',
ResolvableInterface = '@aws-cdk/core.IResolvable',
PhysicalName = '@aws-cdk/core.PhysicalName'
PhysicalName = '@aws-cdk/core.PhysicalName',

BaseConstruct = 'constructs.Construct',
BaseConstructInterface = 'constructs.Construct',

// @deprecated(v2)
Construct = '@aws-cdk/core.Construct',
ConstructInterface = '@aws-cdk/core.IConstruct',
}

export class CoreTypes {
Expand Down Expand Up @@ -86,18 +91,34 @@ export class CoreTypes {

/**
* @returns `classType` for the core type Construct
* @deprecated - use `baseConstructClass()`
*/
public get constructClass() {
return this.sys.findClass(CoreTypesFqn.Construct);
}

/**
* @returns `classType` for the core type Construct
*/
public get baseConstructClass() {
return this.sys.findClass(CoreTypesFqn.BaseConstruct);
}

/**
* @returns `interfacetype` for the core type Construct
* @deprecated - use `baseConstructInterface()`
*/
public get constructInterface() {
return this.sys.findInterface(CoreTypesFqn.ConstructInterface);
}

/**
* @returns `interfacetype` for the core type Construct
*/
public get baseConstructInterface() {
return this.sys.findInterface(CoreTypesFqn.BaseConstructInterface);
}

/**
* @returns `classType` for the core type Construct
*/
Expand Down
8 changes: 6 additions & 2 deletions packages/awslint/lib/rules/imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ importsLinter.add({
// "fromRoleArn" => "roleArn"
const argName = e.ctx.resource.basename[0].toLocaleLowerCase() + method.name.slice('from'.length + 1);

const baseType = e.options.flags?.includes('use-base-constructs') ? e.ctx.resource.core.baseConstructClass :
e.ctx.resource.core.constructClass;
e.assertSignature(method, {
parameters: [
{ name: 'scope', type: e.ctx.resource.construct.ROOT_CLASS },
{ name: 'scope', type: baseType },
{ name: 'id', type: 'string' },
{ name: argName, type: 'string' },
],
Expand All @@ -90,9 +92,11 @@ importsLinter.add({
return;
}

const baseType = e.options.flags?.includes('use-base-constructs') ? e.ctx.resource.core.baseConstructClass
: e.ctx.resource.core.constructClass;
e.assertSignature(e.ctx.fromAttributesMethod, {
parameters: [
{ name: 'scope', type: e.ctx.resource.construct.ROOT_CLASS },
{ name: 'scope', type: baseType },
{ name: 'id', type: 'string' },
{ name: 'attrs', type: e.ctx.attributesStruct },
],
Expand Down

0 comments on commit aba1dd8

Please sign in to comment.