Skip to content

Commit 213188b

Browse files
authored
style(layers): apply standardized formatting (#1453)
* style(layers): apply standardized formatting * style(layers): apply standardized formatting
1 parent 5a5e4b4 commit 213188b

10 files changed

+184
-166
lines changed

layers/.eslintrc.js

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,63 +5,67 @@ module.exports = {
55
jest: true,
66
node: true,
77
},
8-
extends: [ 'plugin:@typescript-eslint/eslint-recommended', 'plugin:@typescript-eslint/recommended' ],
8+
extends: [
9+
'plugin:@typescript-eslint/recommended',
10+
'plugin:prettier/recommended',
11+
],
912
parser: '@typescript-eslint/parser',
10-
plugins: ['@typescript-eslint'],
13+
plugins: ['@typescript-eslint', 'prettier'],
1114
settings: {
1215
'import/resolver': {
1316
node: {},
1417
typescript: {
15-
project: './tsconfig.es.json',
18+
project: './tsconfig.json',
1619
alwaysTryTypes: true,
1720
},
1821
},
1922
},
2023
rules: {
21-
'@typescript-eslint/ban-ts-ignore': ['off'],
22-
'@typescript-eslint/camelcase': ['off'],
23-
'@typescript-eslint/explicit-function-return-type': [ 'error', { allowExpressions: true } ],
24-
'@typescript-eslint/explicit-member-accessibility': 'error',
25-
'@typescript-eslint/indent': [ 'error', 2, { SwitchCase: 1 } ],
26-
'@typescript-eslint/interface-name-prefix': ['off'],
27-
'@typescript-eslint/member-delimiter-style': [ 'error', { multiline: { delimiter: 'none' } } ],
24+
'@typescript-eslint/explicit-function-return-type': [
25+
'error',
26+
{ allowExpressions: true },
27+
], // Enforce return type definitions for functions
28+
'@typescript-eslint/explicit-member-accessibility': 'error', // Enforce explicit accessibility modifiers on class properties and methods (public, private, protected)
2829
'@typescript-eslint/member-ordering': [
30+
// Standardize the order of class members
2931
'error',
3032
{
3133
default: {
3234
memberTypes: [
3335
'signature',
34-
'public-field', // = ["public-static-field", "public-instance-field"]
35-
'protected-field', // = ["protected-static-field", "protected-instance-field"]
36-
'private-field', // = ["private-static-field", "private-instance-field"]
36+
'public-field',
37+
'protected-field',
38+
'private-field',
3739
'constructor',
38-
'public-method', // = ["public-static-method", "public-instance-method"]
39-
'protected-method', // = ["protected-static-method", "protected-instance-method"]
40-
'private-method', // = ["private-static-method", "private-instance-method"]
40+
'public-method',
41+
'protected-method',
42+
'private-method',
4143
],
4244
order: 'alphabetically',
4345
},
4446
},
4547
],
46-
'@typescript-eslint/no-explicit-any': 'error',
47-
'@typescript-eslint/no-inferrable-types': ['off'],
48-
'@typescript-eslint/no-unused-vars': [ 'error', { argsIgnorePattern: '^_' } ],
49-
'@typescript-eslint/no-use-before-define': ['off'],
50-
'@typescript-eslint/semi': [ 'error', 'always' ],
51-
'array-bracket-spacing': [ 'error', 'always', { singleValue: false } ],
52-
'arrow-body-style': [ 'error', 'as-needed' ],
53-
'computed-property-spacing': [ 'error', 'never' ],
54-
'func-style': [ 'warn', 'expression' ],
55-
indent: [ 'error', 2, { SwitchCase: 1 } ],
56-
'keyword-spacing': 'error',
57-
'newline-before-return': 2,
58-
'no-console': 0,
59-
'no-multi-spaces': [ 'error', { ignoreEOLComments: false } ],
60-
'no-multiple-empty-lines': [ 'error', { max: 1, maxBOF: 0 } ],
61-
'no-throw-literal': 'error',
62-
'object-curly-spacing': [ 'error', 'always' ],
63-
'prefer-arrow-callback': 'error',
64-
quotes: [ 'error', 'single', { allowTemplateLiterals: true } ],
65-
semi: [ 'error', 'always' ]
48+
'@typescript-eslint/no-explicit-any': 'error', // Disallow usage of the any type
49+
'@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_' }], // Disallow unused variables, except for variables starting with an underscore
50+
'@typescript-eslint/no-use-before-define': ['off'], // Check if this rule is needed
51+
'no-unused-vars': 'off', // Disable eslint core rule, since it's replaced by @typescript-eslint/no-unused-vars
52+
// Rules from eslint core https://eslint.org/docs/latest/rules/
53+
'array-bracket-spacing': ['error', 'never'], // Disallow spaces inside of array brackets
54+
'computed-property-spacing': ['error', 'never'], // Disallow spaces inside of computed properties
55+
'func-style': ['warn', 'expression'], // Enforce function expressions instead of function declarations
56+
'keyword-spacing': 'error', // Enforce spaces after keywords and before parenthesis, e.g. if (condition) instead of if(condition)
57+
'padding-line-between-statements': [
58+
// Require an empty line before return statements
59+
'error',
60+
{ blankLine: 'always', prev: '*', next: 'return' },
61+
],
62+
'no-console': 0, // Allow console.log statements
63+
'no-multi-spaces': ['error', { ignoreEOLComments: false }], // Disallow multiple spaces except for comments
64+
'no-multiple-empty-lines': ['error', { max: 1, maxBOF: 0, maxEOF: 0 }], // Enforce no empty line at the beginning & end of files and max 1 empty line between consecutive statements
65+
'no-throw-literal': 'error', // Disallow throwing literals as exceptions, e.g. throw 'error' instead of throw new Error('error')
66+
'object-curly-spacing': ['error', 'always'], // Enforce spaces inside of curly braces in objects
67+
'prefer-arrow-callback': 'error', // Enforce arrow functions instead of anonymous functions for callbacks
68+
quotes: ['error', 'single', { allowTemplateLiterals: true }], // Enforce single quotes except for template strings
69+
semi: ['error', 'always'], // Require semicolons instead of ASI (automatic semicolon insertion) at the end of statements
6670
},
6771
};

