Skip to content

Commit

Permalink
Fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
SMotaal committed May 13, 2020
1 parent 57df303 commit ad76c8b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 24 deletions.
18 changes: 5 additions & 13 deletions packages/matcher/experimental/common/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,16 +444,12 @@ Object.freeze(generateDefinitions);
/**
* @template {string} K
* @template {string} I
* @param {{[i in I]: K[]}} mappings
* @param {Record<I, K[]>} mappings
* @returns {Iterable<K> & Readonly<Record<K, I>> & Readonly<Record<I, ReadonlyArray<K>>>}
*/
export const Keywords = mappings => {
/** @type {{[i in I]: ReadonlyArray<K>}} */
//@ts-ignore
const identities = {};

/** @type {{[k in K]: I}} */
//@ts-ignore
const keywords = {...Keywords.prototype};
const identities = /** @type {any} */ ({});
const keywords = /** @type {any} */ ({...Keywords.prototype});

for (const identity in mappings) {
identities[identity] = Object.freeze([...mappings[identity]]);
Expand All @@ -462,11 +458,7 @@ export const Keywords = mappings => {
}
}

Object.setPrototypeOf(keywords, identities);
Object.freeze(identities);
Object.freeze(keywords);

return keywords;
return Object.freeze(Object.setPrototypeOf(keywords, Object.freeze(identities)));
};

Keywords.prototype = {
Expand Down
32 changes: 23 additions & 9 deletions packages/matcher/experimental/es-tokenizer/es-definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,36 @@ export const {
openers: ['{', '(', '[', "'", '"', '`', '/', '/*', '//'],
// TODO: Properly fault on invalid closer
closers: ['}', ')', ']'],
/** @type {ECMAScript.Keywords} */
// @ts-ignore
keywords: Keywords({
// TODO: Let's make those constructs (this.new.target borks)
// [identities.MetaProperty]: 'new.target import.meta',
[identities.Keyword]: [
[identities.Keyword]: /** @type {ECMAScript.Keyword[]} */ ([
...['await', 'break', 'case', 'catch', 'class', 'const', 'continue'],
...['debugger', 'default', 'delete', 'do', 'else', 'export', 'extends'],
...['finally', 'for', 'function', 'if', 'import', 'in', 'instanceof'],
...['let', 'new', 'return', 'super', 'switch', 'this', 'throw', 'try'],
...['typeof', 'var', 'void', 'while', 'with', 'yield'],
],
[identities.RestrictedWord]: ['interface', 'implements', 'package', 'private', 'protected', 'public'],
[identities.FutureReservedWord]: ['enum'],
]),
[identities.RestrictedWord]: /** @type {ECMAScript.RestrictedWord[]} */ ([
'interface',
'implements',
'package',
'private',
'protected',
'public',
]),
[identities.FutureReservedWord]: /** @type {ECMAScript.FutureReservedWord[]} */ (['enum']),
// NOTE: This is purposely not aligned with the spec
[identities.ContextualWord]: ['arguments', 'async', 'as', 'from', 'of', 'static', 'get', 'set'],
[identities.ContextualWord]: /** @type {ECMAScript.ContextualWord[]} */ ([
'arguments',
'async',
'as',
'from',
'of',
'static',
'get',
'set',
]),
}),

punctuation: {
Expand Down Expand Up @@ -413,6 +427,6 @@ export const {
* @typedef {'await'|'break'|'case'|'catch'|'class'|'const'|'continue'|'debugger'|'default'|'delete'|'do'|'else'|'export'|'extends'|'finally'|'for'|'function'|'if'|'import'|'in'|'instanceof'|'new'|'return'|'super'|'switch'|'this'|'throw'|'try'|'typeof'|'var'|'void'|'while'|'with'|'yield'} ECMAScript.Keyword
* @typedef {'interface'|'implements'|'package'|'private'|'protected'|'public'} ECMAScript.RestrictedWord
* @typedef {'enum'} ECMAScript.FutureReservedWord
* @typedef {'arguments'|'async'|'as'|'from'|'of'|'static'} ECMAScript.ContextualKeyword
* @typedef {Record<ECMAScript.Keyword|ECMAScript.RestrictedWord|ECMAScript.FutureReservedWord|ECMAScript.ContextualKeyword, symbol>} ECMAScript.Keywords
* @typedef {'arguments'|'async'|'as'|'from'|'of'|'static'|'get'|'set'} ECMAScript.ContextualWord
* @typedef {Record<ECMAScript.Keyword|ECMAScript.RestrictedWord|ECMAScript.FutureReservedWord|ECMAScript.ContextualWord, symbol>} ECMAScript.Keywords
*/
1 change: 1 addition & 0 deletions packages/matcher/experimental/es-tokenizer/es-matcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ export const matcher = (ECMAScript =>
`${ECMAScriptGoal.ranges.IdentifierStart}${ECMAScriptGoal.ranges.IdentifierPart}`.includes('\\p{') ? 'u' : '',
),
Number: ({
//@ts-ignore
NumericSeparator,
Digits = NumericSeparator
? Digit => TokenMatcher.sequence/* regexp */ `[${Digit}][${Digit}${TokenMatcher.escape(NumericSeparator)}]*`
Expand Down
2 changes: 2 additions & 0 deletions packages/matcher/experimental/html-tokenizer/html-matcher.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//@ts-check

import {TokenMatcher} from '../../lib/token-matcher.js';
import {HTMLGoal} from './html-definitions.js';

Expand Down
3 changes: 1 addition & 2 deletions packages/matcher/lib/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

interface Matcher extends import('./lib/matcher.js').Matcher, RegExp {}

type MatcherFlags = string;
type MatcherText = string;
type MatcherPattern = (string | RegExp) & Definition;
type MatcherEntityFactory = (entity: MatcherEntity) => void;
type MatcherEntityFactory = (entity: MatcherEntity | Matcher) => void;
type MatcherPatternFactory = (entity: MatcherEntityFactory) => MatcherPattern;

type MatcherMatch<T extends MatcherArray = RegExpExecArray | RegExpMatchArray> = T extends
Expand Down

0 comments on commit ad76c8b

Please sign in to comment.