Skip to content

Commit

Permalink
chore(ruleset-migrator): me likes this more
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed Dec 29, 2021
1 parent 2999ebd commit e7f6530
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 51 deletions.
2 changes: 1 addition & 1 deletion packages/ruleset-migrator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"url": "https://github.com/stoplightio/spectral.git"
},
"dependencies": {
"@stoplight/json": "3.17.0",
"@stoplight/json": "~3.17.0",
"@stoplight/ordered-object-literal": "1.0.2",
"@stoplight/path": "1.3.2",
"@stoplight/spectral-functions": "^1.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { oas2: oas2, oas3: oas3, oas3_1: oas3_1, oas3_0: oas3_0 } = require('@stoplight/spectral-formats');
const { truthy: truthy } = require('@stoplight/spectral-functions');
const pascalCase = _interopDefault(require('/.tmp/spectral/extends-variant-7/functions/pascalCase.js'));
const pascalCase$0 = _interopDefault(require('/.tmp/spectral/extends-variant-7/assets/functions/pascalCase.js'));
const oas3$0 = _interopDefault(require('/.tmp/spectral/extends-variant-7/assets/functions/oas3.js'));
const { truthy: truthy } = require('@stoplight/spectral-functions');
const pascalCase$1 = _interopDefault(require('/.tmp/spectral/extends-variant-7/assets/fns/pascalCase.js'));
module.exports = {
formats: [oas2, oas3],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { oas2, oas3, oas3_1, oas3_0 } from '@stoplight/spectral-formats';
import { truthy } from '@stoplight/spectral-functions';
import pascalCase from '/.tmp/spectral/extends-variant-7/functions/pascalCase.js';
import pascalCase$0 from '/.tmp/spectral/extends-variant-7/assets/functions/pascalCase.js';
import oas3$0 from '/.tmp/spectral/extends-variant-7/assets/functions/oas3.js';
import { truthy } from '@stoplight/spectral-functions';
import pascalCase$1 from '/.tmp/spectral/extends-variant-7/assets/fns/pascalCase.js';
export default {
formats: [oas2, oas3],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const { oas3: oas3, oas3_1: oas3_1 } = require('@stoplight/spectral-formats');
const oas3$0 = _interopDefault(require('/.tmp/spectral/functions-variant-4/functions/oas3.js'));
const { oas3: oas3$0, oas3_1: oas3_1 } = require('@stoplight/spectral-formats');
const oas3 = _interopDefault(require('/.tmp/spectral/functions-variant-4/functions/oas3.js'));
module.exports = {
formats: [oas3, oas3_1],
formats: [oas3$0, oas3_1],
rules: {
rule: {
given: '$',
then: [
{
function: oas3$0,
function: oas3,
},
],
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { oas3, oas3_1 } from '@stoplight/spectral-formats';
import oas3$0 from '/.tmp/spectral/functions-variant-4/functions/oas3.js';
import { oas3 as oas3$0, oas3_1 } from '@stoplight/spectral-formats';
import oas3 from '/.tmp/spectral/functions-variant-4/functions/oas3.js';
export default {
formats: [oas3, oas3_1],
formats: [oas3$0, oas3_1],
rules: {
rule: {
given: '$',
then: [
{
function: oas3$0,
function: oas3,
},
],
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const { truthy: truthy } = require('@stoplight/spectral-functions');
module.exports = {
rules: {
rule: {
given: '$',
then: {
function: undefined,
function: truthy,
},
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { truthy } from '@stoplight/spectral-functions';
export default {
rules: {
rule: {
given: '$',
then: {
function: undefined,
function: truthy,
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ rules:
rule:
given: $
then:
function: do-nothing
function: truthy
functionsDir: custom-fns
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
rules: {
rule: {
given: '$',
then: {
function: undefined,
},
},
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default {
rules: {
rule: {
given: '$',
then: {
function: undefined,
},
},
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
rules:
rule:
given: $
then:
function: do-nothing
28 changes: 17 additions & 11 deletions packages/ruleset-migrator/src/transformers/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ const transformer: Transformer = function (ctx) {
const { functionsDir, functions } = ruleset;

if (Array.isArray(functions) && functions.length > 0) {
ruleset.functions = functions.map(fn =>
ctx.tree.resolveModule(`${fn}.js`, path.join(ctx.cwd, functionsDir ?? 'functions')),
);
delete ruleset.functionsDir;
for (const fn of functions) {
assertString(fn);
const resolved = ctx.tree.resolveModule(
`${fn}.js`,
path.join(ctx.cwd, typeof functionsDir === 'string' ? functionsDir : 'functions'),
);
const fnName = path.basename(resolved, true);
const identifier = ctx.tree.addImport(fnName, resolved, true);
ctx.tree.scope.store(`function-${fnName}`, identifier.name);
}
}
},
]);
Expand All @@ -25,14 +31,14 @@ const transformer: Transformer = function (ctx) {
/^\/functions$/,
(value): null => {
assertArray(value);
return null;
},
]);

for (const fn of value) {
assertString(fn);
const fnName = path.basename(fn, true);
const identifier = ctx.tree.addImport(fnName, fn, true);
ctx.tree.scope.store(`function-${fnName}`, identifier.name);
}

ctx.hooks.add([
/^\/functionsDir$/,
(value): null => {
assertString(value);
return null;
},
]);
Expand Down
61 changes: 37 additions & 24 deletions packages/ruleset-migrator/src/tree/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,20 @@ import { Scope } from './scope';

export { Scope };

type ImportDefinition = { imported: namedTypes.Identifier; local: namedTypes.Identifier; default: boolean };

function sortDeclarations([sourceA]: [string, ImportDefinition[]], [sourceB]: [string, ImportDefinition[]]): number {
if (sourceA.startsWith('@stoplight/')) {
return sourceB.startsWith('@stoplight/') ? 0 : -1;
} else if (sourceB.startsWith('@stoplight/')) {
return 1;
}

return 0;
}

export class Tree {
readonly #importDeclarations = new Map<
string,
{ imported: namedTypes.Identifier; local: namedTypes.Identifier; default: boolean }[]
>();
readonly #importDeclarations = new Map<string, ImportDefinition[]>();

readonly #npmRegistry;
readonly #module: IModule;
Expand Down Expand Up @@ -65,26 +74,30 @@ export class Tree {

return astring.generate(
b.program([
...Array.from(this.#importDeclarations.entries()).flatMap(([source, identifiers]) => {
const resolvedSource =
this.#npmRegistry !== null && !this.#localPaths.has(source) ? path.join(this.#npmRegistry, source) : source;

const nonDefault = identifiers.filter(({ default: _default }) => !_default);

return [
...(nonDefault.length > 0
? [
this.#module.importDeclaration(
nonDefault.map(({ imported, local }) => [imported, local]),
resolvedSource,
),
]
: <namedTypes.ImportDeclaration[]>[]),
...identifiers
.filter(({ default: _default }) => _default)
.flatMap(({ local }) => this.#module.importDefaultDeclaration(local, resolvedSource)),
];
}),
...Array.from(this.#importDeclarations.entries())
.sort(sortDeclarations)
.flatMap(([source, identifiers]) => {
const resolvedSource =
this.#npmRegistry !== null && !this.#localPaths.has(source)
? path.join(this.#npmRegistry, source)
: source;

const nonDefault = identifiers.filter(({ default: _default }) => !_default);

return [
...(nonDefault.length > 0
? [
this.#module.importDeclaration(
nonDefault.map(({ imported, local }) => [imported, local]),
resolvedSource,
),
]
: <namedTypes.ImportDeclaration[]>[]),
...identifiers
.filter(({ default: _default }) => _default)
.flatMap(({ local }) => this.#module.importDefaultDeclaration(local, resolvedSource)),
];
}),
this.#module.exportDefaultDeclaration(this.ruleset),
...this.#module.dependencies,
]),
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1935,7 +1935,7 @@ __metadata:
languageName: node
linkType: hard

"@stoplight/json@npm:3.17.0, @stoplight/json@npm:^3.17.0, @stoplight/json@npm:~3.17.1":
"@stoplight/json@npm:3.17.0, @stoplight/json@npm:^3.17.0, @stoplight/json@npm:~3.17.0, @stoplight/json@npm:~3.17.1":
version: 3.17.0
resolution: "@stoplight/json@npm:3.17.0"
dependencies:
Expand Down Expand Up @@ -2129,7 +2129,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@stoplight/spectral-ruleset-migrator@workspace:packages/ruleset-migrator"
dependencies:
"@stoplight/json": 3.17.0
"@stoplight/json": ~3.17.0
"@stoplight/ordered-object-literal": 1.0.2
"@stoplight/path": 1.3.2
"@stoplight/spectral-core": ^1.1.0
Expand Down

0 comments on commit e7f6530

Please sign in to comment.