layers/bin/layers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ new LayerPublisherStack(app, 'LayerPublisherStack', {
1111
powertoolsPackageVersion: app.node.tryGetContext('PowertoolsPackageVersion'),
1212
layerName: 'AWSLambdaPowertoolsTypeScript',
1313
ssmParameterLayerArn: SSM_PARAM_LAYER_ARN,
14-
});
14+
});

layers/jest.config.js

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,21 @@ module.exports = {
88
transform: {
99
'^.+\\.ts?$': 'ts-jest',
1010
},
11-
moduleFileExtensions: [ 'js', 'ts' ],
12-
'collectCoverageFrom': [
13-
'**/src/**/*.ts',
14-
'!**/node_modules/**',
15-
],
16-
'testMatch': ['**/?(*.)+(spec|test).ts'],
17-
'roots': [
18-
'<rootDir>/src',
19-
'<rootDir>/tests',
20-
],
21-
'testPathIgnorePatterns': [
22-
'/node_modules/',
23-
],
24-
'testEnvironment': 'node',
25-
'coveragePathIgnorePatterns': [
26-
'/node_modules/',
27-
'/types/',
28-
],
29-
'coverageThreshold': {
30-
'global': {
31-
'statements': 100,
32-
'branches': 100,
33-
'functions': 100,
34-
'lines': 100,
11+
moduleFileExtensions: ['js', 'ts'],
12+
collectCoverageFrom: ['**/src/**/*.ts', '!**/node_modules/**'],
13+
testMatch: ['**/?(*.)+(spec|test).ts'],
14+
roots: ['<rootDir>/src', '<rootDir>/tests'],
15+
testPathIgnorePatterns: ['/node_modules/'],
16+
testEnvironment: 'node',
17+
coveragePathIgnorePatterns: ['/node_modules/', '/types/'],
18+
coverageThreshold: {
19+
global: {
20+
statements: 100,
21+
branches: 100,
22+
functions: 100,
23+
lines: 100,
3524
},
3625
},
37-
'coverageReporters': [
38-
'json-summary',
39-
'text',
40-
'lcov'
41-
],
42-
'setupFiles': [
43-
'<rootDir>/tests/helpers/populateEnvironmentVariables.ts'
44-
]
45-
};
26+
coverageReporters: ['json-summary', 'text', 'lcov'],
27+
setupFiles: ['<rootDir>/tests/helpers/populateEnvironmentVariables.ts'],
28+
};

layers/package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
"test": "echo 'Not applicable'",
1313
"cdk": "cdk",
1414
"package": "echo 'Not applicable'",
15-
"lint": "eslint --ext .ts --no-error-on-unmatched-pattern src tests",
16-
"lint-fix": "eslint --fix --ext .ts --fix --no-error-on-unmatched-pattern src tests",
15+
"lint": "eslint --ext .ts,.js --no-error-on-unmatched-pattern .",
16+
"lint-fix": "eslint --fix --ext .ts,.js --fix --no-error-on-unmatched-pattern .",
1717
"test:unit": "jest --group=unit",
1818
"test:e2e": "RUNTIME=nodejs14x jest --group=e2e"
1919
},
2020
"lint-staged": {
21-
"*.ts": "npm run lint-fix"
21+
"*.ts": "npm run lint-fix",
22+
"*.js": "npm run lint-fix"
2223
},
2324
"repository": {
2425
"type": "git",
@@ -36,4 +37,4 @@
3637
"devDependencies": {
3738
"source-map-support": "^0.5.21"
3839
}
39-
}
40+
}

