Skip to content

Commit

Permalink
Merge pull request Azure#3 from arpanlaha/json-rules
Browse files Browse the repository at this point in the history
Implement JSON rules
  • Loading branch information
arpanlaha authored Jun 13, 2019
2 parents c94624d + 8967ae2 commit 4682d45
Show file tree
Hide file tree
Showing 50 changed files with 2,613 additions and 259 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ Linting rules for the JavaScript/TypeScript Azure SDK - derived from the [Azure

You'll first need to install [ESLint](http://eslint.org):

```
$ npm i eslint --save-dev
```shell
npm i eslint --save-dev
```

Next, install `eslint-plugin-azure`:

```
$ npm install eslint-plugin-azure --save-dev
```shell
npm install eslint-plugin-azure --save-dev
```

**Note:** If you installed ESLint globally (using the `-g` flag) then you must also install `eslint-plugin-azure` globally.
Expand Down Expand Up @@ -51,14 +51,21 @@ If you need to modify or disable specific rules, you can do so in the `rules` se
- [ts-config-allowsyntheticdefaultimports](https://azuresdkspecs.z5.web.core.windows.net/TypeScriptSpec.html#ts-config-allowsyntheticdefaultimports)
- [ts-config-declaration](https://azuresdkspecs.z5.web.core.windows.net/TypeScriptSpec.html#ts-config-declaration)
- [ts-config-esmoduleinterop](https://azuresdkspecs.z5.web.core.windows.net/TypeScriptSpec.html#ts-config-esmoduleinterop)
- [ts-config-exclude](https://azuresdkspecs.z5.web.core.windows.net/TypeScriptSpec.html#ts-config-exclude)
- [ts-config-forceconsistentcasinginfilenames](https://azuresdkspecs.z5.web.core.windows.net/TypeScriptSpec.html#ts-config-forceconsistentcasinginfilenames)
- [ts-config-importhelpers](https://azuresdkspecs.z5.web.core.windows.net/TypeScriptSpec.html#ts-config-importhelpers)
- [ts-config-isolatedmodules](https://azuresdkspecs.z5.web.core.windows.net/TypeScriptSpec.html#ts-config-isolatedmodules)
- [ts-config-lib](https://azuresdkspecs.z5.web.core.windows.net/TypeScriptSpec.html#ts-config-lib)
- [ts-config-module](https://azuresdkspecs.z5.web.core.windows.net/TypeScriptSpec.html#ts-config-module)
- [ts-config-no-experimentaldecorators](https://azuresdkspecs.z5.web.core.windows.net/TypeScriptSpec.html#ts-config-no-experimentaldecorators)
- [ts-config-sourcemap](https://azuresdkspecs.z5.web.core.windows.net/TypeScriptSpec.html#ts-config-sourcemap)
- [ts-config-strict](https://azuresdkspecs.z5.web.core.windows.net/TypeScriptSpec.html#ts-config-strict)
- [ts-package-json-author](https://azuresdkspecs.z5.web.core.windows.net/TypeScriptSpec.html#ts-package-json-author)
- [ts-package-json-bugs](https://azuresdkspecs.z5.web.core.windows.net/TypeScriptSpec.html#ts-package-json-bugs)
- [ts-package-json-homepage](https://azuresdkspecs.z5.web.core.windows.net/TypeScriptSpec.html#ts-package-json-homepage)
- [ts-package-json-keywords](https://azuresdkspecs.z5.web.core.windows.net/TypeScriptSpec.html#ts-package-json-keywords)
- [ts-package-json-license](https://azuresdkspecs.z5.web.core.windows.net/TypeScriptSpec.html#ts-package-json-license)
- [ts-package-json-name](https://azuresdkspecs.z5.web.core.windows.net/TypeScriptSpec.html#ts-package-json-name)
- [ts-package-json-repo](https://azuresdkspecs.z5.web.core.windows.net/TypeScriptSpec.html#ts-package-json-repo)
- [ts-package-json-required-scripts](https://azuresdkspecs.z5.web.core.windows.net/TypeScriptSpec.html#ts-package-json-required-scripts)
- [ts-package-json-sideeffects](https://azuresdkspecs.z5.web.core.windows.net/TypeScriptSpec.html#ts-package-json-sideeffects)
7 changes: 7 additions & 0 deletions src/configs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,23 @@ export = {
"azure/ts-config-allowsyntheticdefaultimports": "error",
"azure/ts-config-declaration": "error",
"azure/ts-config-esmoduleinterop": "error",
"azure/ts-config-exclude": "error",
"azure/ts-config-forceconsistentcasinginfilenames": "error",
"azure/ts-config-importhelpers": "error",
"azure/ts-config-isolatedmodules": "warn",
"azure/ts-config-lib": "error",
"azure/ts-config-module": "error",
"azure/ts-config-no-experimentaldecorators": "error",
"azure/ts-config-sourcemap": "error",
"azure/ts-config-strict": "error",
"azure/ts-package-json-author": "error",
"azure/ts-package-json-bugs": "error",
"azure/ts-package-json-homepage": "error",
"azure/ts-package-json-keywords": "error",
"azure/ts-package-json-license": "error",
"azure/ts-package-json-name": "error",
"azure/ts-package-json-repo": "error",
"azure/ts-package-json-required-scripts": "error",
"azure/ts-package-json-sideeffects": "error"
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/processors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ import { Linter } from "eslint";

export = {
".json": {
preprocess: function(text: string): string[] {
preprocess: (text: string): string[] => {
const code = "const json = " + text;
return [code];
},
postprocess: function(
messages: Linter.LintMessage[][]
): Linter.LintMessage[] {
return messages[0].filter(function(message) {
postprocess: (messages: Linter.LintMessage[][]): Linter.LintMessage[] => {
return messages[0].filter((message: Linter.LintMessage): boolean => {
return (
message.ruleId !== "no-unused-vars" &&
message.ruleId !== "@typescript-eslint/no-unused-vars"
Expand Down
14 changes: 14 additions & 0 deletions src/rules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,45 @@
import tsConfigAllowSyntheticDefaultImports from "./ts-config-allowsyntheticdefaultimports";
import tsConfigDeclaration from "./ts-config-declaration";
import tsConfigEsModuleInterop from "./ts-config-esmoduleinterop";
import tsConfigExclude from "./ts-config-exclude";
import tsConfigForceConsistentCasingInFileNames from "./ts-config-forceconsistentcasinginfilenames";
import tsConfigImportHelpers from "./ts-config-importhelpers";
import tsConfigIsolatedModules from "./ts-config-isolatedmodules";
import tsConfigLib from "./ts-config-lib";
import tsConfigModule from "./ts-config-module";
import tsConfigNoExperimentalDecorators from "./ts-config-no-experimentaldecorators";
import tsConfigSourceMap from "./ts-config-sourcemap";
import tsConfigStrict from "./ts-config-strict";
import tsPackageJsonAuthor from "./ts-package-json-author";
import tsPackageJsonBugs from "./ts-package-json-bugs";
import tsPackageJsonHomepage from "./ts-package-json-homepage";
import tsPackageJsonKeywords from "./ts-package-json-keywords";
import tsPackageJsonLicense from "./ts-package-json-license";
import tsPackageJsonName from "./ts-package-json-name";
import tsPackageJsonRepo from "./ts-package-json-repo";
import tsPackageJsonRequiredScripts from "./ts-package-json-required-scripts";
import tsPackageJsonSideEffects from "./ts-package-json-sideeffects";

export = {
"ts-config-allowsyntheticdefaultimports": tsConfigAllowSyntheticDefaultImports,
"ts-config-declaration": tsConfigDeclaration,
"ts-config-esmoduleinterop": tsConfigEsModuleInterop,
"ts-config-exclude": tsConfigExclude,
"ts-config-forceconsistentcasinginfilenames": tsConfigForceConsistentCasingInFileNames,
"ts-config-importhelpers": tsConfigImportHelpers,
"ts-config-isolatedmodules": tsConfigIsolatedModules,
"ts-config-lib": tsConfigLib,
"ts-config-module": tsConfigModule,
"ts-config-no-experimentaldecorators": tsConfigNoExperimentalDecorators,
"ts-config-sourcemap": tsConfigSourceMap,
"ts-config-strict": tsConfigStrict,
"ts-package-json-author": tsPackageJsonAuthor,
"ts-package-json-bugs": tsPackageJsonBugs,
"ts-package-json-homepage": tsPackageJsonHomepage,
"ts-package-json-keywords": tsPackageJsonKeywords,
"ts-package-json-license": tsPackageJsonLicense,
"ts-package-json-name": tsPackageJsonName,
"ts-package-json-repo": tsPackageJsonRepo,
"ts-package-json-required-scripts": tsPackageJsonRequiredScripts,
"ts-package-json-sideeffects": tsPackageJsonSideEffects
};
14 changes: 7 additions & 7 deletions src/rules/ts-config-allowsyntheticdefaultimports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author Arpan Laha
*/

import structure from "../utils/structure";
import getVerifiers from "../utils/verifiers";
import { Rule } from "eslint";

//------------------------------------------------------------------------------
Expand All @@ -24,25 +24,25 @@ export = {
},
schema: [] // no options
},
create: function(context: Rule.RuleContext) {
var checkers = structure(context, {
create: (context: Rule.RuleContext): Rule.RuleListener => {
const verifiers = getVerifiers(context, {
outer: "compilerOptions",
inner: "allowSyntheticDefaultImports",
expectedValue: true,
expected: true,
fileName: "tsconfig.json"
});
return {
// callback functions

// check to see if compilerOptions exists at the outermost level
"VariableDeclarator > ObjectExpression": checkers.existsInFile,
"VariableDeclarator > ObjectExpression": verifiers.existsInFile,

// check that allowSyntheticDefaultImports is a member of compilerOptions
"Property[key.value='compilerOptions']": checkers.isMemberOf,
"Property[key.value='compilerOptions']": verifiers.isMemberOf,

// check the node corresponding to compilerOptions.allowSyntheticDefaultImports to see if it is set to true
"VariableDeclarator > ObjectExpression > Property[key.value='compilerOptions'] > ObjectExpression > Property[key.value='allowSyntheticDefaultImports']":
checkers.innerMatchesExpected
verifiers.innerMatchesExpected
} as Rule.RuleListener;
}
};
14 changes: 7 additions & 7 deletions src/rules/ts-config-declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author Arpan Laha
*/

import structure from "../utils/structure";
import getVerifiers from "../utils/verifiers";
import { Rule } from "eslint";

//------------------------------------------------------------------------------
Expand All @@ -24,25 +24,25 @@ export = {
},
schema: [] // no options
},
create: function(context: Rule.RuleContext) {
var checkers = structure(context, {
create: (context: Rule.RuleContext): Rule.RuleListener => {
const verifiers = getVerifiers(context, {
outer: "compilerOptions",
inner: "declaration",
expectedValue: true,
expected: true,
fileName: "tsconfig.json"
});
return {
// callback functions

// check to see if compilerOptions exists at the outermost level
"VariableDeclarator > ObjectExpression": checkers.existsInFile,
"VariableDeclarator > ObjectExpression": verifiers.existsInFile,

// check that declaration is a member of compilerOptions
"Property[key.value='compilerOptions']": checkers.isMemberOf,
"Property[key.value='compilerOptions']": verifiers.isMemberOf,

// check the node corresponding to compilerOptions.declaration to see if it is set to true
"VariableDeclarator > ObjectExpression > Property[key.value='compilerOptions'] > ObjectExpression > Property[key.value='declaration']":
checkers.innerMatchesExpected
verifiers.innerMatchesExpected
} as Rule.RuleListener;
}
};
14 changes: 7 additions & 7 deletions src/rules/ts-config-esmoduleinterop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

"use strict";

import structure from "../utils/structure";
import getVerifiers from "../utils/verifiers";
import { Rule } from "eslint";

//------------------------------------------------------------------------------
Expand All @@ -26,25 +26,25 @@ export = {
},
schema: [] // no options
},
create: function(context: Rule.RuleContext) {
var checkers = structure(context, {
create: (context: Rule.RuleContext): Rule.RuleListener => {
const verifiers = getVerifiers(context, {
outer: "compilerOptions",
inner: "esModuleInterop",
expectedValue: true,
expected: true,
fileName: "tsconfig.json"
});
return {
// callback functions

// check to see if compilerOptions exists at the outermost level
"VariableDeclarator > ObjectExpression": checkers.existsInFile,
"VariableDeclarator > ObjectExpression": verifiers.existsInFile,

// check that esModuleInterop is a member of compilerOptions
"Property[key.value='compilerOptions']": checkers.isMemberOf,
"Property[key.value='compilerOptions']": verifiers.isMemberOf,

// check the node corresponding to compilerOptions.esModuleInterop to see if it is set to true
"VariableDeclarator > ObjectExpression > Property[key.value='compilerOptions'] > ObjectExpression > Property[key.value='esModuleInterop']":
checkers.innerMatchesExpected
verifiers.innerMatchesExpected
} as Rule.RuleListener;
}
};
44 changes: 44 additions & 0 deletions src/rules/ts-config-exclude.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* @fileoverview Rule to force tsconfig.json's "exclude" value to at least contain "node_modules"
* @author Arpan Laha
*/

import getVerifiers from "../utils/verifiers";
import { Rule } from "eslint";

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

export = {
meta: {
type: "problem",

docs: {
description:
"force tsconfig.json's compilerOptions.exclude value to at least contain 'node_modules'",
category: "Best Practices",
recommended: true,
url:
"https://azuresdkspecs.z5.web.core.windows.net/TypeScriptSpec.html#ts-config-exclude"
},
schema: [] // no options
},
create: (context: Rule.RuleContext): Rule.RuleListener => {
const verifiers = getVerifiers(context, {
outer: "exclude",
expected: "node_modules",
fileName: "tsconfig.json"
});
return {
// callback functions

// check to see if exclude exists at the outermost level
"VariableDeclarator > ObjectExpression": verifiers.existsInFile,

// check the node corresponding to exclude to see if its value contains "node_modules"
"VariableDeclarator > ObjectExpression > Property[key.value='exclude']":
verifiers.outerContainsExpected
} as Rule.RuleListener;
}
};
14 changes: 7 additions & 7 deletions src/rules/ts-config-forceconsistentcasinginfilenames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author Arpan Laha
*/

import structure from "../utils/structure";
import getVerifiers from "../utils/verifiers";
import { Rule } from "eslint";

//------------------------------------------------------------------------------
Expand All @@ -24,25 +24,25 @@ export = {
},
schema: [] // no options
},
create: function(context: Rule.RuleContext) {
var checkers = structure(context, {
create: (context: Rule.RuleContext): Rule.RuleListener => {
const verifiers = getVerifiers(context, {
outer: "compilerOptions",
inner: "forceConsistentCasingInFileNames",
expectedValue: true,
expected: true,
fileName: "tsconfig.json"
});
return {
// callback functions

// check to see if compilerOptions exists at the outermost level
"VariableDeclarator > ObjectExpression": checkers.existsInFile,
"VariableDeclarator > ObjectExpression": verifiers.existsInFile,

// check that strict is a member of compilerOptions
"Property[key.value='compilerOptions']": checkers.isMemberOf,
"Property[key.value='compilerOptions']": verifiers.isMemberOf,

// check the node corresponding to compilerOptions.forceConsistentCasingInFileNames to see if it is set to true
"VariableDeclarator > ObjectExpression > Property[key.value='compilerOptions'] > ObjectExpression > Property[key.value='forceConsistentCasingInFileNames']":
checkers.innerMatchesExpected
verifiers.innerMatchesExpected
} as Rule.RuleListener;
}
};
14 changes: 7 additions & 7 deletions src/rules/ts-config-importhelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author Arpan Laha
*/

import structure from "../utils/structure";
import getVerifiers from "../utils/verifiers";
import { Rule } from "eslint";

//------------------------------------------------------------------------------
Expand All @@ -24,25 +24,25 @@ export = {
},
schema: [] // no options
},
create: function(context: Rule.RuleContext) {
var checkers = structure(context, {
create: (context: Rule.RuleContext): Rule.RuleListener => {
const verifiers = getVerifiers(context, {
outer: "compilerOptions",
inner: "importHelpers",
expectedValue: true,
expected: true,
fileName: "tsconfig.json"
});
return {
// callback functions

// check to see if compilerOptions exists at the outermost level
"VariableDeclarator > ObjectExpression": checkers.existsInFile,
"VariableDeclarator > ObjectExpression": verifiers.existsInFile,

// check that importHelpers is a member of compilerOptions
"Property[key.value='compilerOptions']": checkers.isMemberOf,
"Property[key.value='compilerOptions']": verifiers.isMemberOf,

// check the node corresponding to compilerOptions.importHelpers to see if it is set to true
"VariableDeclarator > ObjectExpression > Property[key.value='compilerOptions'] > ObjectExpression > Property[key.value='importHelpers']":
checkers.innerMatchesExpected
verifiers.innerMatchesExpected
} as Rule.RuleListener;
}
};
Loading

0 comments on commit 4682d45

Please sign in to comment.