Skip to content

Commit 5b3aef8

Browse files
authored
style(commons): apply standardized formatting (#1454)
1 parent 213188b commit 5b3aef8

21 files changed

+424
-290
lines changed

packages/commons/.eslintrc.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
module.exports = {
2+
env: {
3+
browser: false,
4+
es2020: true,
5+
jest: true,
6+
node: true,
7+
},
8+
ignorePatterns: ['coverage', 'lib'],
9+
extends: [
10+
'plugin:@typescript-eslint/recommended',
11+
'plugin:prettier/recommended',
12+
],
13+
parser: '@typescript-eslint/parser',
14+
plugins: ['@typescript-eslint', 'prettier'],
15+
settings: {
16+
'import/resolver': {
17+
node: {},
18+
typescript: {
19+
project: './tsconfig.json',
20+
alwaysTryTypes: true,
21+
},
22+
},
23+
},
24+
rules: {
25+
'@typescript-eslint/explicit-function-return-type': [
26+
'error',
27+
{ allowExpressions: true },
28+
], // Enforce return type definitions for functions
29+
'@typescript-eslint/explicit-member-accessibility': 'error', // Enforce explicit accessibility modifiers on class properties and methods (public, private, protected)
30+
'@typescript-eslint/member-ordering': [
31+
// Standardize the order of class members
32+
'error',
33+
{
34+
default: {
35+
memberTypes: [
36+
'signature',
37+
'public-field',
38+
'protected-field',
39+
'private-field',
40+
'constructor',
41+
'public-method',
42+
'protected-method',
43+
'private-method',
44+
],
45+
order: 'alphabetically',
46+
},
47+
},
48+
],
49+
'@typescript-eslint/no-explicit-any': 'error', // Disallow usage of the any type
50+
'@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_' }], // Disallow unused variables, except for variables starting with an underscore
51+
'@typescript-eslint/no-use-before-define': ['off'], // Check if this rule is needed
52+
'no-unused-vars': 'off', // Disable eslint core rule, since it's replaced by @typescript-eslint/no-unused-vars
53+
// Rules from eslint core https://eslint.org/docs/latest/rules/
54+
'array-bracket-spacing': ['error', 'never'], // Disallow spaces inside of array brackets
55+
'computed-property-spacing': ['error', 'never'], // Disallow spaces inside of computed properties
56+
'func-style': ['warn', 'expression'], // Enforce function expressions instead of function declarations
57+
'keyword-spacing': 'error', // Enforce spaces after keywords and before parenthesis, e.g. if (condition) instead of if(condition)
58+
'padding-line-between-statements': [
59+
// Require an empty line before return statements
60+
'error',
61+
{ blankLine: 'always', prev: '*', next: 'return' },
62+
],
63+
'no-console': 0, // Allow console.log statements
64+
'no-multi-spaces': ['error', { ignoreEOLComments: false }], // Disallow multiple spaces except for comments
65+
'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
66+
'no-throw-literal': 'error', // Disallow throwing literals as exceptions, e.g. throw 'error' instead of throw new Error('error')
67+
'object-curly-spacing': ['error', 'always'], // Enforce spaces inside of curly braces in objects
68+
'prefer-arrow-callback': 'error', // Enforce arrow functions instead of anonymous functions for callbacks
69+
quotes: ['error', 'single', { allowTemplateLiterals: true }], // Enforce single quotes except for template strings
70+
semi: ['error', 'always'], // Require semicolons instead of ASI (automatic semicolon insertion) at the end of statements
71+
},
72+
};