layers/src/layer-publisher-stack.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import {
2-
CfnOutput,
3-
RemovalPolicy,
4-
Stack,
5-
StackProps
6-
} from 'aws-cdk-lib';
1+
import { CfnOutput, RemovalPolicy, Stack, StackProps } from 'aws-cdk-lib';
72
import { Construct } from 'constructs';
83
import {
94
LayerVersion,
@@ -14,39 +9,49 @@ import {
149
import { StringParameter } from 'aws-cdk-lib/aws-ssm';
1510

1611
export interface LayerPublisherStackProps extends StackProps {
17-
readonly layerName?: string
18-
readonly powertoolsPackageVersion?: string
19-
readonly ssmParameterLayerArn: string
12+
readonly layerName?: string;
13+
readonly powertoolsPackageVersion?: string;
14+
readonly ssmParameterLayerArn: string;
2015
}
2116

2217
export class LayerPublisherStack extends Stack {
2318
public readonly lambdaLayerVersion: LayerVersion;
24-
public constructor(scope: Construct, id: string, props: LayerPublisherStackProps) {
19+
public constructor(
20+
scope: Construct,
21+
id: string,
22+
props: LayerPublisherStackProps
23+
) {
2524
super(scope, id, props);
2625

2726
const { layerName, powertoolsPackageVersion } = props;
2827

29-
console.log(`publishing layer ${layerName} version : ${powertoolsPackageVersion}`);
28+
console.log(
29+
`publishing layer ${layerName} version : ${powertoolsPackageVersion}`
30+
);
3031

3132
this.lambdaLayerVersion = new LayerVersion(this, 'LambdaPowertoolsLayer', {
3233
layerVersionName: props?.layerName,
3334
description: `AWS Lambda Powertools for TypeScript version ${powertoolsPackageVersion}`,
3435
compatibleRuntimes: [
3536
Runtime.NODEJS_14_X,
3637
Runtime.NODEJS_16_X,
37-
Runtime.NODEJS_18_X
38+
Runtime.NODEJS_18_X,
3839
],
3940
license: 'MIT-0',
4041
// This is needed because the following regions do not support the compatibleArchitectures property #1400
4142
// ...(![ 'eu-south-2', 'eu-central-2', 'ap-southeast-4' ].includes(Stack.of(this).region) ? { compatibleArchitectures: [Architecture.X86_64] } : {}),
4243
code: Code.fromAsset('../tmp'),
4344
});
4445

45-
const layerPermission = new CfnLayerVersionPermission(this, 'PublicLayerAccess', {
46-
action: 'lambda:GetLayerVersion',
47-
layerVersionArn: this.lambdaLayerVersion.layerVersionArn,
48-
principal: '*',
49-
});
46+
const layerPermission = new CfnLayerVersionPermission(
47+
this,
48+
'PublicLayerAccess',
49+
{
50+
action: 'lambda:GetLayerVersion',
51+
layerVersionArn: this.lambdaLayerVersion.layerVersionArn,
52+
principal: '*',
53+
}
54+
);
5055

5156
layerPermission.applyRemovalPolicy(RemovalPolicy.RETAIN);
5257
this.lambdaLayerVersion.applyRemovalPolicy(RemovalPolicy.RETAIN);

layers/tests/e2e/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ export const RESOURCE_NAME_PREFIX = 'Layers-E2E';
22
export const ONE_MINUTE = 60 * 1000;
33
export const TEST_CASE_TIMEOUT = 3 * ONE_MINUTE;
44
export const SETUP_TIMEOUT = 5 * ONE_MINUTE;
5-
export const TEARDOWN_TIMEOUT = 5 * ONE_MINUTE;
5+
export const TEARDOWN_TIMEOUT = 5 * ONE_MINUTE;

layers/tests/e2e/layerPublisher.class.test.functionCode.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,22 @@ import { Metrics } from '@aws-lambda-powertools/metrics';
44
import { Tracer } from '@aws-lambda-powertools/tracer';
55

66
const logger = new Logger({
7-
logLevel: 'DEBUG'
7+
logLevel: 'DEBUG',
88
});
99
const metrics = new Metrics();
1010
const tracer = new Tracer();
1111

1212
export const handler = (): void => {
13-
1413
// Check that the packages version matches the expected one
1514
try {
1615
const packageJSON = JSON.parse(
17-
readFileSync('/opt/nodejs/node_modules/@aws-lambda-powertools/logger/package.json', {
18-
encoding: 'utf8',
19-
flag: 'r',
20-
})
16+
readFileSync(
17+
'/opt/nodejs/node_modules/@aws-lambda-powertools/logger/package.json',
18+
{
19+
encoding: 'utf8',
20+
flag: 'r',
21+
}
22+
)
2123
);
2224

2325
if (packageJSON.version != process.env.POWERTOOLS_PACKAGE_VERSION) {
@@ -42,5 +44,4 @@ export const handler = (): void => {
4244
tracer.annotateColdStart();
4345
handlerSegment.close();
4446
tracer.setSegment(segment);
45-
46-
};
47+
};

0 commit comments

Comments
 (0)