Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup eslint with TypeScript configuration #4

Merged
merged 6 commits into from
Jun 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 37 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"env": {
"es6": true,
"jest": true,
"node": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"prettier",
"prettier/@typescript-eslint"
],
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "./tsconfig.json"
},
"plugins": ["@typescript-eslint", "prettier"],
"rules": {
"linebreak-style": ["error", "unix"],
"no-case-declarations": "warn",
"quotes": ["error", "single", { "avoidEscape": true }],
"semi": ["error", "always"],

"@typescript-eslint/explicit-function-return-type": ["error", { "allowExpressions": true }],
"@typescript-eslint/indent": ["error", "tab"],
"@typescript-eslint/interface-name-prefix": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-inferrable-types": "off",
"@typescript-eslint/no-parameter-properties": "off",
"@typescript-eslint/prefer-string-starts-ends-with": "error"
}
}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"eslint.validate": ["javascript", "typescript"]
}
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "dist/index.js",
"scripts": {
"build": "tsc",
"lint": "",
"lint": "eslint --ext .ts src",
"test": "jest"
},
"keywords": [
Expand All @@ -31,6 +31,11 @@
"@types/jest": "~24.0.15",
"@types/node": "~12.0.10",
"@types/prettier": "~1.16.4",
"@typescript-eslint/eslint-plugin": "~1.11.0",
"@typescript-eslint/parser": "~1.11.0",
"eslint": "~6.0.1",
"eslint-config-prettier": "~6.0.0",
"eslint-plugin-prettier": "~3.1.0",
"jest": "~24.8.0",
"ts-jest": "~24.0.2",
"typescript": "~3.5.2"
Expand Down
19 changes: 10 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const plugin: Plugin = {
let _options: ParserOptions = { ...options };
for (const plugin of options.plugins) {
if (typeof plugin !== 'string') {
if (plugin.parsers && plugin.parsers.hasOwnProperty('pug')) {
if (plugin.parsers && Object.prototype.hasOwnProperty.call(plugin.parsers, 'pug')) {
_options = { ..._options, ...plugin.defaultOptions, ...plugin.options };
}
}
Expand Down Expand Up @@ -184,7 +184,7 @@ export const plugin: Plugin = {
}

if (previousToken && previousToken.type === 'attribute') {
result += `, `;
result += ', ';
}

result += `${token.name}`;
Expand All @@ -204,11 +204,11 @@ export const plugin: Plugin = {
val = val.replace('[ ', '[').replace(' ]', ']');
if (quotationType(val) === 'SINGLE') {
// Swap single and double quotes
val = val.replace(/[\'\"]/g, (match) => (match === '"' ? "'" : '"'));
val = val.replace(/['"]/g, (match) => (match === '"' ? "'" : '"'));
}
} else if (val.startsWith("'")) {
// Swap single and double quotes
val = val.replace(/[\'\"]/g, (match) => (match === '"' ? "'" : '"'));
val = val.replace(/['"]/g, (match) => (match === '"' ? "'" : '"'));
} else if (val === 'true') {
// The value is exactly true and is not quoted
break;
Expand All @@ -223,7 +223,7 @@ export const plugin: Plugin = {
}
break;
case 'end-attributes':
if (result.charAt(result.length - 1) === '(') {
if (result.endsWith('(')) {
// There were no attributes
result = result.substring(0, result.length - 1);
} else if (previousToken && previousToken.type === 'attribute') {
Expand Down Expand Up @@ -271,7 +271,7 @@ export const plugin: Plugin = {
result = result.substring(0, result.length - 1);
}
// Insert one newline
result += `\n`;
result += '\n';
break;
case 'comment':
result += indent.repeat(indentLevel);
Expand Down Expand Up @@ -333,7 +333,7 @@ export const plugin: Plugin = {
code = code.trim();
if (quotationType(code) === 'DOUBLE') {
val = '{{ ';
val += code.replace(/[\'\"]/g, (match) => (match === '"' ? "'" : '"'));
val += code.replace(/['"]/g, (match) => (match === '"' ? "'" : '"'));
val += ' }}';
}
}
Expand All @@ -360,7 +360,6 @@ export const plugin: Plugin = {
break;
case 'id':
// Handle id attribute
let idVal = token.val;
// Write css-id in front of css-classes
let lastPositionOfNewline = result.lastIndexOf('\n');
if (lastPositionOfNewline === -1) {
Expand All @@ -383,7 +382,9 @@ export const plugin: Plugin = {
break;
}
}
result = [result.slice(0, position), _indent, `#${idVal}`, result.slice(position)].join('');
result = [result.slice(0, position), _indent, `#${token.val}`, result.slice(position)].join(
''
);
break;
case 'start-pipeless-text':
pipelessText = true;
Expand Down
2 changes: 1 addition & 1 deletion src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class Logger implements ILogger {
'error'
];

constructor(private readonly logger: ILogger = console, private level: LogLevel = LogLevel.INFO) {}
public constructor(private readonly logger: ILogger = console, private level: LogLevel = LogLevel.INFO) {}

public setLogLevel(level: LogLevel): void {
this.level = level;
Expand Down