packages/commons/jest.config.js

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,24 @@ module.exports = {
33
name: 'AWS Lambda Powertools utility: COMMONS',
44
color: 'red',
55
},
6-
'preset': 'ts-jest',
7-
'transform': {
6+
preset: 'ts-jest',
7+
transform: {
88
'^.+\\.ts?$': 'ts-jest',
99
},
10-
moduleFileExtensions: [ 'js', 'ts' ],
11-
'collectCoverageFrom': [
12-
'**/src/**/*.ts',
13-
'!**/node_modules/**',
14-
],
15-
'testMatch': ['**/?(*.)+(spec|test).ts'],
16-
'roots': [
17-
'<rootDir>/src',
18-
'<rootDir>/tests',
19-
],
20-
'testPathIgnorePatterns': [
21-
'/node_modules/',
22-
],
23-
'testEnvironment': 'node',
24-
'coveragePathIgnorePatterns': [
25-
'/node_modules/',
26-
],
27-
'coverageThreshold': {
28-
'global': {
29-
'statements': 100,
30-
'branches': 100,
31-
'functions': 100,
32-
'lines': 100,
10+
moduleFileExtensions: ['js', 'ts'],
11+
collectCoverageFrom: ['**/src/**/*.ts', '!**/node_modules/**'],
12+
testMatch: ['**/?(*.)+(spec|test).ts'],
13+
roots: ['<rootDir>/src', '<rootDir>/tests'],
14+
testPathIgnorePatterns: ['/node_modules/'],
15+
testEnvironment: 'node',
16+
coveragePathIgnorePatterns: ['/node_modules/'],
17+
coverageThreshold: {
18+
global: {
19+
statements: 100,
20+
branches: 100,
21+
functions: 100,
22+
lines: 100,
3323
},
3424
},
35-
'coverageReporters': [
36-
'json-summary',
37-
'text',
38-
'lcov' ],
39-
};
25+
coverageReporters: ['json-summary', 'text', 'lcov'],
26+
};

packages/commons/package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
"test:e2e": "echo 'Not Applicable'",
1717
"watch": "jest --watch",
1818
"build": "tsc",
19-
"lint": "eslint --ext .ts --no-error-on-unmatched-pattern src tests",
20-
"lint-fix": "eslint --fix --ext .ts --no-error-on-unmatched-pattern src tests",
19+
"lint": "eslint --ext .ts,.js --no-error-on-unmatched-pattern .",
20+
"lint-fix": "eslint --fix --ext .ts,.js --no-error-on-unmatched-pattern .",
2121
"package": "mkdir -p dist/ && npm pack && mv *.tgz dist/",
2222
"package-bundle": "../../package-bundler.sh commons-bundle ./dist",
2323
"prepare": "npm run build"
2424
},
2525
"lint-staged": {
26-
"*.ts": "npm run lint-fix"
26+
"*.ts": "npm run lint-fix",
27+
"*.js": "npm run lint-fix"
2728
},
2829
"homepage": "https://github.com/awslabs/aws-lambda-powertools-typescript/tree/main/packages/metrics#readme",
2930
"license": "MIT-0",
@@ -51,4 +52,4 @@
5152
"@aws-sdk/client-lambda": "^3.310.0",
5253
"@aws-sdk/util-utf8-node": "^3.259.0"
5354
}
54-
}
55+
}

packages/commons/src/Utility.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
11
/**
22
* ## Intro
33
* Utility is a base class that other Powertools utilites can extend to inherit shared logic.
4-
*
5-
*
4+
*
5+
*
66
* ## Key features
7-
* * Cold Start heuristic to determine if the current
8-
*
7+
* * Cold Start heuristic to determine if the current
8+
*
99
* ## Usage
10-
*
10+
*
1111
* ### Cold Start
12-
*
12+
*
1313
* Cold start is a term commonly used to describe the `Init` phase of a Lambda function. In this phase, Lambda creates or unfreezes an execution environment with the configured resources, downloads the code for the function and all layers, initializes any extensions, initializes the runtime, and then runs the function’s initialization code (the code outside the main handler). The Init phase happens either during the first invocation, or in advance of function invocations if you have enabled provisioned concurrency.
14-
*
14+
*
1515
* To learn more about the Lambda execution environment lifecycle, see the [Execution environment section](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-context.html) of the AWS Lambda documentation.
16-
*
16+
*
1717
* As a Powertools user you probably won't be using this class directly, in fact if you use other Powertools utilities the cold start heuristic found here is already used to:
1818
* * Add a `coldStart` key to the structured logs when injecting context information in `Logger`
1919
* * Emit a metric during a cold start function invocation in `Metrics`
2020
* * Annotate the invocation segment with a `coldStart` key in `Tracer`
21-
*
21+
*
2222
* If you want to use this logic in your own utilities, `Utility` provides two methods:
23-
*
23+
*
2424
* #### `getColdStart()`
25-
*
25+
*
2626
* Since the `Utility` class is instantiated outside of the Lambda handler it will persist across invocations of the same execution environment. This means that if you call `getColdStart()` multiple times, it will return `true` during the first invocation, and `false` afterwards.
27-
*
27+
*
2828
* @example
2929
* ```typescript
3030
* import { Utility } from '@aws-lambda-powertools/commons';
31-
*
31+
*
3232
* const utility = new Utility();
33-
*
33+
*
3434
* export const handler = async (_event: any, _context: any) => {
3535
* utility.getColdStart();
3636
* };
3737
* ```
38-
*
38+
*
3939
* #### `isColdStart()`
40-
*
40+
*
4141
* This method is an alias of `getColdStart()` and is exposed for convenience and better readability in certain usages.
42-
*
42+
*
4343
* @example
4444
* ```typescript
4545
* import { Utility } from '@aws-lambda-powertools/commons';
46-
*
46+
*
4747
* const utility = new Utility();
48-
*
48+
*
4949
* export const handler = async (_event: any, _context: any) => {
5050
* if (utility.isColdStart()) {
5151
* // do something, this block is only executed on the first invocation of the function
@@ -56,7 +56,7 @@
5656
* ```
5757
*/
5858
export class Utility {
59-
private coldStart: boolean = true;
59+
private coldStart = true;
6060
private readonly defaultServiceName: string = 'service_undefined';
6161

6262
public getColdStart(): boolean {
@@ -78,12 +78,12 @@ export class Utility {
7878
}
7979

8080
/**
81-
* Validate that the service name provided is valid.
82-
* Used internally during initialization.
83-
*
84-
* @param serviceName - Service name to validate
85-
*/
81+
* Validate that the service name provided is valid.
82+
* Used internally during initialization.
83+
*
84+
* @param serviceName - Service name to validate
85+
*/
8686
protected isValidServiceName(serviceName?: string): boolean {
8787
return typeof serviceName === 'string' && serviceName.trim().length > 0;
8888
}
89-
}
89+
}

packages/commons/src/config/ConfigService.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
* @abstract
99
*/
1010
abstract class ConfigService {
11-
1211
/**
1312
* It returns the value of an environment variable that has given name.
1413
*
@@ -26,7 +25,7 @@ abstract class ConfigService {
2625

2726
/**
2827
* It returns the value of the _X_AMZN_TRACE_ID environment variable.
29-
*
28+
*
3029
* The AWS X-Ray Trace data available in the environment variable has this format:
3130
* `Root=1-5759e988-bd862e3fe1be46a994272793;Parent=557abcec3ee5a047;Sampled=1`,
3231
*
@@ -45,6 +44,4 @@ abstract class ConfigService {
4544
public abstract isValueTrue(value: string): boolean;
4645
}
4746

48-
export {
49-
ConfigService,
50-
};
47+
export { ConfigService };

packages/commons/src/config/EnvironmentVariablesService.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import { ConfigService } from '.';
1414
* @see https://awslabs.github.io/aws-lambda-powertools-typescript/latest/#environment-variables
1515
*/
1616
class EnvironmentVariablesService extends ConfigService {
17-
1817
/**
1918
* @see https://awslabs.github.io/aws-lambda-powertools-typescript/latest/#environment-variables
2019
* @protected
@@ -44,7 +43,7 @@ class EnvironmentVariablesService extends ConfigService {
4443

4544
/**
4645
* It returns the value of the _X_AMZN_TRACE_ID environment variable.
47-
*
46+
*
4847
* The AWS X-Ray Trace data available in the environment variable has this format:
4948
* `Root=1-5759e988-bd862e3fe1be46a994272793;Parent=557abcec3ee5a047;Sampled=1`,
5049
*
@@ -60,7 +59,7 @@ class EnvironmentVariablesService extends ConfigService {
6059

6160
/**
6261
* It returns true if the Sampled flag is set in the _X_AMZN_TRACE_ID environment variable.
63-
*
62+
*
6463
* The AWS X-Ray Trace data available in the environment variable has this format:
6564
* `Root=1-5759e988-bd862e3fe1be46a994272793;Parent=557abcec3ee5a047;Sampled=1`,
6665
*
@@ -79,7 +78,7 @@ class EnvironmentVariablesService extends ConfigService {
7978
* @returns boolean
8079
*/
8180
public isValueTrue(value: string): boolean {
82-
const truthyValues: string[] = [ '1', 'y', 'yes', 't', 'true', 'on' ];
81+
const truthyValues: string[] = ['1', 'y', 'yes', 't', 'true', 'on'];
8382

8483
return truthyValues.includes(value.toLowerCase());
8584
}
@@ -98,8 +97,7 @@ class EnvironmentVariablesService extends ConfigService {
9897
const xRayTraceData: Record<string, string> = {};
9998

10099
xRayTraceEnv.split(';').forEach((field) => {
101-
102-
const [ key, value ] = field.split('=');
100+
const [key, value] = field.split('=');
103101

104102
xRayTraceData[key] = value;
105103
});
@@ -108,6 +106,4 @@ class EnvironmentVariablesService extends ConfigService {
108106
}
109107
}
110108

111-
export {
112-
EnvironmentVariablesService,
113-
};
109+
export { EnvironmentVariablesService };

packages/commons/src/config/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
export * from './ConfigService';
2-
export * from './EnvironmentVariablesService';
2+
export * from './EnvironmentVariablesService';

packages/commons/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ export * from './Utility';
33
export * from './config';
44
export * as ContextExamples from './samples/resources/contexts';
55
export * as Events from './samples/resources/events';
6-
export * from './types/middy';
6+
export * from './types/middy';

packages/commons/src/samples/resources/contexts/hello-world.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ const helloworldContext: Context = {
77
memoryLimitInMB: '128',
88
logGroupName: '/aws/lambda/foo-bar-function-123456abcdef',
99
logStreamName: '2021/03/09/[$LATEST]abcdef123456abcdef123456abcdef123456',
10-
invokedFunctionArn: 'arn:aws:lambda:eu-west-1:123456789012:function:foo-bar-function',
10+
invokedFunctionArn:
11+
'arn:aws:lambda:eu-west-1:123456789012:function:foo-bar-function',
1112
awsRequestId: 'c6af9ac6-7b61-11e6-9a41-93e812345678',
1213
getRemainingTimeInMillis: () => 1234,
1314
done: () => console.log('Done!'),
1415
fail: () => console.log('Failed!'),
1516
succeed: () => console.log('Succeeded!'),
1617
};
1718

18-
export {
19-
helloworldContext,
20-
};
19+
export { helloworldContext };
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from './hello-world';
1+
export * from './hello-world';
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * as Custom from './custom';
1+
export * as Custom from './custom';

0 commit comments

Comments
 (0)